numeric.py (1728B)
1 # standard imports 2 import math 3 import os 4 import logging 5 6 # local imports 7 from .base import LevelDir 8 9 logg = logging.getLogger(__name__) 10 11 12 class NumDir(LevelDir): 13 14 def __init__(self, root_path, thresholds=[1000]): 15 thresholds = self.__thresholds_sanity(thresholds) 16 super(NumDir, self).__init__(root_path, len(thresholds), 8) 17 self.thresholds = thresholds 18 fi = os.stat(self.master_file) 19 self.entry_length = 8 20 21 22 def __thresholds_sanity(self, thresholds): 23 if len(thresholds) == 0: 24 raise ValueError('thresholds must have at least one value') 25 last_t = thresholds[0] 26 for i in range(len(thresholds) - 1): 27 if thresholds[i+1] > last_t: 28 raise ValueError('thresholds must have diminishing order') 29 return thresholds 30 31 32 def to_dirpath(self, n): 33 c = n 34 x = 0 35 d = [] 36 v = 0 37 logg.debug('dirpath {}'.format(n)) 38 for t in self.thresholds: 39 x = math.floor(c / t) 40 y = x * t 41 v += y 42 d.append(str(v)) 43 c -= y 44 return os.path.join(self.path, *d) 45 46 47 def to_filepath(self, n): 48 path = self.to_dirpath(n) 49 return os.path.join(path, str(n)) 50 51 52 def add(self, n, content, prefix=b''): 53 entry_path = self.to_filepath(n) 54 55 os.makedirs(os.path.dirname(entry_path), exist_ok=True) 56 57 f = open(entry_path, 'wb') 58 f.write(content) 59 f.close() 60 61 f = open(self.master_file, 'ab') 62 f.write(n.to_bytes(8, byteorder='big')) 63 f.close() 64 65 c = self.count() 66 67 logg.debug('created new numdir entry {} idx {} in {}'.format(str(n), c, entry_path))