Пример #1
0
def _assemble_config(opts, default_section_name='default'):
    '''
    This builds a config by the following steps:
    1. read config file (as specified in opts, otherwise empty)
    2. inject environment vars as specified by config
    3. inject opts

    returns a ConfigParserRaw object.
    '''
    if opts.config:
        try:
            config = get_config(opts.config, config_type='Raw')
        except OSError as e:
            if e.errno == 2 and e.filename != _get_default_config_fn():
                raise
            else:
                config = get_config_from_data(f'[{default_section_name}]')
                if opts.debug:
                    warn(f'skipping non-existent config file {opts.config}')

    inject_opts(config, opts)

    # add a convenience method to get opts, which are stored in the default section:
    def opt(self, opt, default=None):
        try:
            return self.get(default_section_name, opt)
        except CP.NoOptionError:
            if default is not None:
                return default
            else:
                raise

    config.opt = MethodType(opt, config)

    return config
Пример #2
0
def sqlA_init_from_config_fn(config_fn, section):
    '''
    Extract db connection information from config/section.  The following keys must be present in the config section:
    host, database, user, password
    '''
    config = get_config(config_fn)
    return sqlA_init_from_config(config, section)
Пример #3
0
 def test_init(self):
     ''' test initialization using sqlA_init_from_config_fn() '''
     mysql_ini = pr.resource_filename('pbutils', 'tests/fixtures/mysql.ini')
     config = get_config(mysql_ini)
     database = config.get('mysql', 'database')
     args = dict(config.items('mysql'))
     args['database'] = 'mysql'
     dbh = get_mysql(**args)
     create_db(dbh, database)
     Session = sqlA_init_from_config_fn(mysql_ini, 'mysql')
     self.assertEqual(type(Session), sessionmaker)
Пример #4
0
    def test_get_mysql(self):
        config_fn = pr.resource_filename('pbutils', 'tests/fixtures/mysql.ini')
        config = get_config(config_fn)
        self.assertIn('mysql', config.sections())

        db = 'mysql'            # needed for this test
        args = dict(config.items('mysql'))
        args['database'] = db
        dbh = get_mysql(**args)
        self.assertEqual(get_database(dbh), db)
        return dbh
Пример #5
0
    def getopts(opts_ini=None):
        import argparse
        parser = argparse.ArgumentParser()

        if opts_ini:
            opts_config = get_config(opts_ini)
        parser.add_argument('-v', action='store_true', help='verbose mode')
        parser.add_argument('-q', action='store_true', help='silent mode')
        parser.add_argument('-d', action='store_true', help='debugging flag')

        opts = parser.parse_args()
        if opts.debug:
            os.environ['DEBUG'] = 'True'
            print(ppjson(vars(opts)))

        return opts