Exemple #1
0
	def _open_shelve(self, db_file, db_desc):
		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
Exemple #2
0
 def __init__(self, dictName, dictRegistry=NamedDicts):
     """Set up for the protocol by opening the named persistent dictionary.
     """
     self.dictName = dictName
     try:
         self.dbFile = dictRegistry[dictName]['dbFile']
         self.port = dictRegistry[dictName]['port']
         if self.dbFile:
             dbHome = os.path.split(self.dbFile)[0]
             if not os.path.exists(dbHome):
                 os.makedirs(dbHome, 0o777)
             self.dbHome = dbHome
             logFile = dictRegistry[dictName]['logFile']
             if not logFile.startswith('/'):
                 logFile = os.path.join(dbHome, logFile)
             self.logFile = logFile
     except:
         raise PersistentDictFactoryException(
             'Error, no dict of that name: %s' % dictName)
     validateDirectory(os.path.dirname(self.logFile))
     log.startLogging(open(self.logFile, 'w'))
     if dictName == 'Test':
         self.dict = _TestDict
     else:
         self.dict = dbshelve.open(self.dbFile)
         os.chmod(self.dbFile, 0o666)
def _reopen():
    _ensure_shelf_exists()
    global _shelf
    assert _shelf is None
    _shelf = shelve.open(_shelfname)
    # don't bother to re-update our _cache! This would be too slow to do every time.
    return
Exemple #4
0
	def _open_shelve(self, db_file, db_desc):
		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 sys.hexversion < 0x3000000:
				db = ShelveUnicodeWrapper(db)

		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
Exemple #5
0
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
Exemple #6
0
def open_shelve(db_file, flag="r"):
    """
    The optional flag parameter has the same interpretation as the flag
    parameter of dbm.open()
    """
    try:
        db = shelve.open(db_file, flag=flag)
    except ImportError as e:
        # ImportError has different attributes for python2 vs. python3
        if getattr(e, "name", None) == "bsddb" or getattr(e, "message", None) in (
            "No module named bsddb",
            "No module named _bsddb",
        ):
            from bsddb3 import dbshelve

            db = dbshelve.open(db_file)
        else:
            raise

    return db
Exemple #7
0
 def createDB(self):
     self.primary = dbshelve.open(self.filename,
                                  dbname="primary",
                                  dbenv=self.env,
                                  filetype=self.dbtype)
Exemple #8
0
#!/usr/bin/python
#
# Copyright (C) 2005, TUBITAK/UEKAE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# 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

d = shelve.open( sys.argv[1], 'r', 0660, filetype = db.DB_BTREE )

for key, data in d.items():
    print key, data

d.close()
Exemple #9
0
 def do_open(self):
     self.d = dbshelve.open(self.filename)
Exemple #10
0
 def test01_badpointer(self):
     dbs = dbshelve.open(self.filename)
     dbs.close()
     self.assertRaises(db.DBError, dbs.get, "foo")
Exemple #11
0
 def __init__(self, database_path: str):
     self.db = dbshelve.open(database_path)
 def createDB(self):
     self.primary = dbshelve.open(self.filename,
                                  dbname="primary",
                                  dbenv=self.env,
                                  filetype=self.dbtype)
Exemple #13
0
def _make_prefs_shelf():
    """
    [private function]
    call this once per session,
    to create or find the shelf (whose name depends only on the dbm format we'll use for it),
    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 = str_or_unicode(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]
        del _cache[key]
    ###@@@ following should be revised to handle junk contents gracefully,
    # and to notice the existing format version and handle older formats appropriately
    # or reject them gracefully.
    _store_while_open('_format_version', 'preferences.py/v050106')
        # storing this blindly is only ok since the only prior version is one
        # we can transparently convert to this one by the "zap obskeys" above.
    
    # store a comment about the last process to start using this shelf
    # (nothing yet looks at this comment)
    proc_info = "process: pid = %d, starttime = %r" % (os.getpid(), time.asctime())
    _store_while_open( '_fyi/last_proc', proc_info ) # (nothing yet looks at this)
    _close()
    
    if was_just_made:
        # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature];
        # file format must correspond with that written by
        # packaging/Pref_Mod/pref_modifier.py
        default_prefs_values = {}
        # read the values from DEFAULT_PREFS_BASENAME
        # (while shelf is closed, in case this takes time)
        try:
            filename = os.path.join( nanorex, "Preferences", DEFAULT_PREFS_BASENAME )
            if not os.path.exists(filename):
                lines = []
                print u"didn't find", filename.encode("utf_8")
            else:
                file = open( filename, "rU")
                lines = file.readlines()
                file.close()
                print u"reading from", filename.encode("utf_8")
            for line in lines:
                line0 = line
                try:
                    # try/except so corrupted lines don't break good ones added later
                    # assume line has the correct format: key = val\n
                    while line[-1] in ('\r', '\n'):
                        # 'while' is to handle Windows newlines
                        # (probably not needed due to 'rU')
                        line = line[:-1]
                    key, val = line.split(" = ")
                        # don't strip key or val -- they might end with spaces
                    def decode(string1):
                        words = string1.split(r'\\')
                        for i in range(len(words)):
                            word = words[i]
                            word = word.replace(r'\=', '=')
                            word = word.replace(r'\n', '\n')
                            word = word.replace(r'\r', '\r')
                            words[i] = word
                            continue
                        return '\\'.join(words)                        
                    key = decode(key)
                    val = decode(val)
                    if val == 'True':
                        val = True
                    elif val == 'False':
                        val = False
                    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
Exemple #14
0
from bsddb3 import dbshelve
from frowns import Smiles, Smarts, Fingerprint

# ################################################################
# load database (this only needs to be done once
# you can comment this out at a later data
# ################################################################
# this is a persistent disk cache
cache = dbshelve.open("FOO.shelve")
molecules = ["CCCN", "c1ccccc1CCN", "C#C(N)CCc1ccccc1CCc1ccccc1"]

for m in molecules:
    mol = Smiles.smilin(m)
    fp = Fingerprint.generateFingerprint(mol)
    # cache the fingerprint using the canonical smiles
    # as the key and the fingerprint object as the value
    # shelves can store any picklable object!
    cache[mol.cansmiles()] = fp

cache.close()

# ##################################################################
# Iterate through database to see which molecules we need to
# do a full substructure search on
# ##################################################################
cache = dbshelve.open("FOO.shelve", 'r')

query = "CCCN"  # this should reject molecule #2 above

testFP = Fingerprint.generateFingerprint(Smiles.smilin(query))
Exemple #15
0
 def test01_badpointer(self):
     dbs = dbshelve.open(self.filename)
     dbs.close()
     self.assertRaises(db.DBError, dbs.get, "foo")
Exemple #16
0
 def do_open(self):
     self.d = dbshelve.open(self.filename)
def _make_prefs_shelf():
    """
    [private function]
    call this once per session,
    to create or find the shelf (whose name depends only on the dbm format we'll use for it),
    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)
    _cache = {}
    _cache.update(_shelf)  # will this work?
    was_just_made = (not _cache)  #bruce 080505
    if was_just_made:
        print "made prefs db, basename", _shelfname
    else:
        print "prefs db already existed, basename", _shelfname
    _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]
        del _cache[key]
    ###@@@ following should be revised to handle junk contents gracefully,
    # and to notice the existing format version and handle older formats appropriately
    # or reject them gracefully.
    _store_while_open('_format_version', 'preferences.py/v050106')
    # storing this blindly is only ok since the only prior version is one
    # we can transparently convert to this one by the "zap obskeys" above.

    # store a comment about the last process to start using this shelf
    # (nothing yet looks at this comment)
    proc_info = "process: pid = %d, starttime = %r" % (os.getpid(),
                                                       time.asctime())
    _store_while_open('_fyi/last_proc',
                      proc_info)  # (nothing yet looks at this)
    _close()

    if was_just_made:
        # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature];
        # file format must correspond with that written by
        # packaging/Pref_Mod/pref_modifier.py
        default_prefs_values = {}
        # read the values from DEFAULT_PREFS_BASENAME
        # (while shelf is closed, in case this takes time)
        try:
            filename = os.path.join(nanorex, "Preferences",
                                    DEFAULT_PREFS_BASENAME)
            if not os.path.exists(filename):
                lines = []
                print "didn't find", filename
            else:
                file = open(filename, "rU")
                lines = file.readlines()
                file.close()
                print "reading from", filename
            for line in lines:
                line0 = line
                try:
                    # try/except so corrupted lines don't break good ones added later
                    # assume line has the correct format: key = val\n
                    while line[-1] in ('\r', '\n'):
                        # 'while' is to handle Windows newlines
                        # (probably not needed due to 'rU')
                        line = line[:-1]
                    key, val = line.split(" = ")

                    # don't strip key or val -- they might end with spaces
                    def decode(string1):
                        words = string1.split(r'\\')
                        for i in range(len(words)):
                            word = words[i]
                            word = word.replace(r'\=', '=')
                            word = word.replace(r'\n', '\n')
                            word = word.replace(r'\r', '\r')
                            words[i] = word
                            continue
                        return '\\'.join(words)

                    key = decode(key)
                    val = decode(val)
                    if val == 'True':
                        val = True
                    elif val == 'False':
                        val = False
                    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)
        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
Exemple #18
0
from bsddb3 import dbshelve
from frowns import Smiles, Smarts, Fingerprint

# ################################################################
# load database (this only needs to be done once
# you can comment this out at a later data
# ################################################################
# this is a persistent disk cache
cache = dbshelve.open("FOO.shelve")
molecules = ["CCCN", "c1ccccc1CCN", "C#C(N)CCc1ccccc1CCc1ccccc1"]

for m in molecules:
    mol = Smiles.smilin(m)
    fp = Fingerprint.generateFingerprint(mol)
    # cache the fingerprint using the canonical smiles
    # as the key and the fingerprint object as the value
    # shelves can store any picklable object!
    cache[mol.cansmiles()] = fp

cache.close()

# ##################################################################
# Iterate through database to see which molecules we need to
# do a full substructure search on
# ##################################################################
cache = dbshelve.open("FOO.shelve", 'r')

query = "CCCN" # this should reject molecule #2 above

testFP = Fingerprint.generateFingerprint(Smiles.smilin(query))