Merge pull request #2947 from thebubbleindex/patch-1

properly handle integer environment values, fixes issue with specifying ports via environment vars
This commit is contained in:
Lex Berezhny 2020-05-18 11:11:10 -04:00 committed by GitHub
commit 46ef6c8ab7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View file

@ -277,14 +277,23 @@ class Strings(ListSetting):
class EnvironmentAccess: class EnvironmentAccess:
PREFIX = 'LBRY_' PREFIX = 'LBRY_'
def __init__(self, environ: dict): def __init__(self, config: 'BaseConfig', environ: dict):
self.environ = environ self.configuration = config
self.data = {}
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.data[setting.name] = setting.deserialize(value)
def __contains__(self, item: str): def __contains__(self, item: str):
return f'{self.PREFIX}{item.upper()}' in self.environ return item in self.data
def __getitem__(self, item: str): def __getitem__(self, item: str):
return self.environ[f'{self.PREFIX}{item.upper()}'] return self.data[item]
class ArgumentAccess: class ArgumentAccess:
@ -443,7 +452,7 @@ class BaseConfig:
self.arguments = ArgumentAccess(self, args) self.arguments = ArgumentAccess(self, args)
def set_environment(self, environ=None): def set_environment(self, environ=None):
self.environment = EnvironmentAccess(environ or os.environ) self.environment = EnvironmentAccess(self, environ or os.environ)
def set_persisted(self, config_file_path=None): def set_persisted(self, config_file_path=None):
if config_file_path is None: if config_file_path is None:

View file

@ -90,10 +90,15 @@ class ConfigurationTests(unittest.TestCase):
def test_environment(self): def test_environment(self):
c = TestConfig() c = TestConfig()
self.assertEqual(c.test_str, 'the default') self.assertEqual(c.test_str, 'the default')
c.set_environment({'LBRY_TEST_STR': 'from environ'}) c.set_environment({'LBRY_TEST_STR': 'from environ'})
self.assertEqual(c.test_str, 'from environ') self.assertEqual(c.test_str, 'from environ')
self.assertEqual(c.test_int, 9)
c.set_environment({'LBRY_TEST_INT': '1'})
self.assertEqual(c.test_int, 1)
def test_persisted(self): def test_persisted(self):
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir: