Esempio n. 1
0
def connect(name,
            type='extract_from_name',
            create_indices=True,
            use_lock_file=True,
            append=True,
            serial=False):
    """Create connection to database.

    name: str
        Filename or address of database.
    type: str
        One of 'json', 'db', 'postgresql',
        (JSON, SQLite, PostgreSQL).
        Default is 'extract_from_name', which will guess the type
        from the name.
    use_lock_file: bool
        You can turn this off if you know what you are doing ...
    append: bool
        Use append=False to start a new database.
    """

    if isinstance(name, PurePath):
        name = str(name)

    if type == 'extract_from_name':
        if name is None:
            type = None
        elif not isinstance(name, basestring):
            type = 'json'
        elif (name.startswith('postgresql://')
              or name.startswith('postgres://')):
            type = 'postgresql'
        else:
            type = os.path.splitext(name)[1][1:]
            if type == '':
                raise ValueError('No file extension or database type given')

    if type is None:
        return Database()

    if not append and world.rank == 0:
        if isinstance(name, str) and os.path.isfile(name):
            os.remove(name)

    if type != 'postgresql' and isinstance(name, basestring):
        name = os.path.abspath(name)

    if type == 'json':
        from ase.db.jsondb import JSONDatabase
        return JSONDatabase(name, use_lock_file=use_lock_file, serial=serial)
    if type == 'db':
        from ase.db.sqlite import SQLite3Database
        return SQLite3Database(name,
                               create_indices,
                               use_lock_file,
                               serial=serial)
    if type == 'postgresql':
        from ase.db.postgresql import PostgreSQLDatabase
        return PostgreSQLDatabase(name)
    raise ValueError('Unknown database type: ' + type)
Esempio n. 2
0
def connect(name,
            type='extract_from_name',
            create_indices=True,
            use_lock_file=True):
    """Create connection to database.
    
    name: str
        Filename or address of database.
    type: str
        One of 'json', 'db', 'postgresql', 'mysql'
        (JSON, SQLite, PostgreSQL, MySQL/MariaDB).
        Default is 'extract_from_name', which will ... guess the type
        from the name.
    use_lock_file: bool
        You can turn this off if you know what you are doing ...
        """

    if type == 'extract_from_name':
        if name is None:
            type = None
        elif name.startswith('postgresql://'):
            type = 'postgresql'
        elif name.startswith('mysql://'):
            type = 'mysql'
        else:
            type = os.path.splitext(name)[1][1:]

    if type is None:
        return Database()

    if type == 'json':
        from ase.db.jsondb import JSONDatabase
        return JSONDatabase(name, use_lock_file=use_lock_file)
    if type == 'db':
        from ase.db.sqlite import SQLite3Database
        return SQLite3Database(name, create_indices, use_lock_file)
    if type == 'postgresql':
        from ase.db.postgresql import PostgreSQLDatabase
        return PostgreSQLDatabase(name[5:])
    if type == 'mysql':
        from ase.db.mysql import MySQLDatabase
        return MySQLDatabase(name[5:])
    raise ValueError('Unknown database type: ' + type)