Exemplo n.º 1
0
 def test_project_in_database():
     'It test the ability to know if the databse is already added'
     project = 'test'
     engine = sqlalchemy.create_engine('sqlite:///:memory:')
     create_naming_database(engine)
     assert not project_in_database(engine, project)
     add_project_to_naming_database(engine, name=project, code='my',
                                    description='a test project')
     assert project_in_database(engine, project)
     assert not project_in_database(engine, 'test2')
def _change_names_in_file(io_options, database, filecache, project,
                          feature_kind, description):
    'It changes the name to a file giving a naming'
    # check if the database exits
    if database is not None:
        engine   = sqlalchemy.create_engine( 'sqlite:///%s'  % database)
        if not os.path.exists(database):
            create_naming_database(engine)
        # check if the project exists
        project_name = project['name']
        if not project_in_database(engine, project_name):
            if project['code']:
                add_project_to_naming_database(engine, name=project_name,
                                           code=project['code'],
                                           description=project['description'])
            else:
                msg  = 'To add a new project to the database the code should be'
                msg += ' given'
                raise ValueError(msg)

        # create a naming schema
        naming = DbNamingSchema(engine, project=project_name,
                                feature_kind=feature_kind)
    else:
        naming = None

    if filecache is not None:
        if os.path.exists(filecache):
            mode = 'a'
        else:
            mode = 'w'
        fhand = open(filecache, mode)
        naming = FileNamingSchema(fhand, naming)

    try:
        # change name to seqs in file
        infhand  = io_options['infhand']
        outfhand = io_options['outfhand']
        format   = io_options['format']
        change_names_in_files(infhand, outfhand, naming, format)
        naming.commit(description=description)
    except:
        #if we don't commit we lose the changes in the database
        raise