def __init__(self, filename, flag, key_type='str', dump_method=None, cached=True, writeback=False): if flag=='r': Shelf.__init__(self, tinycdb.read(filename), -1, writeback) elif flag=='c': Shelf.__init__(self, tinycdb.create(filename), -1, writeback) self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag, key_type='str', dump_method=None, cached=True, writeback=False): if flag=='r': Shelf.__init__(self, CdbReadDict(filename), -1, writeback) elif flag=='c': Shelf.__init__(self, CdbWriteDict(filename), -1, writeback) self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag='c', protocol=None, keyencoding='utf-8'): self.db = filename self.flag = flag self.dict = {} with dbm.open(self.db, self.flag) as db: for k in db.keys(): v = io.BytesIO(db[k]) self.dict[k] = Unpickler(v).load() Shelf.__init__(self, self.dict, protocol, False, keyencoding)
def __init__( self, filename=None, flag="", key_type="unicode", dump_method=None, cached=True, connection=None, table="shelf" ): Shelf.__init__(self, SqliteDict(filename, connection, table)) # 'int' type packs integer key to 2-byte sequence and # sqlite doesn't support binary data without extra efforts if key_type == "int": key_type = "unicode" self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(anydbm.open(filename, flag))
def __init__(self, filename=None, flag='', key_type='str', dump_method='json', cached=True, connection=None, table='shelf',): Shelf.__init__(self, SqliteDict(filename, connection, table)) # 'int' type packs integer key to 2-byte sequence and # sqlite doesn't support binary data without extra efforts if key_type == 'int': key_type = 'unicode' self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag, key_type='str', dump_method='json', cached=True, writeback=False): if flag == 'r': Shelf.__init__(self, CdbReadDict(filename), -1, writeback) elif flag == 'c': Shelf.__init__(self, CdbWriteDict(filename), -1, writeback) self._setup_methods(cached, key_type, dump_method)
def set_ui_dir(shelf: shelve.Shelf): if len(sys.argv) != 3: print("Wrong argument count") return ui_dir = sys.argv[2] if not os.path.isdir(ui_dir): print("path does not exist") return shelf[UI_DIR_KEY] = ui_dir shelf.sync()
def __init__(self, filename, flag, key_type='str', dump_method=None, cached=True, writeback=False): db = self.DB_CLASS() if flag == 'r': flags = pytc.BDBOREADER elif flag == 'c': flags = pytc.BDBOWRITER | pytc.BDBOREADER | pytc.BDBOCREAT else: raise NotImplementedError db.open(filename, flags) Shelf.__init__(self, db, -1, writeback) self._setup_methods(cached, key_type, dump_method)
def set_uic(shelf: shelve.Shelf): if len(sys.argv) != 3: print("Wrong argument count") return uic_path = sys.argv[2] if not uic_path.endswith("uic.exe"): print("path doesn't end with `uic.exe`") return if not os.path.isfile(uic_path): print("path does not exist") return shelf[UIC_KEY] = uic_path shelf.sync()
def _get_item(self, value_key: str, local_db: Shelf) -> Optional[Any]: version_key = f"{value_key}_{constants.DB_ITEM_VERSION_SUFFIX}" default_version = Version("0.0.0") current_version = self._current_versions[version_key] actual_version = (checked_cast(Version, local_db[version_key]) if version_key in local_db else default_version) return local_db.get( value_key) if actual_version >= current_version else None
def __init__( self, filename=None, flag='', key_type='str', dump_method='json', cached=True, connection=None, table='shelf', ): Shelf.__init__(self, SqliteDict(filename, connection, table)) # 'int' type packs integer key to 2-byte sequence and # sqlite doesn't support binary data without extra efforts if key_type == 'int': key_type = 'unicode' self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag, key_type='str', dump_method='json', cached=True, writeback=False): db = self.DB_CLASS() if flag == 'r': flags = pytc.BDBOREADER elif flag == 'c': flags = pytc.BDBOWRITER | pytc.BDBOREADER | pytc.BDBOCREAT else: raise NotImplementedError db.open(filename, flags) Shelf.__init__(self, db, -1, writeback) self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(dbm.open(filename, flag))
def open(filename, flag='c'): """Open a persistent dictionary for reading and writing. The filename parameter is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created. The optional flag parameter has the same interpretation as the flag parameter of dbm.open(). """ writeback = False from . import _dumbdbm_patched d = _dumbdbm_patched.open(filename, flag) return Shelf(ZlibMapping(d) if zlib is not None else d)
async def render(stdscr: 'curses._CursesWindow', slos: shelve.Shelf) -> None: q: 'asyncio.Queue[str]' = asyncio.Queue(maxsize=1) asyncio.create_task(tick(q)) asyncio.create_task(readio(stdscr, q)) state = State() while True: k = await q.get() if k == 'q': return state.handle(k) items = sorted(slos.values(), key=state.key, reverse=state.reverse) height, width = stdscr.getmaxyx() i = 0 stdscr.clear() for payload in items: if i >= height - 1 - len(payload.results): # TODO: pagination break stdscr.addstr(i, 0, payload.name) for result in payload.results: stdscr.addstr(i, width // 2, result.name) value = f'{result.value:.2f}' style = (1 if result.value >= 99.99 else 2 if result.value >= 99.9 else 3) stdscr.addstr(i, width - len(value) - 1, value, curses.color_pair(style)) i += 1 stdscr.addstr(i, 0, '') state.render_bar(stdscr, height - 1) stdscr.refresh()
def __delitem__(self, key): logger.debug('Removing key "%s" from persistent DB', key) Shelf.__delitem__(self, key)
def __init__(self, filename, flag='c', protocol=None, writeback=False): self.filename = filename logger.info('Opening persistent DB file %s', filename) Shelf.__init__(self, dbm.open(filename, flag, mode=0o600), protocol, writeback)
def __getitem__(self, item): with self._lock: return Shelf.__getitem__(self, item)
def __init__(self, filename, flag='c'): import dumbdbm_patched Shelf.__init__(self, dumbdbm_patched.open(filename, flag))
def sync(self): logger.info('Syncing persistent DB file %s', self.filename) Shelf.sync(self)
def __getitem__(self, key): return Shelf.__getitem__(self, str(key))
def sync(self): with self._lock: Shelf.sync(self)
def get(self, key, default=None): with self._lock: return Shelf.get(self, key, default)
def __contains__(self, item): with self._lock: return Shelf.__contains__(self, item)
def __len__(self): with self._lock: return Shelf.__len__(self)
def __del__(self): with self._lock: Shelf.__del__(self)
def __delitem__(self, key): return Shelf.__delitem__(self, str(key))
def close(self): logger.info('Closing persistent DB file %s', self.filename) Shelf.close(self)
def close(self): with self._lock: Shelf.close(self)
def keys(self): with self._lock: return Shelf.keys(self)
def __init__(self, filename, flag='c'): from . import _dumbdbm_patched Shelf.__init__(self, _dumbdbm_patched.open(filename, flag))
class ShelfDatabase(database.Database): """ShelfDatabase is a thread-safe implementation of the journal.database.Database interface which uses python Shelve for the underlying persistence. Attributes: lock (threading.RLock): A reentrant lock to ensure threadsafe access. shelf (shelve.Shelf): The underlying shelf database. """ def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(anydbm.open(filename, flag)) def __len__(self): with self._lock: return len(self._shelf) def __contains__(self, key): with self._lock: return key in self._shelf def get(self, key): """Retrieves a value associated with a key from the database Args: key (str): The key to retrieve """ with self._lock: return self._shelf.get(key, default=None) def set(self, key, value): """Sets a value associated with a key in the database Args: key (str): The key to set. value (str): The value to associate with the key. """ with self._lock: self._shelf[key] = value def delete(self, key): """Removes a key:value from the database Args: key (str): The key to remove. """ with self._lock: del self._shelf[key] def sync(self): """Ensures that pending writes are flushed to disk """ with self._lock: self._shelf.sync() def close(self): """Closes the connection to the database """ with self._lock: self._shelf.close() def keys(self): """Returns a list of keys in the database """ with self._lock: return self._shelf.keys()
def __init__(self, filename, flag='c'): Shelf.__init__(self, dumbdbm_patched.open(filename, flag))
def __init__(self, filename=None, flag='', key_type='str', dump_method='json', cached=True, connection=None, table='shelf',): Shelf.__init__(self, SqliteDict(filename, connection, table)) self._setup_methods(cached, key_type, dump_method)
def __init__(self, filename, mode='c', protocol=None, writeback=False): Shelf.__init__(self, SQLhash(filename, mode), protocol, writeback)
def __setitem__(self, key, value): logger.debug('Assigning item %r to persistent DB key "%s"', value, key) Shelf.__setitem__(self, key, value)
def __setitem__(self, key, value): return Shelf.__setitem__(self, self._prefix_key(key), value)
def __delitem__(self, key): with self._lock: Shelf.__delitem__(self, key)
def __setitem__(self, key, val): return Shelf.__setitem__(self, str(key), val)
def __init__(self, redis, key_prefix=None, protocol=None, writeback=False): self._prefix = '{}|'.format(key_prefix) if key_prefix else '' Shelf.__init__(self, dict=redis, protocol=protocol, writeback=writeback)
class ShelfDatabase(database.Database): """ShelfDatabase is a thread-safe implementation of the sawtooth_validator.database.Database interface which uses python Shelve for the underlying persistence. Attributes: lock (threading.RLock): A reentrant lock to ensure threadsafe access. shelf (shelve.Shelf): The underlying shelf database. """ def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(dbm.open(filename, flag)) def __len__(self): with self._lock: return len(self._shelf) def __contains__(self, key): with self._lock: return key in self._shelf def get(self, key): """Retrieves a value associated with a key from the database Args: key (str): The key to retrieve """ with self._lock: return self._shelf.get(key, default=None) def set(self, key, value): """Sets a value associated with a key in the database Args: key (str): The key to set. value (str): The value to associate with the key. """ with self._lock: self._shelf[key] = value def delete(self, key): """Removes a key:value from the database Args: key (str): The key to remove. """ with self._lock: del self._shelf[key] def sync(self): """Ensures that pending writes are flushed to disk """ with self._lock: self._shelf.sync() def close(self): """Closes the connection to the database """ with self._lock: self._shelf.close() def keys(self): """Returns a list of keys in the database """ with self._lock: return self._shelf.keys()
def __delitem__(self, key): return Shelf.__delitem__(self, self._prefix_key(key))
def __init__(self, filename, flag='c'): from . import dumbdbm_patched Shelf.__init__(self, dumbdbm_patched.open(filename, flag))
def __init__(self, filename, flag='c', protocol=None, writeback=False): Shelf.__init__(self, dbm.ndbm.open(filename, flag), protocol, writeback)
def __init__(self, filename, flag='c', protocol=None, writeback=False): self.filename = filename self.flag = flag Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)