Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, database_path: str):
self.db = dbshelve.open(database_path)
def close(self):
def _reopen():
_ensure_shelf_exists()
global _shelf
assert _shelf is None
_shelf = shelve.open(_shelfname.encode("utf_8"))
# don't bother to re-update our _cache! This would be too slow to do every time.
return
if self.options.dry_run:
open_flag = "r"
else:
open_flag = "c"
if self.options.dry_run and not os.path.exists(db_file):
db = {}
else:
try:
db = shelve.open(db_file, flag=open_flag)
except ImportError as e:
# ImportError has different attributes for python2 vs. python3
if (getattr(e, 'name', None) == 'bsddb' or
getattr(e, 'message', None) == 'No module named bsddb'):
from bsddb3 import dbshelve
db = dbshelve.open(db_file, flags=open_flag)
if self.options.dry_run:
logging.warning("dry-run: %s db opened in readonly mode" % db_desc)
if not isinstance(db, dict):
volatile_db = dict((k, db[k]) for k in db)
db.close()
db = volatile_db
else:
self._open_files.append(db)
return db