summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-04-27 04:14:54 +0300
committerDevaev Maxim <[email protected]>2019-04-27 04:14:54 +0300
commit3476f52da9728995c978394748cec812d554ec2f (patch)
tree93ed629385b536ebb0e5bb0f984ea5085d0050a5
parentc40f13e2d79c2ed23502e3c49f9b1e8c7d23f1c2 (diff)
apps: init only required sections
-rw-r--r--kvmd/apps/__init__.py26
-rw-r--r--kvmd/apps/cleanup/__init__.py8
-rw-r--r--kvmd/apps/htpasswd/__init__.py6
-rw-r--r--kvmd/apps/kvmd/__init__.py9
4 files changed, 38 insertions, 11 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index e95d19b9..65d30f2e 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -74,6 +74,7 @@ def init(
prog: Optional[str]=None,
description: Optional[str]=None,
add_help: bool=True,
+ sections: Optional[List[str]]=None,
argv: Optional[List[str]]=None,
) -> Tuple[argparse.ArgumentParser, List[str], Section]:
@@ -89,7 +90,7 @@ def init(
help="View current configuration (include all overrides)")
(options, remaining) = args_parser.parse_known_args(argv)
- config = _init_config(options.config_path, options.set_options)
+ config = _init_config(options.config_path, (sections or []), options.set_options)
if options.dump_config:
_dump_config(config)
raise SystemExit()
@@ -100,13 +101,13 @@ def init(
# =====
-def _init_config(config_path: str, options: List[str]) -> Section:
+def _init_config(config_path: str, sections: List[str], override_options: List[str]) -> Section:
config_path = os.path.expanduser(config_path)
raw_config: Dict = load_yaml_file(config_path)
- scheme = _get_config_scheme()
+ scheme = _get_config_scheme(sections)
try:
- _merge_dicts(raw_config, build_raw_from_options(options))
+ _merge_dicts(raw_config, build_raw_from_options(override_options))
config = make_config(raw_config, scheme)
scheme["kvmd"]["auth"]["internal"] = get_auth_service_class(config.kvmd.auth.internal_type).get_options()
@@ -138,8 +139,10 @@ def _merge_dicts(dest: Dict, src: Dict) -> None:
dest[key] = src[key]
-def _get_config_scheme() -> Dict:
- return {
+def _get_config_scheme(sections: List[str]) -> Dict:
+ scheme = {
+ "logging": Option({}),
+
"kvmd": {
"server": {
"host": Option("localhost", type=valid_ip_or_host),
@@ -230,6 +233,13 @@ def _get_config_scheme() -> Dict:
"cmd": Option(["/bin/true"], type=valid_command),
},
},
-
- "logging": Option({}),
}
+
+ if sections:
+ return {
+ section: sub
+ for (section, sub) in scheme.items()
+ if section in sections
+ }
+ else:
+ return scheme
diff --git a/kvmd/apps/cleanup/__init__.py b/kvmd/apps/cleanup/__init__.py
index bf5e4455..8c9af7fe 100644
--- a/kvmd/apps/cleanup/__init__.py
+++ b/kvmd/apps/cleanup/__init__.py
@@ -36,7 +36,13 @@ from .. import init
# =====
def main(argv: Optional[List[str]]=None) -> None:
- config = init("kvmd-cleanup", description="Kill KVMD and clear resources", argv=argv)[2].kvmd
+ config = init(
+ prog="kvmd-cleanup",
+ description="Kill KVMD and clear resources",
+ sections=["logging", "kvmd"],
+ argv=argv,
+ )[2].kvmd
+
logger = get_logger(0)
logger.info("Cleaning up ...")
diff --git a/kvmd/apps/htpasswd/__init__.py b/kvmd/apps/htpasswd/__init__.py
index a223fc16..c6f56087 100644
--- a/kvmd/apps/htpasswd/__init__.py
+++ b/kvmd/apps/htpasswd/__init__.py
@@ -98,7 +98,11 @@ def _cmd_delete(config: Section, options: argparse.Namespace) -> None:
# =====
def main(argv: Optional[List[str]]=None) -> None:
- (parent_parser, argv, config) = init(add_help=False, argv=argv)
+ (parent_parser, argv, config) = init(
+ add_help=False,
+ sections=["logging", "kvmd"],
+ argv=argv,
+ )
parser = argparse.ArgumentParser(
prog="kvmd-htpasswd",
description="Manage KVMD users (htpasswd auth only)",
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index caa8aa4b..49d7820f 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -41,7 +41,13 @@ from .server import Server
# =====
def main(argv: Optional[List[str]]=None) -> None:
- config = init("kvmd", description="The main Pi-KVM daemon", argv=argv)[2].kvmd
+ config = init(
+ prog="kvmd",
+ description="The main Pi-KVM daemon",
+ sections=["logging", "kvmd"],
+ argv=argv,
+ )[2].kvmd
+
with gpio.bcm():
# pylint: disable=protected-access
Server(
@@ -60,4 +66,5 @@ def main(argv: Optional[List[str]]=None) -> None:
msd=MassStorageDevice(**config.msd._unpack()),
streamer=Streamer(**config.streamer._unpack()),
).run(**config.server._unpack())
+
get_logger().info("Bye-bye")