fixes for toggle config, passing servers as list, etc
This commit is contained in:
parent
d0526ad7d5
commit
4a35247016
4 changed files with 75 additions and 22 deletions
|
@ -196,14 +196,23 @@ class EnvironmentAccess:
|
|||
|
||||
class ArgumentAccess:
|
||||
|
||||
def __init__(self, args: dict):
|
||||
self.args = args
|
||||
def __init__(self, config: 'BaseConfig', args: dict):
|
||||
self.configuration = config
|
||||
self.args = {}
|
||||
if args:
|
||||
self.load(args)
|
||||
|
||||
def load(self, args):
|
||||
for setting in self.configuration.get_settings():
|
||||
value = getattr(args, setting.name, NOT_SET)
|
||||
if value not in (None, NOT_SET):
|
||||
self.args[setting.name] = setting.deserialize(value)
|
||||
|
||||
def __contains__(self, item: str):
|
||||
return getattr(self.args, item, None) is not None
|
||||
return item in self.args
|
||||
|
||||
def __getitem__(self, item: str):
|
||||
return getattr(self.args, item)
|
||||
return self.args[item]
|
||||
|
||||
|
||||
class ConfigFileAccess:
|
||||
|
@ -337,17 +346,33 @@ class BaseConfig:
|
|||
parser.add_argument(
|
||||
f"--{setting.name.replace('_', '-')}",
|
||||
help=setting.doc,
|
||||
action="store_true"
|
||||
action="store_true",
|
||||
default=NOT_SET
|
||||
)
|
||||
parser.add_argument(
|
||||
f"--no-{setting.name.replace('_', '-')}",
|
||||
help=f"Opposite of --{setting.name.replace('_', '-')}",
|
||||
dest=setting.name,
|
||||
action="store_false",
|
||||
default=NOT_SET
|
||||
)
|
||||
elif isinstance(setting, Servers):
|
||||
parser.add_argument(
|
||||
f"--{setting.name.replace('_', '-')}",
|
||||
nargs="*",
|
||||
help=setting.doc,
|
||||
default=None
|
||||
)
|
||||
else:
|
||||
parser.add_argument(
|
||||
f"--{setting.name.replace('_', '-')}",
|
||||
help=setting.doc,
|
||||
metavar=setting.metavar
|
||||
metavar=setting.metavar,
|
||||
default=NOT_SET
|
||||
)
|
||||
|
||||
def set_arguments(self, args):
|
||||
self.arguments = ArgumentAccess(args)
|
||||
self.arguments = ArgumentAccess(self, args)
|
||||
|
||||
def set_environment(self, environ=None):
|
||||
self.environment = EnvironmentAccess(environ or os.environ)
|
||||
|
|
|
@ -252,8 +252,8 @@ def main(argv=None):
|
|||
|
||||
daemon = Daemon(conf)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(daemon.start())
|
||||
try:
|
||||
loop.run_until_complete(daemon.start())
|
||||
loop.run_forever()
|
||||
except (GracefulExit, KeyboardInterrupt):
|
||||
pass
|
||||
|
|
|
@ -258,12 +258,11 @@ class Daemon(metaclass=JSONRPCServerType):
|
|||
self.component_startup_task = None
|
||||
|
||||
logging.getLogger('aiohttp.access').setLevel(logging.WARN)
|
||||
self.app = web.Application()
|
||||
self.app.router.add_get('/lbryapi', self.handle_old_jsonrpc)
|
||||
self.app.router.add_post('/lbryapi', self.handle_old_jsonrpc)
|
||||
self.app.router.add_post('/', self.handle_old_jsonrpc)
|
||||
self.runner = web.AppRunner(self.app)
|
||||
self.site = None
|
||||
app = web.Application()
|
||||
app.router.add_get('/lbryapi', self.handle_old_jsonrpc)
|
||||
app.router.add_post('/lbryapi', self.handle_old_jsonrpc)
|
||||
app.router.add_post('/', self.handle_old_jsonrpc)
|
||||
self.runner = web.AppRunner(app)
|
||||
|
||||
@property
|
||||
def dht_node(self) -> typing.Optional['Node']:
|
||||
|
@ -378,11 +377,11 @@ class Daemon(metaclass=JSONRPCServerType):
|
|||
log.info("Platform: %s", json.dumps(system_info.get_platform(), indent=2))
|
||||
await self.analytics_manager.send_server_startup()
|
||||
await self.runner.setup()
|
||||
self.site = web.TCPSite(self.runner, self.conf.api_host, self.conf.api_port)
|
||||
|
||||
try:
|
||||
await self.site.start()
|
||||
log.info('lbrynet API listening on TCP %s:%i', *self.site._server.sockets[0].getsockname()[:2])
|
||||
site = web.TCPSite(self.runner, self.conf.api_host, self.conf.api_port)
|
||||
await site.start()
|
||||
log.info('lbrynet API listening on TCP %s:%i', *site._server.sockets[0].getsockname()[:2])
|
||||
except OSError as e:
|
||||
log.error('lbrynet API failed to bind TCP %s for listening. Daemon is already running or this port is '
|
||||
'already in use by another application.', self.conf.api)
|
||||
|
|
|
@ -11,7 +11,8 @@ from lbrynet.error import InvalidCurrencyError
|
|||
class TestConfig(BaseConfig):
|
||||
test_str = String('str help', 'the default', previous_names=['old_str'])
|
||||
test_int = Integer('int help', 9)
|
||||
test_toggle = Toggle('toggle help', False)
|
||||
test_false_toggle = Toggle('toggle help', False)
|
||||
test_true_toggle = Toggle('toggle help', True)
|
||||
servers = Servers('servers help', [('localhost', 80)])
|
||||
|
||||
|
||||
|
@ -45,12 +46,39 @@ class ConfigurationTests(unittest.TestCase):
|
|||
|
||||
def test_arguments(self):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--test-str")
|
||||
TestConfig.contribute_args(parser)
|
||||
|
||||
args = parser.parse_args([])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertEqual(c.test_str, 'the default')
|
||||
self.assertTrue(c.test_true_toggle)
|
||||
self.assertFalse(c.test_false_toggle)
|
||||
self.assertEqual(c.servers, [('localhost', 80)])
|
||||
|
||||
args = parser.parse_args(['--test-str', 'blah'])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertEqual(c.test_str, 'blah')
|
||||
c.arguments = {}
|
||||
self.assertEqual(c.test_str, 'the default')
|
||||
self.assertTrue(c.test_true_toggle)
|
||||
self.assertFalse(c.test_false_toggle)
|
||||
|
||||
args = parser.parse_args(['--test-true-toggle'])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertTrue(c.test_true_toggle)
|
||||
self.assertFalse(c.test_false_toggle)
|
||||
|
||||
args = parser.parse_args(['--test-false-toggle'])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertTrue(c.test_true_toggle)
|
||||
self.assertTrue(c.test_false_toggle)
|
||||
|
||||
args = parser.parse_args(['--no-test-true-toggle'])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertFalse(c.test_true_toggle)
|
||||
self.assertFalse(c.test_false_toggle)
|
||||
|
||||
args = parser.parse_args(['--servers', 'localhost:1', '192.168.0.1:2'])
|
||||
c = TestConfig.create_from_arguments(args)
|
||||
self.assertEqual(c.servers, [('localhost', 1), ('192.168.0.1', 2)])
|
||||
|
||||
def test_environment(self):
|
||||
c = TestConfig()
|
||||
|
@ -127,7 +155,8 @@ class ConfigurationTests(unittest.TestCase):
|
|||
with self.assertRaisesRegex(AssertionError, 'must be an integer'):
|
||||
c.test_int = 'hi'
|
||||
with self.assertRaisesRegex(AssertionError, 'must be a true/false'):
|
||||
c.test_toggle = 'hi'
|
||||
c.test_true_toggle = 'hi'
|
||||
c.test_false_toggle = 'hi'
|
||||
|
||||
def test_file_extension_validation(self):
|
||||
with self.assertRaisesRegex(AssertionError, "'.json' is not supported"):
|
||||
|
|
Loading…
Reference in a new issue