From 7e3e8920026bac455cbc836d797ed3d59e6b0c2c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 20 Dec 2025 16:33:06 -0600 Subject: [PATCH] fix: allow passing just `key` to ImportCommandHandler so config can override designated handler, and command still work --- src/wuttasync/cli/base.py | 31 ++++++++++++++++++++++------ src/wuttasync/cli/import_csv.py | 4 +--- src/wuttasync/cli/import_versions.py | 4 +--- tests/cli/test_base.py | 4 ++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/wuttasync/cli/base.py b/src/wuttasync/cli/base.py index a2460d5..e972e18 100644 --- a/src/wuttasync/cli/base.py +++ b/src/wuttasync/cli/base.py @@ -50,19 +50,35 @@ class ImportCommandHandler(GenericHandler): create this handler and call its :meth:`run()` method. This handler does not know how to import/export data, but it knows - how to make its :attr:`import_handler` do it. + how to make its :attr:`import_handler` do it. Likewise, the + import handler is not "CLI-aware" - so this provides the glue. :param import_handler: During construction, caller can specify the :attr:`import_handler` as any of: * import handler instance * import handler factory (e.g. class) - * import handler spec (cf. :func:`~wuttjamaican:wuttjamaican.util.load_object()`) + * import handler :term:`spec` - For example:: + :param key: Optional :term:`import/export key` to use for handler + lookup. Only used if ``import_handler`` param is not set. - handler = ImportCommandHandler( - config, import_handler='wuttasync.importing.csv:FromCsvToWutta') + Typical usage for custom commands will be to provide the spec:: + + handler = ImportCommandHandler( + config, "poser.importing.foo:FromFooToPoser" + ) + + Library authors may prefer to use the import/export key; this lets + the command work with any designated handler:: + + handler = ImportCommandHandler( + config, key="import.to_poser.from_foo" + ) + + See also + :meth:`~wuttasync.app.WuttaSyncAppProvider.get_import_handler()` + which does the lookup by key. """ import_handler = None @@ -71,7 +87,7 @@ class ImportCommandHandler(GenericHandler): invoked when command runs. See also :meth:`run()`. """ - def __init__(self, config, import_handler=None): + def __init__(self, config, import_handler=None, key=None): super().__init__(config) if import_handler: @@ -83,6 +99,9 @@ class ImportCommandHandler(GenericHandler): factory = self.app.load_object(import_handler) self.import_handler = factory(self.config) + elif key: + self.import_handler = self.app.get_import_handler(key, require=True) + def run(self, params, progress=None): # pylint: disable=unused-argument """ Run the import/export job(s) based on command line params. diff --git a/src/wuttasync/cli/import_csv.py b/src/wuttasync/cli/import_csv.py index d3c8047..4c5694a 100644 --- a/src/wuttasync/cli/import_csv.py +++ b/src/wuttasync/cli/import_csv.py @@ -38,7 +38,5 @@ def import_csv(ctx: typer.Context, **kwargs): # pylint: disable=unused-argument Import data from CSV file(s) to Wutta DB """ config = ctx.parent.wutta_config - handler = ImportCommandHandler( - config, import_handler="wuttasync.importing.csv:FromCsvToWutta" - ) + handler = ImportCommandHandler(config, key="import.to_wutta.from_csv") handler.run(ctx.params) diff --git a/src/wuttasync/cli/import_versions.py b/src/wuttasync/cli/import_versions.py index 86da4c4..aa82088 100644 --- a/src/wuttasync/cli/import_versions.py +++ b/src/wuttasync/cli/import_versions.py @@ -69,7 +69,5 @@ def import_versions( # pylint: disable=unused-argument ) sys.exit(1) - handler = ImportCommandHandler( - config, import_handler="wuttasync.importing.versions:FromWuttaToVersions" - ) + handler = ImportCommandHandler(config, key="import.to_versions.from_wutta") handler.run(ctx.params) diff --git a/tests/cli/test_base.py b/tests/cli/test_base.py index 991358e..b8fc954 100644 --- a/tests/cli/test_base.py +++ b/tests/cli/test_base.py @@ -34,6 +34,10 @@ class TestImportCommandHandler(DataTestCase): handler = self.make_handler(import_handler=myhandler) self.assertIs(handler.import_handler, myhandler) + # as key + handler = self.make_handler(key="import.to_wutta.from_csv") + self.assertIsInstance(handler.import_handler, FromCsvToWutta) + def test_run(self): handler = self.make_handler( import_handler="wuttasync.importing.csv:FromCsvToWutta"