fix: allow passing just key to ImportCommandHandler
so config can override designated handler, and command still work
This commit is contained in:
parent
19574ea4a0
commit
7e3e892002
4 changed files with 31 additions and 12 deletions
|
|
@ -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.
|
||||
|
||||
Typical usage for custom commands will be to provide the spec::
|
||||
|
||||
handler = ImportCommandHandler(
|
||||
config, import_handler='wuttasync.importing.csv:FromCsvToWutta')
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue