Migrate the crepes commands to use typer

This commit is contained in:
Lance Edgar 2024-05-15 23:12:49 -05:00
parent 2b0ca89fb8
commit 93c7b254a3
2 changed files with 47 additions and 104 deletions

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2023 Lance Edgar # Copyright © 2010-2024 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,103 +24,62 @@
CORE-POS commands CORE-POS commands
""" """
import sys import typer
import warnings from typing_extensions import Annotated
from rattail import commands from rattail.commands.typer import make_typer, importer_command, typer_get_runas_user
from rattail_corepos import __version__ from rattail.commands.importing import ImportCommandHandler
from rattail_corepos.corepos.office.commands import ExportCSV, ImportCSV
def main(*args):
"""
Primary entry point for Crepes command system
"""
if args:
args = list(args)
else:
args = sys.argv[1:]
cmd = Command() crepes_typer = make_typer(
cmd.run(*args) name='crepes',
help="Crepes -- command line interface for CORE-POS"
)
class Command(commands.Command): @crepes_typer.command()
""" @importer_command
Primary command for Crepes (CORE-POS) def export_core(
""" ctx: typer.Context,
name = 'crepes' dbkey: Annotated[
version = __version__ str,
description = "Crepes -- command line interface for CORE-POS" typer.Option(help="Config key for database engine to be used as the \"target\" "
long_description = "" "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
class ImportToCore(commands.ImportSubcommand): ):
"""
Generic base class for commands which import *to* a CORE DB.
"""
class ExportCore(commands.ImportSubcommand):
""" """
Export data to another CORE database Export data to another CORE database
""" """
name = 'export-core' config = ctx.parent.rattail_config
description = __doc__.strip() progress = ctx.parent.rattail_progress
handler_key = 'to_corepos_db_office_op.from_corepos_db_office_op.export' handler = ImportCommandHandler(
default_dbkey = 'host' config, import_handler_key='to_corepos_db_office_op.from_corepos_db_office_op.export')
kwargs['user'] = typer_get_runas_user(ctx)
def add_parser_args(self, parser): kwargs['handler_kwargs'] = {'dbkey': dbkey}
super(ExportCore, self).add_parser_args(parser) handler.run(kwargs, progress=progress)
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
class LegacyExportCSV(ExportCSV): @crepes_typer.command()
@importer_command
def __init__(self, *args, **kwargs): def import_core(
warnings.warn("the `crepes export-csv` command is deprecated; " ctx: typer.Context,
"please use `core-office export-csv` instead", dbkey: Annotated[
DeprecationWarning, stacklevel=2) str,
super().__init__(*args, **kwargs) 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 "
class ImportCore(ImportToCore): "config file.")] = 'host',
**kwargs
):
""" """
Import data from another CORE database Import data from another CORE database
""" """
name = 'import-core' config = ctx.parent.rattail_config
description = __doc__.strip() progress = ctx.parent.rattail_progress
handler_key = 'to_corepos_db_office_op.from_corepos_db_office_op.import' handler = ImportCommandHandler(
accepts_dbkey_param = True config, import_handler_key='to_corepos_db_office_op.from_corepos_db_office_op.import')
kwargs['user'] = typer_get_runas_user(ctx)
def add_parser_args(self, parser): kwargs['handler_kwargs'] = {'dbkey': dbkey}
super(ImportCore, self).add_parser_args(parser) handler.run(kwargs, progress=progress)
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)

View file

@ -37,25 +37,9 @@ zip_safe = False
[options.entry_points] [options.entry_points]
console_scripts = 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 = 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.config.extensions =
rattail-corepos = rattail_corepos.config:RattailCOREPOSExtension rattail-corepos = rattail_corepos.config:RattailCOREPOSExtension