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 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 sync(self): logger.info('Syncing persistent DB file %s', self.filename) Shelf.sync(self)
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()
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 sync(self): with self._lock: Shelf.sync(self)