Exemplo n.º 1
0
 def __init__(self, autoGC=True, dbname='generic', **kwargs):
     "Initialize seq db from filepath or ifile"
     if autoGC: # automatically garbage collect unused objects
         self._weakValueDict = classutil.RecentValueDictionary(autoGC)
     else:
         self._weakValueDict = {}  # object cache
     self.autoGC = autoGC
     kwargs = kwargs.copy() # get a copy we can modify w/o side effects
     classutil.apply_itemclass(self, kwargs)
     kwargs['db'] = self
     classutil.get_bound_subclass(self, 'itemClass', dbname,
                                  subclassArgs=kwargs)
     self.set_seqtype()
Exemplo n.º 2
0
    def __init__(self,
                 filename,
                 mode=None,
                 writeback=False,
                 unpicklingMode=False,
                 verbose=True,
                 **kwargs):
        '''Wrapper for a shelve object that can be pickled.  Ideally, you
should specify a TWO letter mode string: the first letter to
indicate what mode the shelve should be initially opened in, and
the second to indicate the mode to open the shelve during unpickling.
e.g. mode='nr': to create an empty shelve (writable),
which in future will be re-opened read-only.
Also, mode=None makes it first attempt to open read-only, but if the file
does not exist will create it using mode 'c'. '''
        # Mark this string as a file path.
        self.filename = classutil.SourceFileName(str(filename))
        self.writeback = writeback
        if unpicklingMode or mode is None or mode == 'r':
            # Just use mode as given.
            self.mode = mode
        elif mode == 'n' or mode == 'c' or mode == 'w':
            # Ambiguous modes, warn & set default.
            if verbose:
                import sys
                print >> sys.stderr, '''Warning: you opened shelve file %s
in mode '%s' but this is ambiguous for how the shelve should be
re-opened later during unpickling.  By default it will be
re-opened in mode 'r' (read-only).  To make it be re-opened
writable, create it in mode '%sw', or call its method
reopen('w'), which will make it be re-opened in mode 'w' now and
in later unpickling.  To suppress this warning message, use the
verbose=False option.''' % (filename, mode, mode)
            self.mode = 'r'
        else:  # PROCESS UNAMBIGUOUS TWO-LETTER mode STRING
            try:
                if len(mode) == 2 and mode[0] in 'ncwr' and mode[1] in 'cwr':
                    self.mode = mode[1]  # IN FUTURE OPEN IN THIS MODE
                    mode = mode[0]  # OPEN NOW IN THIS MODE
                else:
                    raise ValueError('invalid mode string: ' + mode)
            except TypeError:
                raise ValueError('file mode must be a string!')
        if unpicklingMode:
            self.d = classutil.open_shelve(filename,
                                           mode,
                                           writeback,
                                           allowReadOnly=True)
        else:
            self.d = classutil.open_shelve(filename, mode, writeback)
        classutil.apply_itemclass(self, kwargs)
Exemplo n.º 3
0
 def __init__(self, autoGC=True, dbname='generic', **kwargs):
     "Initialize seq db from filepath or ifile"
     if autoGC:  # automatically garbage collect unused objects
         self._weakValueDict = classutil.RecentValueDictionary(autoGC)
     else:
         self._weakValueDict = {}  # object cache
     self.autoGC = autoGC
     kwargs = kwargs.copy()  # get a copy we can modify w/o side effects
     classutil.apply_itemclass(self, kwargs)
     kwargs['db'] = self
     classutil.get_bound_subclass(self,
                                  'itemClass',
                                  dbname,
                                  subclassArgs=kwargs)
     self.set_seqtype()
Exemplo n.º 4
0
    def __init__(self, filename, mode=None, writeback=False,
                 unpicklingMode=False, verbose=True, **kwargs):
        '''Wrapper for a shelve object that can be pickled.  Ideally, you
should specify a TWO letter mode string: the first letter to
indicate what mode the shelve should be initially opened in, and
the second to indicate the mode to open the shelve during unpickling.
e.g. mode='nr': to create an empty shelve (writable),
which in future will be re-opened read-only.
Also, mode=None makes it first attempt to open read-only, but if the file
does not exist will create it using mode 'c'. '''
        # Mark this string as a file path.
        self.filename = classutil.SourceFileName(str(filename))
        self.writeback = writeback
        if unpicklingMode or mode is None or mode == 'r':
            # Just use mode as given.
            self.mode = mode
        elif mode == 'n' or mode == 'c' or mode == 'w':
            # Ambiguous modes, warn & set default.
            if verbose:
                import sys
                print >>sys.stderr, '''Warning: you opened shelve file %s
in mode '%s' but this is ambiguous for how the shelve should be
re-opened later during unpickling.  By default it will be
re-opened in mode 'r' (read-only).  To make it be re-opened
writable, create it in mode '%sw', or call its method
reopen('w'), which will make it be re-opened in mode 'w' now and
in later unpickling.  To suppress this warning message, use the
verbose=False option.''' % (filename, mode, mode)
            self.mode = 'r'
        else: # PROCESS UNAMBIGUOUS TWO-LETTER mode STRING
            try:
                if len(mode) == 2 and mode[0] in 'ncwr' and mode[1] in 'cwr':
                    self.mode = mode[1] # IN FUTURE OPEN IN THIS MODE
                    mode = mode[0] # OPEN NOW IN THIS MODE
                else:
                    raise ValueError('invalid mode string: ' + mode)
            except TypeError:
                raise ValueError('file mode must be a string!')
        if unpicklingMode:
            self.d = classutil.open_shelve(filename, mode, writeback,
                                           allowReadOnly=True)
        else:
            self.d = classutil.open_shelve(filename, mode, writeback)
        classutil.apply_itemclass(self, kwargs)
Exemplo n.º 5
0
 def __init__(self,saveDict=None,dictClass=dict,**kwargs):
     '''saveDict, if not None, is the internal mapping to use as our storage.
     filename: if provided, is a file path to a shelve (BerkeleyDB) file to
           store the data in.
     dictClass: if provided, is the class to use for storage of the dict data.'''
     if saveDict is not None:
         self.d = saveDict
     elif 'filename' in kwargs: # USE A SHELVE (BERKELEY DB)
         try:
             if kwargs['intKeys']: # ALLOW INT KEYS, HAVE TO USE IntShelve
                 self.__class__ = IntShelve
             else:
                 raise KeyError
         except KeyError:
             self.__class__ = PicklableShelve
         return self.__init__(**kwargs)
     else:
         self.d = dictClass()
     classutil.apply_itemclass(self,kwargs)
Exemplo n.º 6
0
 def __init__(self, saveDict=None, dictClass=dict, **kwargs):
     '''saveDict, if not None, the internal mapping to use as our storage.
     filename: if provided, a file path to a shelve (BerkeleyDB) file to
           store the data in.
     dictClass: if provided, the class to use for storage of dict data.'''
     if saveDict is not None:
         self.d = saveDict
     elif 'filename' in kwargs:  # USE A SHELVE (BERKELEY DB)
         try:
             if kwargs['intKeys']:  # ALLOW INT KEYS, HAVE TO USE IntShelve
                 self.__class__ = IntShelve
             else:
                 raise KeyError
         except KeyError:
             self.__class__ = PicklableShelve
         return self.__init__(**kwargs)
     else:
         self.d = dictClass()
     classutil.apply_itemclass(self, kwargs)