Beispiel #1
0
 def unlock(self, lock_slug):
     lock_filename = self.lock_filename(lock_slug)
     if not exists(lock_filename):
         raise StorageError(f"Can't unlock: Storage wan't locked: "
                            f'"{lock_filename}"')
     os.remove(lock_filename)
     self.locked = False
Beispiel #2
0
 def get(self, silent=False):
     if self.index is None:
         if silent:
             return ''
         raise StorageError(f"Block doesn't contain title: '{self.title}', "
                            f"path: {self.path}")
     return self.contents[self.index].split('\t', maxsplit=1)[1]
Beispiel #3
0
 def lock(self, lock_slug):
     lock_filename = self.lock_filename(lock_slug)
     if exists(lock_filename):
         raise StorageError(f"Can't lock: Storage is already locked: "
                            f'"{lock_filename}"')
     write(lock_filename, dt())
     self.locked = True
Beispiel #4
0
    def __init__(self, path):
        # print(path)
        self.path = path
        self.contents = None  # should be set in inheritors
        self.titles = None  # should be set in inheritors

        self.block_content = read(self.path)
        if not self.block_content:
            raise StorageError(f'Block is empty: "{self.path}"')
Beispiel #5
0
 def get_contents_and_title(self):
     block_content = read(self.path)
     if not block_content:
         if exists(f'{self.path}.bak'):
             raise StorageError(f'Block is empty: "{self.path}"')
     if block_content:
         self.contents = block_content.split('\n')
     else:
         self.contents = []
     self.titles = [line.split('\t')[0] for line in self.contents]
Beispiel #6
0
    def iterate_pages_with_info(self, limit=None, silent=False):  # todo: start_from
        from libs.parse.storage_page import IteratedStoragePage

        # todo: skip_errors ? -- имеется в виду даже не возвращать ошибочные или возвращать с флагом?
        # todo: sorted=...
        # todo: cyrilic= latin=...
        count = 0
        iterator = zip(
            storage.iterate('content'),
            storage.iterate('info'),
        )
        for (title, content), (title_2, info) in iterator:
            if title != title_2:
                print(title, '≠', title_2)
                raise StorageError('Internal storage error (inconsistency)')
            yield title, \
                  IteratedStoragePage(title, content, info, silent=silent)
            count += 1
            if limit and count >= limit:  # todo: fix use `i` instead of `count`
                break
Beispiel #7
0
 def create_dir(self, path, structure, level):
     print(level, path)
     if not self.splitting or not exists(path):
         os.makedirs(path)
     for prefix, sub_structure in structure.items():
         # print(' ' * level, prefix)
         key = prefix
         if level > 1:
             code = ord(prefix[-1]) if len(prefix) + 1 >= level else 0
             key = f'{code} - {hex(code)}'
         print(' ' * level, key)
         new_path = join(path, key)
         if type(sub_structure) == dict:
             # print(' ' * level, prefix)
             self.create_dir(new_path, sub_structure, level + 1)
         else:
             if exists(new_path) and not self.splitting:
                 raise StorageError(f"File shouldn't exist: '{new_path}'")
             self.save_data(new_path, prefix, sub_structure)
             if self.splitting:
                 copy(new_path, f'{new_path}.new')
Beispiel #8
0
    def get_block(self, title):
        def is_block(_dict, key):
            if key not in _dict:
                _dict[key] = list()
            return type(_dict[key]) == list

        category, name = self.char_info[title[0]]

        if is_block(self.structure, name):
            return self.structure[name]

        prefixes = self.structure[name]
        for i in range(1, MAX_DEPTH):
            prefix = title[:i]
            if is_block(prefixes, prefix):
                return prefixes[prefix]

            prefixes = prefixes[prefix]

        # todo: Возможно всё-таки возвращать здесь последний блок?
        raise StorageError(u'Prefix length is too big.')
Beispiel #9
0
 def get_contents_and_title(self):
     block_content = read(self.path)
     if not block_content:
         raise StorageError(f'Block is empty: "{self.path}"')
     self.contents = block_content.split(SEPARATOR)
     self.titles = self.contents[0].split('\n')