diff --git a/shepherd/agent/core.py b/shepherd/agent/core.py index 077d94a..c004bc7 100644 --- a/shepherd/agent/core.py +++ b/shepherd/agent/core.py @@ -42,6 +42,8 @@ def cli(ctx, default_config_path, local_operation, only_default_layer): in the current working directory beginning with "shepherd" and ending with ".toml" will be used. """ + ctx.ensure_object(SimpleNamespace) + version_text = pkg_resources.get_distribution("shepherd") log.info(F"Shepherd Agent [{version_text}]") @@ -56,8 +58,9 @@ def cli(ctx, default_config_path, local_operation, only_default_layer): with open(default_config_path, 'r+') as f: content = f.read() if "Compiled Shepherd config" in content: - log.warn("Default config file looks like it is full compiled config file" - " generated by Shepherd and picked up due to accidental name match") + log.warning("Default config file looks like it is full compiled config" + " file generated by Shepherd and picked up due to accidental" + " name match") else: log.error("No default config file provided, and no 'shepherd*.toml' could be" " found in the current directory") @@ -79,6 +82,11 @@ def cli(ctx, default_config_path, local_operation, only_default_layer): del plugin_configs["shepherd"] core_config = confman.get_config_bundle("shepherd") + if ctx.invoked_subcommand == "test": + ctx.obj.plugin_configs = plugin_configs + ctx.obj.core_config = core_config + return + # control.init_control(core_config, plugin_configs) scheduler.init_scheduler(core_config) @@ -98,12 +106,37 @@ def cli(ctx, default_config_path, local_operation, only_default_layer): @cli.command() -@click.option('-d', '--plugin-dir', type=click.Path(), - help="Override the configured directory to search for plugin modules, in addition to" - " built in Shepherd plugins and the global import path." - " Supplying the option empty will use the current directory.") -def test(): - print("test!") +@click.argument('plugin_name', required=False) +@click.argument('interface_function', required=False) +@click.pass_context +def test(ctx, plugin_name, interface_function): + if not plugin_name: + click.echo("") + click.echo(click.style("===", fg='white', bold=True) + + click.style('Shepherd Test', fg='blue', bold=True) + + click.style("===", fg='white', bold=True)) + click.secho(" Plugins loaded:", fg='green') + if len(ctx.obj.plugin_configs) == 0: + click.echo("---none---") + for plugin_name, config in ctx.obj.plugin_configs.items(): + click.secho(plugin_name, bold=True) + + click.secho("\n Loaded core config:", fg='green') + pprint(ctx.obj.core_config) + + click.secho("\n Loaded plugin configs:", fg='green') + if len(ctx.obj.plugin_configs) == 0: + click.echo("---none---") + for plugin_name, config in ctx.obj.plugin_configs.items(): + click.secho(plugin_name, bold=True) + pprint(config) + + click.echo("") + + log.info("Initialising plugins...") + plugin.init_plugins(ctx.obj.plugin_configs, ctx.obj.core_config) + + return @cli.command() @@ -197,8 +230,8 @@ def compile_config(confman, default_config_path, layers_disabled): log.info(F"Loaded default config layer from {default_config_path}") except Exception as e: if isinstance(e, InvalidConfigError): - log.error( - F"Failed to load default config from {default_config_path}. {chr(10).join(e.args)}") + log.error(F"Failed to load default config from {default_config_path}." + F" {chr(10).join(e.args)}") else: log.error(F"Failed to load default config from {default_config_path}", exc_info=True) sys.exit(1) @@ -216,7 +249,7 @@ def compile_config(confman, default_config_path, layers_disabled): confman.save_fallback() if not core_conf["plugin_dir"]: - log.warn("Custom plugin path is empty, won't load custom plugins") + log.warning("Custom plugin path is empty, won't load custom plugins") # ====Custom Local Config Layer==== # If this fails, maintain default config but continue on to Control layer @@ -295,9 +328,9 @@ def core_confspec(): ("custom_config_path", StringSpec(optional=True, helptext="Path to custom config" " layer TOML file.")), ("compiled_config_path", StringSpec(default="compiled-config.toml", optional=True, - helptext="Path to custom file Shepherd will generate" - " to show compiled config that was used and any" - " errors in validation.")) + helptext="Path to custom file Shepherd will generate" + " to show compiled config that was used and any" + " errors in validation.")) ]) confspec.add_spec("control_server", StringSpec()) @@ -317,16 +350,16 @@ def resolve_core_conf_paths(core_conf, relative_dir): os.chdir(relative_dir) core_conf["root_dir"] = str(Path(core_conf["root_dir"]).expanduser().resolve()) try: - os.chdir(core_conf["root_dir"]) + os.chdir(core_conf["root_dir"]) except FileNotFoundError: raise FileNotFoundError(F"Shepherd root operating directory '{core_conf['root_dir']}'" F" does not exist") if core_conf["plugin_dir"]: - core_conf["plugin_dir"] = str(Path(core_conf["plugin_dir"]).expanduser().resolve()) + core_conf["plugin_dir"] = str(Path(core_conf["plugin_dir"]).expanduser().resolve()) if core_conf["custom_config_path"]: - core_conf["custom_config_path"] = str( - Path(core_conf["custom_config_path"]).expanduser().resolve()) + core_conf["custom_config_path"] = str( + Path(core_conf["custom_config_path"]).expanduser().resolve()) if core_conf["compiled_config_path"]: core_conf["compiled_config_path"] = str( Path(core_conf["compiled_config_path"]).expanduser().resolve())