Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
and create the cache of its contents,
and store a comment there about this process,
and close the shelf again in case a concurrent process is sharing the same shelf with us.
"""
global _shelfname, _shelf, _cache, _defaults, _trackers
nanorex = find_or_make_Nanorex_directory()
global dbname
_shelfname = os.path.join( nanorex, "Preferences", "%s-shelf" % dbname )
# This name should differ when db format differs.
# Note: the actual filename used might have an extension added
# by the db module (in theory, it might even create two files
# with different extensions from the given basename).
# By experiment, on the Mac, with bsddb there is no extension added,
# and without it there is '.db' added. [bruce 050105]
mkdirs_in_filename(_shelfname)
_shelf = shelve.open(_shelfname.encode("utf_8"))
_cache = {}
_cache.update(_shelf) # will this work?
was_just_made = (not _cache) #bruce 080505
if was_just_made:
print u"made prefs db, basename", _shelfname.encode("utf_8")
else:
print u"prefs db already existed, basename", _shelfname.encode("utf_8")
_defaults = {}
_trackers = {}
# zap obsolete contents
obskeys = []
for key in _cache.keys():
if key.isdigit() or key in ['_session_counter']:
obskeys.append(key)
for key in obskeys:
del _shelf[key]
# any later version.
#
# Please read the COPYING file.
#
# Author: Eray Ozkural
import sys
import bsddb3.dbshelve as shelve
import bsddb3.db as db
sys.path.append('.')
import pisi
import pisi.cli
d = shelve.open( sys.argv[1], 'r', 0660, filetype = db.DB_BTREE )
for key, data in d.items():
pisi.cli.printu(u'%s: %s\n' % (key, data))
d.close()
def __open_shelf(self, file_name, table_name, dbtype=db.DB_HASH):
dbmap = dbshelve.DBShelf(self.env)
fname = os.path.join(file_name, table_name + DBEXT)
if self.readonly:
dbmap.open(fname, table_name, dbtype, DBFLAGS_R)
else:
dbmap.open(fname, table_name, dbtype, DBFLAGS_O, DBMODE)
return dbmap
Returns ("Unknown", "Unknown") if invalid DB or other error.
"""
if config.get('preferences.use-bsddb3') or sys.version_info[0] >= 3:
from bsddb3 import dbshelve, db
else:
from bsddb import dbshelve, db
from gramps.gen.db import META, PERSON_TBL
env = db.DBEnv()
flags = db.DB_CREATE | db.DB_PRIVATE |\
db.DB_INIT_MPOOL | db.DB_INIT_LOCK |\
db.DB_INIT_LOG | db.DB_INIT_TXN | db.DB_THREAD
try:
env.open(file_name, flags)
except:
return "Unknown", "Unknown"
dbmap1 = dbshelve.DBShelf(env)
fname = os.path.join(file_name, META + ".db")
try:
dbmap1.open(fname, META, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", "Unknown"
version = dbmap1.get('version', default=None)
dbmap1.close()
dbmap2 = dbshelve.DBShelf(env)
fname = os.path.join(file_name, PERSON_TBL + ".db")
try:
dbmap2.open(fname, PERSON_TBL, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", "Unknown"
count = len(dbmap2)
try:
env.close()
except Exception as msg:
LOG.warning("Error closing db environment for '%s': %s" %
(name, str(msg)))
return "Unknown", bsddb_version, "Unknown"
dbmap1 = dbshelve.DBShelf(env)
fname = os.path.join(dirpath, META + ".db")
try:
dbmap1.open(fname, META, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", bsddb_version, "Unknown"
schema_version = dbmap1.get(b'version', default=None)
dbmap1.close()
dbmap2 = dbshelve.DBShelf(env)
fname = os.path.join(dirpath, PERSON_TBL + ".db")
try:
dbmap2.open(fname, PERSON_TBL, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", bsddb_version, schema_version
count = len(dbmap2)
dbmap2.close()
env.close()
return (count, bsddb_version, schema_version)
env = db.DBEnv()
flags = db.DB_CREATE | db.DB_PRIVATE |\
db.DB_INIT_MPOOL |\
db.DB_INIT_LOG | db.DB_INIT_TXN
try:
env.open(dirpath, flags)
except Exception as msg:
LOG.warning("Error opening db environment for '%s': %s" %
(name, str(msg)))
try:
env.close()
except Exception as msg:
LOG.warning("Error closing db environment for '%s': %s" %
(name, str(msg)))
return "Unknown", bsddb_version, "Unknown"
dbmap1 = dbshelve.DBShelf(env)
fname = os.path.join(dirpath, META + ".db")
try:
dbmap1.open(fname, META, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", bsddb_version, "Unknown"
schema_version = dbmap1.get(b'version', default=None)
dbmap1.close()
dbmap2 = dbshelve.DBShelf(env)
fname = os.path.join(dirpath, PERSON_TBL + ".db")
try:
dbmap2.open(fname, PERSON_TBL, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", bsddb_version, schema_version
count = len(dbmap2)
db.DB_INIT_MPOOL | db.DB_INIT_LOCK |\
db.DB_INIT_LOG | db.DB_INIT_TXN | db.DB_THREAD
try:
env.open(file_name, flags)
except:
return "Unknown", "Unknown"
dbmap1 = dbshelve.DBShelf(env)
fname = os.path.join(file_name, META + ".db")
try:
dbmap1.open(fname, META, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", "Unknown"
version = dbmap1.get('version', default=None)
dbmap1.close()
dbmap2 = dbshelve.DBShelf(env)
fname = os.path.join(file_name, PERSON_TBL + ".db")
try:
dbmap2.open(fname, PERSON_TBL, db.DB_HASH, db.DB_RDONLY)
except:
env.close()
return "Unknown", "Unknown"
count = len(dbmap2)
dbmap2.close()
env.close()
return (count, version)
default_prefs_values[key] = val
# print "read key, val = (%r, %r)" % (key, val)
pass
except:
print "ignoring exception in this line: %r" % (line0,)
pass
continue
pass
except:
print "ignoring exception reading from", DEFAULT_PREFS_BASENAME
default_prefs_values = {}
pass
items = default_prefs_values.items()
items.sort() # just to make the following console prints look nicer
# now open, store the values, and close
_shelf = shelve.open(_shelfname.encode("utf_8"))
for key, val in items:
pkey = _PREFS_KEY_TO_SHELF_KEY(key)
_store_while_open( pkey, val)
print "stored key, val = (%r, %r)" % (key, val)
_close()
pass
return