def main() -> NoReturn: arg_parser = argparse.ArgumentParser( description=sys.modules[__name__].__doc__) arg_parser.add_argument("config_file", type=argparse.FileType("r"), help="path to a configuration file") arg_parser.add_argument("--debug", default=False, action="store_true", help="enable debug logging") args = arg_parser.parse_args() if args.debug: level = logging.DEBUG else: level = logging.INFO logging.basicConfig(level=level, format="%(message)s") # quiet kazoo's verbose logs a bit logging.getLogger("kazoo").setLevel(logging.WARNING) parser = configparser.RawConfigParser( interpolation=EnvironmentInterpolation()) parser.read_file(args.config_file) watcher_config = dict(parser.items("live-data")) cfg = config.parse_config( watcher_config, { "nodes": config.DictOf( { "source": config.String, "dest": config.String, "owner": config.Optional(config.UnixUser), "group": config.Optional(config.UnixGroup), "mode": config.Optional(config.Integer(base=8), default=0o400), # type: ignore }) }, ) # pylint: disable=maybe-no-member nodes = cfg.nodes.values() secrets = secrets_store_from_config(watcher_config, timeout=30) zookeeper = zookeeper_client_from_config(secrets, watcher_config, read_only=True) zookeeper.start() try: watch_zookeeper_nodes(zookeeper, nodes) finally: zookeeper.stop()
def from_config(cls, app_config: config.RawConfig) -> "TimeoutBaseplateObserver": cfg = config.parse_config( app_config, { "server_timeout": { "default": config.TimespanOrInfinite, "debug": config.Optional(config.Boolean, default=False), "by_endpoint": config.DictOf(config.TimespanOrInfinite), } }, ) return cls(cfg.server_timeout)
def test_root_level(self): parser = config.DictOf({"a": config.Integer, "b": config.String}) result = parser.parse( "", { "first.a": "1", "first.b": "test", "first.c": "ignored", "second.a": "2", "second.b": "test", }, ) self.assertEqual(result, {"first": {"a": 1, "b": "test"}, "second": {"a": 2, "b": "test"}})
def test_vector_children(self): parser = config.DictOf({"a": config.Integer, "b": config.String}) result = parser.parse( "my_key", { "my_key.first.a": "1", "my_key.first.b": "test", "my_key.first.c": "ignored", "my_key.second.a": "2", "my_key.second.b": "test", }, ) self.assertEqual(result, {"first": {"a": 1, "b": "test"}, "second": {"a": 2, "b": "test"}})
def from_config( cls, app_config: config.RawConfig) -> "TimeoutBaseplateObserver": cfg = config.parse_config( app_config, { "server_timeout": { "default": config.Optional(config.TimespanOrInfinite, default=None), "debug": config.Optional(config.Boolean, default=False), "by_endpoint": config.DictOf(config.TimespanOrInfinite), } }, ) if cfg.server_timeout.default is None: warn_deprecated( "No server_timeout.default configured. Defaulting to no timeout. " "Set the default timeout to 'infinite' or a timespan like '2 seconds'. " "This will become mandatory in Baseplate.py 2.0.") cfg.server_timeout.default = config.InfiniteTimespan return cls(cfg.server_timeout)
def test_subparsers(self): result = config.parse_config(self.config, {"foo": config.DictOf(config.String)}) self.assertEqual(result, {"foo": {"bar": "33", "baz": "a cool guy"}})
def test_scalar_children(self): parser = config.DictOf(config.Integer) result = parser.parse("my_key", {"my_key.a": "1", "my_key.b": "2"}) self.assertEqual(result, {"a": 1, "b": 2})
def test_empty(self): parser = config.DictOf(config.Integer) result = parser.parse("my_key", {"not_related": "a"}) self.assertEqual(result, {})