Ejemplo n.º 1
0
    def __init__(
            self,
            filename=None,
            mode='c',
            tablename='ostap',
            writeback=True,  ## original name: "autocommit"
            compress_level=zlib.Z_BEST_COMPRESSION,
            journal_mode="DELETE",
            protocol=PROTOCOL):
        """Initialize a thread-safe sqlite-backed dictionary.
        The dictionary will be a table ``tablename`` in database file
        ``filename``. A single file (=database) may contain multiple tables.
        
        If no ``filename`` is given, a random file in temp will be used
        (and deleted from temp once the dict is closed/deleted).
        
        If you enable ``writeback/autocommit`` changes will be committed
        after each operation (more inefficient but safer).
        Otherwise, changes are committed on
        ``self.commit()``,
        ``self.clear()`` and
        ``self.close()``.
        
        Set ``journal_mode`` to ``OFF``
        if you're experiencing sqlite I/O problems
        or if you need performance and don't care about crash-consistency.
        
        The `mode` parameter:
        - 'c': default mode, open for read/write, creating the db/table if necessary.
        - 'w': open for r/w, but drop `tablename` contents first (start with empty table)
        - 'n': create a new database (erasing any existing tables, not just `tablename`!).
        
        Modes: %s 
        """ % _modes_

        ## the mode
        mode = _modes_.get(mode.lower(), '')
        if not mode:
            logger.warning("Unknown opening mode '%s', replace with 'c'")
            mode = 'c'

        if not filename is None:
            import os
            filename = os.path.expandvars(filename)
            filename = os.path.expanduser(filename)
            filename = os.path.expandvars(filename)
            filename = os.path.abspath(filename)

        self.__filename = filename

        SqliteDict.__init__(self,
                            filename=filename,
                            tablename=tablename,
                            flag=mode,
                            autocommit=writeback,
                            journal_mode=journal_mode)

        self.__compression = compress_level
        self.__protocol = protocol
Ejemplo n.º 2
0
def dbopen(file, flag='r', mode=0o666, concurrent=True, **kwargs):
    """Open or create database at path given by *file*.
    
    Optional argument *flag* can be 'r' (default) for read-only access, 'w'
    for read-write access of an existing database, 'c' for read-write access
    to a new or existing database, and 'n' for read-write access to a new
    database.
    
    Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
    only if it doesn't exist; and 'n' always creates a new database.
    
    - Actually it is a bit extended  form of `dbm.open` that  accounts for `bsddb3` and `sqlite3`
    """

    if 'n' in flag and os.path.isfile(file):
        os.unlink(file)

    check = whichdb(file) if 'n' not in flag else None

    if 'c' in flag and '' == check:
        check = None
        os.unlink(file)

    # 'n' flag is specified  or dbase does not exist and c flag is specified
    if 'n' in flag or (check is None and 'c' in flag):

        if concurrent and use_berkeleydb:
            return berkeleydb_open(file, flag, mode, **kwargs)

        if concurrent and use_bsddb3:
            return bsddb3.hashopen(file, flag, mode, **kwargs)

        if concurrent:
            return SqliteDict(filename=file, flag=flag, **kwargs)

        return std_db.open(file, flag, mode)

    if use_berkeleydb and check in ('berkeleydb', 'bsddb3', 'dbhash'):
        return berkeleydb_open(file, flag, mode, **kwargs)

    if use_bsddb3 and check in ('berkeleydb', 'bsddb3', 'bsddb', 'dbhash',
                                'bsddb185'):
        return bsddb3.hashopen(file, flag, mode, **kwargs)

    if check == 'sqlite3':
        return SqliteDict(filename=file, flag=flag, **kwargs)

    return std_db.open(file, flag, mode)
Ejemplo n.º 3
0
    def dbopen(file, flag='r', mode=0o666, **kwargs):
        """Open or create database at path given by *file*.
        
        Optional argument *flag* can be 'r' (default) for read-only access, 'w'
        for read-write access of an existing database, 'c' for read-write access
        to a new or existing database, and 'n' for read-write access to a new
        database.
        
        Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
        only if it doesn't exist; and 'n' always creates a new database.
        
        - Actually it is a bit extended  form of `dbm.open` that  accounts for `sqlite3`
        """

        result = whichdb(file) if 'n' not in flag else None

        if result is None:

            # db doesn't exist or 'n' flag was specified to create a new db

            if 'c' in flag or 'n' in flag:

                # file doesn't exist and the new flag was used so use bsddb3
                return anydbm.open(file, flag, mode)

            raise anydbm.error[0](
                "db file '%s' doesn't exist; use 'c' or 'n' flag to create a new db"
                % file)

        elif result is 'sqlite3':

            return SqliteDict(filename=file, flag=flag, **kwargs)

        return anydbm.open(file, flag, mode)
Ejemplo n.º 4
0
    def close ( self ) :
        """ Close the file (and compress it if required) 
        """

        if self.flag == 'c' : 
            dct = self.get ( '__metainfo__' , {} )
            dct  [ 'Updated at'                  ] = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' )   
            dct  [ 'Updated by'                  ] = meta_info.User 
            dct  [ 'Updated with Ostap version'  ] = meta_info.Ostap 
            dct  [ 'Updated with Python version' ] = meta_info.Python 
            dct  [ 'Updated with ROOT version'   ] = meta_info.ROOT   
            self [ '__metainfo__' ] = dct

        return SqliteDict.close ( self )
Ejemplo n.º 5
0
        def dbopen(file, flag='r', mode=0o666, **kwargs):
            """Open or create database at path given by *file*.
            
            Optional argument *flag* can be 'r' (default) for read-only access, 'w'
            for read-write access of an existing database, 'c' for read-write access
            to a new or existing database, and 'n' for read-write access to a new
            database.
            
            Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
            only if it doesn't exist; and 'n' always creates a new database.
            
            - Actually it is a bit extended  form of `dbm.open` that  accounts for `bsddb3` and `sqlite3`
            """

            result = whichdb(file) if 'n' not in flag else None

            db = None
            if result is None:

                # db doesn't exist or 'n' flag was specified to create a new db
                if 'c' in flag or 'n' in flag:

                    # file doesn't exist and the new flag was used so use bsddb3
                    db = bsddb3.hashopen(file, flag, mode)

                else:

                    raise dbm.error[0](
                        "db file '%s' doesn't exist; use 'c' or 'n' flag to create a new db"
                        % file)

            elif result in ('bsddb', 'dbhash', 'bsddb3', 'bsddb185'):

                db = bsddb3.hashopen(file, flag, mode)

            elif result == 'sqlite3':

                db = SqliteDict(filename=file, flag=flag, *kwargs)

            ## use DBM
            if db is None: db = dbm.open(file, flag, mode)

            logger.debug("Open DBASE %s of type %s/%s" %
                         (file, whichdb(file), type(db)))
            return db