From e49fcea6e3fac49d37e03d7fba2bfcef2d07c199 Mon Sep 17 00:00:00 2001 From: thebubbleindex <21062721+thebubbleindex@users.noreply.github.com> Date: Thu, 7 May 2020 08:59:03 -0700 Subject: [PATCH 1/2] fix issue with specifying ports via env vars make sure tcp and udp port for dht are int type --- lbry/conf.py | 15 ++++++++++++--- tests/unit/test_conf.py | 5 +++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lbry/conf.py b/lbry/conf.py index afd648193..b6c11af4d 100644 --- a/lbry/conf.py +++ b/lbry/conf.py @@ -277,8 +277,17 @@ class Strings(ListSetting): class EnvironmentAccess: PREFIX = 'LBRY_' - def __init__(self, environ: dict): - self.environ = environ + def __init__(self, config: 'BaseConfig', environ: dict): + self.configuration = config + self.environ = {} + if environ: + self.load(environ) + + def load(self, environ): + for setting in self.configuration.get_settings(): + value = environ.get(f'{self.PREFIX}{setting.name.upper()}', NOT_SET) + if value != NOT_SET and not (isinstance(setting, ListSetting) and value is None): + self.environ[f'{self.PREFIX}{setting.name.upper()}'] = setting.deserialize(value) def __contains__(self, item: str): return f'{self.PREFIX}{item.upper()}' in self.environ @@ -443,7 +452,7 @@ class BaseConfig: self.arguments = ArgumentAccess(self, args) def set_environment(self, environ=None): - self.environment = EnvironmentAccess(environ or os.environ) + self.environment = EnvironmentAccess(self, dict(environ or os.environ)) def set_persisted(self, config_file_path=None): if config_file_path is None: diff --git a/tests/unit/test_conf.py b/tests/unit/test_conf.py index cae6e6a90..138354604 100644 --- a/tests/unit/test_conf.py +++ b/tests/unit/test_conf.py @@ -93,6 +93,11 @@ class ConfigurationTests(unittest.TestCase): self.assertEqual(c.test_str, 'the default') c.set_environment({'LBRY_TEST_STR': 'from environ'}) self.assertEqual(c.test_str, 'from environ') + + c = TestConfig() + self.assertEqual(c.test_int, 9) + c.set_environment({'LBRY_TEST_INT': '1'}) + self.assertEqual(c.test_int, 1) def test_persisted(self): with tempfile.TemporaryDirectory() as temp_dir: From b09eabc478b3f626be7e419e6e3b41ef4a7d3a70 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 18 May 2020 08:51:06 -0400 Subject: [PATCH 2/2] minor simplifcation --- lbry/conf.py | 10 +++++----- tests/unit/test_conf.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lbry/conf.py b/lbry/conf.py index b6c11af4d..008742157 100644 --- a/lbry/conf.py +++ b/lbry/conf.py @@ -279,7 +279,7 @@ class EnvironmentAccess: def __init__(self, config: 'BaseConfig', environ: dict): self.configuration = config - self.environ = {} + self.data = {} if environ: self.load(environ) @@ -287,13 +287,13 @@ class EnvironmentAccess: for setting in self.configuration.get_settings(): value = environ.get(f'{self.PREFIX}{setting.name.upper()}', NOT_SET) if value != NOT_SET and not (isinstance(setting, ListSetting) and value is None): - self.environ[f'{self.PREFIX}{setting.name.upper()}'] = setting.deserialize(value) + self.data[setting.name] = setting.deserialize(value) def __contains__(self, item: str): - return f'{self.PREFIX}{item.upper()}' in self.environ + return item in self.data def __getitem__(self, item: str): - return self.environ[f'{self.PREFIX}{item.upper()}'] + return self.data[item] class ArgumentAccess: @@ -452,7 +452,7 @@ class BaseConfig: self.arguments = ArgumentAccess(self, args) def set_environment(self, environ=None): - self.environment = EnvironmentAccess(self, dict(environ or os.environ)) + self.environment = EnvironmentAccess(self, environ or os.environ) def set_persisted(self, config_file_path=None): if config_file_path is None: diff --git a/tests/unit/test_conf.py b/tests/unit/test_conf.py index 138354604..6abeef642 100644 --- a/tests/unit/test_conf.py +++ b/tests/unit/test_conf.py @@ -90,11 +90,11 @@ class ConfigurationTests(unittest.TestCase): def test_environment(self): c = TestConfig() + self.assertEqual(c.test_str, 'the default') c.set_environment({'LBRY_TEST_STR': 'from environ'}) self.assertEqual(c.test_str, 'from environ') - - c = TestConfig() + self.assertEqual(c.test_int, 9) c.set_environment({'LBRY_TEST_INT': '1'}) self.assertEqual(c.test_int, 1)