From 93c7b254a326899d6c45b5ba4de2c325cddbaa8c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 15 May 2024 23:12:49 -0500 Subject: [PATCH] Migrate the `crepes` commands to use typer --- rattail_corepos/corepos/commands.py | 133 ++++++++++------------------ setup.cfg | 18 +--- 2 files changed, 47 insertions(+), 104 deletions(-) diff --git a/rattail_corepos/corepos/commands.py b/rattail_corepos/corepos/commands.py index 7546760..efafb6f 100644 --- a/rattail_corepos/corepos/commands.py +++ b/rattail_corepos/corepos/commands.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2023 Lance Edgar +# Copyright © 2010-2024 Lance Edgar # # This file is part of Rattail. # @@ -24,103 +24,62 @@ CORE-POS commands """ -import sys -import warnings +import typer +from typing_extensions import Annotated -from rattail import commands -from rattail_corepos import __version__ -from rattail_corepos.corepos.office.commands import ExportCSV, ImportCSV +from rattail.commands.typer import make_typer, importer_command, typer_get_runas_user +from rattail.commands.importing import ImportCommandHandler -def main(*args): - """ - Primary entry point for Crepes command system - """ - if args: - args = list(args) - else: - args = sys.argv[1:] - cmd = Command() - cmd.run(*args) +crepes_typer = make_typer( + name='crepes', + help="Crepes -- command line interface for CORE-POS" +) -class Command(commands.Command): - """ - Primary command for Crepes (CORE-POS) - """ - name = 'crepes' - version = __version__ - description = "Crepes -- command line interface for CORE-POS" - long_description = "" - - -class ImportToCore(commands.ImportSubcommand): - """ - Generic base class for commands which import *to* a CORE DB. - """ - - -class ExportCore(commands.ImportSubcommand): +@crepes_typer.command() +@importer_command +def export_core( + ctx: typer.Context, + dbkey: Annotated[ + str, + typer.Option(help="Config key for database engine to be used as the \"target\" " + "CORE DB, i.e. where data will be exported. This key must be " + "defined in the [rattail_corepos.db] section of your config file.")] = 'host', + **kwargs +): """ Export data to another CORE database """ - name = 'export-core' - description = __doc__.strip() - handler_key = 'to_corepos_db_office_op.from_corepos_db_office_op.export' - default_dbkey = 'host' - - def add_parser_args(self, parser): - super(ExportCore, self).add_parser_args(parser) - parser.add_argument('--dbkey', metavar='KEY', default=self.default_dbkey, - help="Config key for database engine to be used as the \"target\" " - "CORE DB, i.e. where data will be exported. This key must be " - "defined in the [rattail_corepos.db] section of your config file.") - - def get_handler_kwargs(self, **kwargs): - if 'args' in kwargs: - kwargs['dbkey'] = kwargs['args'].dbkey - return kwargs + config = ctx.parent.rattail_config + progress = ctx.parent.rattail_progress + handler = ImportCommandHandler( + config, import_handler_key='to_corepos_db_office_op.from_corepos_db_office_op.export') + kwargs['user'] = typer_get_runas_user(ctx) + kwargs['handler_kwargs'] = {'dbkey': dbkey} + handler.run(kwargs, progress=progress) -class LegacyExportCSV(ExportCSV): - - def __init__(self, *args, **kwargs): - warnings.warn("the `crepes export-csv` command is deprecated; " - "please use `core-office export-csv` instead", - DeprecationWarning, stacklevel=2) - super().__init__(*args, **kwargs) - - -class ImportCore(ImportToCore): +@crepes_typer.command() +@importer_command +def import_core( + ctx: typer.Context, + dbkey: Annotated[ + str, + typer.Option(help="Config key for database engine to be used as the CORE " + "\"host\", i.e. the source of the data to be imported. This key " + "must be defined in the [rattail_corepos.db] section of your " + "config file.")] = 'host', + **kwargs +): """ Import data from another CORE database """ - name = 'import-core' - description = __doc__.strip() - handler_key = 'to_corepos_db_office_op.from_corepos_db_office_op.import' - accepts_dbkey_param = True - - def add_parser_args(self, parser): - super(ImportCore, self).add_parser_args(parser) - if self.accepts_dbkey_param: - parser.add_argument('--dbkey', metavar='KEY', default='host', - help="Config key for database engine to be used as the CORE " - "\"host\", i.e. the source of the data to be imported. This key " - "must be defined in the [rattail_corepos.db] section of your config file. " - "Defaults to 'host'.") - - def get_handler_kwargs(self, **kwargs): - if self.accepts_dbkey_param: - if 'args' in kwargs: - kwargs['dbkey'] = kwargs['args'].dbkey - return kwargs - - -class LegacyImportCSV(ImportCSV): - - def __init__(self, *args, **kwargs): - warnings.warn("the `crepes import-csv` command is deprecated; " - "please use `core-office import-csv` instead", - DeprecationWarning, stacklevel=2) - super().__init__(*args, **kwargs) + config = ctx.parent.rattail_config + progress = ctx.parent.rattail_progress + handler = ImportCommandHandler( + config, import_handler_key='to_corepos_db_office_op.from_corepos_db_office_op.import') + kwargs['user'] = typer_get_runas_user(ctx) + kwargs['handler_kwargs'] = {'dbkey': dbkey} + handler.run(kwargs, progress=progress) diff --git a/setup.cfg b/setup.cfg index 6b86b80..1f5779e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,25 +37,9 @@ zip_safe = False [options.entry_points] console_scripts = - crepes = rattail_corepos.corepos.commands:main + crepes = rattail_corepos.corepos.commands:crepes_typer core-office = rattail_corepos.corepos.office.commands:core_office_typer -core_office.subcommands = - anonymize = rattail_corepos.corepos.office.commands:Anonymize - export-csv = rattail_corepos.corepos.office.commands:ExportCSV - export-lane-op = rattail_corepos.corepos.office.commands:ExportLaneOp - import-csv = rattail_corepos.corepos.office.commands:ImportCSV - import-self = rattail_corepos.corepos.office.commands:ImportSelf - get-config-value = rattail_corepos.corepos.office.commands:GetConfigValue - patch-customer-gaps = rattail_corepos.corepos.office.commands:PatchCustomerGaps - ping-install = rattail_corepos.corepos.office.commands:PingInstall - -crepes.subcommands = - export-core = rattail_corepos.corepos.commands:ExportCore - export-csv = rattail_corepos.corepos.commands:LegacyExportCSV - import-core = rattail_corepos.corepos.commands:ImportCore - import-csv = rattail_corepos.corepos.commands:LegacyImportCSV - rattail.config.extensions = rattail-corepos = rattail_corepos.config:RattailCOREPOSExtension