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: