def test_load_save_config_file(self): # setup settings adjustable_settings = { 'data_dir': (str, conf.default_data_dir), 'lbryum_servers': (list, []) } env = conf.Env(**adjustable_settings) settings = conf.Config({}, adjustable_settings, environment=env) conf.settings = settings # setup tempfile conf_entry = b"lbryum_servers: ['localhost:50001', 'localhost:50002']\n" with tempfile.NamedTemporaryFile(suffix='.yml') as conf_file: conf_file.write(conf_entry) conf_file.seek(0) conf.conf_file = conf_file.name # load and save settings from conf file settings.load_conf_file_settings() settings.save_conf_file_settings() # test if overwritten entry equals original entry # use decoded versions, because format might change without # changing the interpretation decoder = conf.settings_decoders['.yml'] conf_decoded = decoder(conf_entry) conf_entry_new = conf_file.read() conf_decoded_new = decoder(conf_entry_new) self.assertEqual(conf_decoded, conf_decoded_new)
def test_data_dir(self): # check if these directories are returned as string and not unicode # otherwise there will be problems when calling os.path.join on # unicode directory names with string file names settings = conf.Config({}, {}) self.assertEqual(str, type(settings.download_dir)) self.assertEqual(str, type(settings.data_dir)) self.assertEqual(str, type(settings.wallet_dir))
def mock_conf_settings(obj, settings={}): original_settings = conf.settings conf.settings = conf.Config(conf.FIXED_SETTINGS, conf.ADJUSTABLE_SETTINGS) conf.settings.update(settings) def _reset_settings(): conf.settings = original_settings obj.addCleanup(_reset_settings)
def mock_conf_settings(obj, settings={}): original_settings = conf.settings conf.settings = conf.Config(conf.FIXED_SETTINGS, conf.ADJUSTABLE_SETTINGS) conf.settings.installation_id = conf.settings.get_installation_id() conf.settings.node_id = conf.settings.get_node_id() conf.settings.update(settings) def _reset_settings(): conf.settings = original_settings obj.addCleanup(_reset_settings)
def init_conf_windows(settings={}): """ There is no fork on windows, so imports are freshly initialized in new processes. So conf needs to be intialized for new processes """ if os.name == 'nt': original_settings = conf.settings conf.settings = conf.Config(conf.FIXED_SETTINGS, conf.ADJUSTABLE_SETTINGS) conf.settings.installation_id = conf.settings.get_installation_id() conf.settings.update(settings)
def get_mock_config_instance(self): settings = {'test': (str, '')} env = conf.Env(**settings) self.tmp_dir = tempfile.mkdtemp() self.addCleanup(lambda: defer.succeed(shutil.rmtree(self.tmp_dir))) return conf.Config({}, settings, environment=env, data_dir=self.tmp_dir, wallet_dir=self.tmp_dir, download_dir=self.tmp_dir)
def mock_conf_settings(obj, settings={}): settings.setdefault('download_mirrors', []) conf.initialize_settings(False) original_settings = conf.settings conf.settings = conf.Config(conf.FIXED_SETTINGS, conf.ADJUSTABLE_SETTINGS) conf.settings.installation_id = conf.settings.get_installation_id() conf.settings.node_id = conf.settings.get_node_id() conf.settings.update(settings) def _reset_settings(): conf.settings = original_settings obj.addCleanup(_reset_settings)
def test_max_key_fee_set(self): fixed_default = {'CURRENCIES':{'BTC':{'type':'crypto'}}} adjustable_settings = {'max_key_fee': (json.loads, {'currency':'USD', 'amount':1})} env = conf.Env(**adjustable_settings) settings = conf.Config(fixed_default, adjustable_settings, environment=env) with self.assertRaises(InvalidCurrencyError): settings.set('max_key_fee', {'currency':'USD', 'amount':1}) valid_setting= {'currency':'BTC', 'amount':1} settings.set('max_key_fee', valid_setting ) out = settings.get('max_key_fee') self.assertEqual(out, valid_setting)
def mock_conf_settings(obj, settings={}): conf.settings = None settings.setdefault('download_mirrors', []) conf.initialize_settings(False) original_settings = conf.settings conf.settings = conf.Config(conf.FIXED_SETTINGS, conf.ADJUSTABLE_SETTINGS) conf.settings['data_dir'] = settings.get('data_dir') or conf.settings.data_dir \ or conf.settings.default_data_dir conf.settings['download_directory'] = settings.get('download_directory') or conf.settings.download_dir \ or conf.settings.default_download_dir conf.settings['wallet_dir'] = settings.get('wallet_dir') or conf.settings.wallet_dir or \ conf.settings.default_wallet_dir conf.settings.installation_id = conf.settings.get_installation_id() conf.settings.node_id = conf.settings.get_node_id() conf.settings.update(settings) def _reset_settings(): conf.settings = original_settings obj.addCleanup(_reset_settings)
def get_mock_config_instance(): settings = {'test': (str, '')} env = conf.Env(**settings) return conf.Config({}, settings, environment=env)