Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] config command #19

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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