def _config_to_configparser(self, config): """ Return a ConfigParser instance representing a given config dictionary. Args: config (dict): Dictionary of config key/value pairs. Returns: SafeConfigParser: SafeConfigParser instance representing config. """ def get_store(): return config['store'] def get_day_start(): return config['day_start'].strftime('%H:%M:%S') def get_fact_min_delta(): return text_type(config['fact_min_delta']) def get_tmpfile_path(): return text_type(config['tmpfile_path']) def get_db_engine(): return config['db_engine'] def get_db_path(): return text_type(config['db_path']) cp_instance = SafeConfigParser() cp_instance.add_section('Backend') cp_instance.set('Backend', 'store', get_store()) cp_instance.set('Backend', 'day_start', get_day_start()) cp_instance.set('Backend', 'fact_min_delta', get_fact_min_delta()) cp_instance.set('Backend', 'tmpfile_path', get_tmpfile_path()) cp_instance.set('Backend', 'db_engine', get_db_engine()) cp_instance.set('Backend', 'db_path', get_db_path()) return cp_instance
def configparser_instance(request): """Provide a ``ConfigParser`` instance and its expected config dict.""" config = SafeConfigParser() config.add_section('Backend') config.set('Backend', 'store', 'sqlalchemy') config.set('Backend', 'day_start', '05:00:00') config.set('Backend', 'fact_min_delta', '60') config.set('Backend', 'tmpfile_path', '/tmp') config.set('Backend', 'db_engine', 'sqlite') config.set('Backend', 'db_path', '/tmp/hamster.db') config.set('Backend', 'db_host', 'www.example.com') config.set('Backend', 'db_port', '22') config.set('Backend', 'db_name', 'hamster') config.set('Backend', 'db_user', 'hamster') config.set('Backend', 'db_password', 'hamster') expectation = { 'store': text_type('sqlalchemy'), 'day_start': datetime.datetime.strptime('05:00:00', '%H:%M:%S').time(), 'fact_min_delta': 60, 'tmpfile_path': text_type('/tmp'), 'db_engine': text_type('sqlite'), 'db_path': text_type('/tmp/hamster.db'), 'db_host': text_type('www.example.com'), 'db_port': 22, 'db_name': text_type('hamster'), 'db_user': text_type('hamster'), 'db_password': text_type('hamster'), } return config, expectation
def generate_config(**kwargs): config = SafeConfigParser() # Backend config.add_section('Backend') config.set('Backend', 'store', kwargs.get('store', 'sqlalchemy')) config.set('Backend', 'daystart', kwargs.get('daystart', '00:00:00')) config.set('Backend', 'fact_min_delta', kwargs.get('fact_min_delta', '60')) config.set('Backend', 'db_engine', kwargs.get('db_engine', 'sqlite')) config.set('Backend', 'db_path', kwargs.get('db_path', os.path.join( tmpdir.strpath, 'hamster_db.sqlite'))) config.set('Backend', 'db_host', kwargs.get('db_host', '')) config.set('Backend', 'db_name', kwargs.get('db_name', '')) config.set('Backend', 'db_port', kwargs.get('db_port', '')) config.set('Backend', 'db_user', kwargs.get('db_user', '')), config.set('Backend', 'db_password', kwargs.get('db_password', '')) # Client config.add_section('Client') config.set('Client', 'unsorted_localized', kwargs.get( 'unsorted_localized', 'Unsorted')) config.set('Client', 'log_level', kwargs.get('log_level', 'debug')) config.set('Client', 'log_console', kwargs.get('log_console', '0')) config.set('Client', 'log_filename', kwargs.get('log_filename', faker.file_name())) return config
def _write_config_file(file_path): """ Write a default config file to the specified location. Returns: SafeConfigParser: Instace written to file. """ # [FIXME] # This may be usefull to turn into a proper command, so users can restore to # factory settings easily. def get_db_path(): return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.sqlite') def get_tmp_file_path(): return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.fact') config = SafeConfigParser() # Backend config.add_section('Backend') config.set('Backend', 'store', 'sqlalchemy') config.set('Backend', 'daystart', '00:00:00') config.set('Backend', 'fact_min_delta', '60') config.set('Backend', 'db_engine', 'sqlite') config.set('Backend', 'db_host', '') config.set('Backend', 'db_port', '') config.set('Backend', 'db_name', '') config.set('Backend', 'db_path', get_db_path()) config.set('Backend', 'db_user', '') config.set('Backend', 'db_password', '') # Client config.add_section('Client') config.set('Client', 'unsorted_localized', 'Unsorted') config.set('Client', 'log_level', 'debug') config.set('Client', 'log_console', 'False') config.set('Client', 'log_filename', 'hamster_cli.log') configfile_path = os.path.dirname(file_path) if not os.path.lexists(configfile_path): os.makedirs(configfile_path) with open(file_path, 'w') as fobj: config.write(fobj) return config