file.py (1131B)
1 # standard imports 2 import os 3 4 # local imports 5 from .base import ( 6 to_key, 7 BaseStore, 8 ) 9 10 11 class FileStore(BaseStore): 12 """Filesystem backend for storing key value pairs with filenames as keys. 13 14 Base storage directory will be created if it does not exist. 15 16 :param base_dir: Base storage directory 17 :type base_dir: str 18 """ 19 20 def __init__(self, base_dir): 21 os.makedirs(base_dir, exist_ok=True) 22 self.base_dir = base_dir 23 24 25 def put(self, k, v): 26 """Implements taint.store.base.BaseStore 27 """ 28 k = to_key(k) 29 filepath = os.path.join(self.base_dir, k) 30 31 f = open(filepath, 'wb') 32 33 l = len(v) 34 c = 0 35 while c < l: 36 c += f.write(v[c:]) 37 f.close() 38 39 40 def get(self, k): 41 """Implements taint.store.base.BaseStore 42 """ 43 k = to_key(k) 44 filepath = os.path.join(self.base_dir, k) 45 46 f = open(filepath, 'rb') 47 48 b = b'' 49 c = -1 50 while c != 0: 51 d = f.read(4096) 52 c = len(d) 53 b += d 54 55 f.close() 56 57 return b