Skip to content

Commit

Permalink
Merge pull request #19 from initOS/config-modules
Browse files Browse the repository at this point in the history
[IMP] config command
  • Loading branch information
ortlam committed Oct 5, 2023
2 parents 297c019 + a6bc7ed commit 050b6cb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/doblib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,19 @@ def main(args=None):
args, left = load_arguments(args)

log_level = LOG_LEVELS.get(args.logging, logging.INFO)
if args.command in ("c", "config"):
# Show the configuration of the environment (skip all non-ERROR logging)
config_logger(logging.ERROR)
print(Environment(args.cfg).config(left))
return

if log_level:
config_logger(log_level)

if show_help:
left.append("--help")

if args.command in ("c", "config"):
# Show the configuration of the environment
print(Environment(args.cfg).config(left))
elif args.command in ("g", "generate"):
if args.command in ("g", "generate"):
# Regenerate the configuration file
sys.exit(Environment(args.cfg).generate_config())
elif args.command in ("f", "freeze"):
Expand Down
28 changes: 28 additions & 0 deletions src/doblib/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
def load_config_arguments(args):
parser = utils.default_parser("config")
parser.add_argument("option", nargs="?", help="Show only specific information")
parser.add_argument(
"--modules",
action="store_true",
help="Show the modules within the environment",
)
return parser.parse_known_args(args)


Expand Down Expand Up @@ -146,6 +151,26 @@ def _load_config(self, cfg, raise_if_missing=True):
# Merge the configurations
self._config = utils.merge(self._config, options, replace=["merges"])

def _get_modules(self):
"""Return the list of modules"""
modes = self.get(base.SECTION, "mode", default=[])
modes = set(modes.split(",") if isinstance(modes, str) else modes)

modules = set()
for module in self.get("modules", default=[]):
if isinstance(module, str):
modules.add(module)
elif isinstance(module, dict) and len(module) == 1:
mod, mode = list(module.items())[0]
if isinstance(mode, str) and mode in modes:
modules.add(mod)
elif isinstance(mode, list) and modes.intersection(mode):
modules.add(mod)
else:
raise TypeError("modules: must be str or dict of length 1")

return modules

def _link_modules(self):
"""Create symlinks to the modules to allow black-/whitelisting"""
shutil.rmtree(base.ADDON_PATH, True)
Expand Down Expand Up @@ -249,6 +274,9 @@ def config(self, args=None):
"""Simply output the rendered configuration file"""
args, _ = load_config_arguments(args or [])

if args.modules:
return "\n".join(sorted(self._get_modules()))

if args.option:
return yaml.dump(self.get(*args.option.split(":")))

Expand Down
20 changes: 0 additions & 20 deletions src/doblib/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,6 @@ def _run_migration_sql(self, db_name, script_name):
with closing(db.cursor()) as cr, open(script_name, "r") as f:
cr.execute(f.read())

def _get_modules(self):
"""Return the list of modules"""
modes = self.get(base.SECTION, "mode", default=[])
modes = set(modes.split(",") if isinstance(modes, str) else modes)

modules = set()
for module in self.get("modules", default=[]):
if isinstance(module, str):
modules.add(module)
elif isinstance(module, dict) and len(module) == 1:
mod, mode = list(module.items())[0]
if isinstance(mode, str) and mode in modes:
modules.add(mod)
elif isinstance(mode, list) and modes.intersection(mode):
modules.add(mod)
else:
raise TypeError("modules: must be str or dict of length 1")

return modules

def _get_installed_modules(self, db_name):
"""Return the list of modules which are installed"""
with self.env(db_name, False) as env:
Expand Down

0 comments on commit 050b6cb

Please sign in to comment.