From 8287f02793dce4530ca7de0d7ee64a5a1b809689 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 15 May 2024 15:34:42 -0500 Subject: [PATCH] Migrate all commands to use 'typer' framework --- rattail_demo/commands.py | 89 ++++++++++++++++++++++------------------ setup.cfg | 17 ++------ 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/rattail_demo/commands.py b/rattail_demo/commands.py index a4a3a30..9aa1dc1 100644 --- a/rattail_demo/commands.py +++ b/rattail_demo/commands.py @@ -3,59 +3,68 @@ Rattail Demo Commands """ -import sys +import typer -from rattail import commands -from rattail_demo import __version__ +from rattail.commands.typer import (typer_callback, typer_get_runas_user, + importer_command, file_exporter_command) +from rattail.commands.importing import ImportCommandHandler -def main(*args): +# nb. this is the top-level command for titeship +rattail_demo_typer = typer.Typer( + callback=typer_callback, + help="Rattail Demo (custom Rattail system)" +) + + +@rattail_demo_typer.command() +@file_exporter_command +def export_shopfoo( + ctx: typer.Context, + **kwargs +): """ - Main entry point for Rattail Demo command system + Export data to the Harvest system """ - args = list(args or sys.argv[1:]) - cmd = Command() - cmd.run(*args) + config = ctx.parent.rattail_config + progress = ctx.parent.rattail_progress + handler = ImportCommandHandler( + config, import_handler_spec='rattail_demo.shopfoo.importing.rattail:FromRattailToShopfoo') + kwargs['user'] = typer_get_runas_user(ctx) + kwargs['handler_kwargs'] = {'output_dir': kwargs['output_dir']} + handler.run(kwargs, progress=progress) -class Command(commands.Command): - """ - Main command for Rattail Demo - """ - name = 'rattail-demo' - version = __version__ - description = "Rattail Demo (custom Rattail system)" - long_description = "" - - -class ExportShopfoo(commands.ExportFileSubcommand): - """ - Export data to the Shopfoo system - """ - name = 'export-shopfoo' - description = __doc__.strip() - handler_spec = 'rattail_demo.shopfoo.importing.rattail:FromRattailToShopfoo' - - -class ImportSelf(commands.ImportSubcommand): +@rattail_demo_typer.command() +@importer_command +def import_self( + ctx: typer.Context, + **kwargs +): """ Update "cascading" Rattail data based on "core" Rattail data """ - name = 'import-self' - description = __doc__.strip() - handler_spec = 'rattail_demo.importing.local:FromRattailDemoToSelf' + config = ctx.parent.rattail_config + progress = ctx.parent.rattail_progress + handler = ImportCommandHandler( + config, import_handler_spec='rattail_demo.importing.local:FromRattailDemoToSelf') + kwargs['user'] = typer_get_runas_user(ctx) + handler.run(kwargs, progress=progress) -class Install(commands.InstallSubcommand): +@rattail_demo_typer.command() +def install( + ctx: typer.Context, +): """ Install the Rattail Demo app """ - name = 'install' - description = __doc__.strip() + from rattail.install import InstallHandler - # nb. these must be explicitly set b/c config is not available - # when running normally, e.g. `rattail-demo -n install` - app_title = "Rattail Demo" - app_package = 'rattail_demo' - app_eggname = 'rattail_demo' - app_pypiname = 'rattail-demo' + config = ctx.parent.rattail_config + handler = InstallHandler(config, + app_title="Rattail Demo", + app_package='rattail_demo', + app_eggname='rattail_demo', + app_pypiname='rattail_demo') + handler.run() diff --git a/setup.cfg b/setup.cfg index a4fad5d..6e8b2c1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,23 +38,12 @@ packages = find: [options.entry_points] +console_scripts = + rattail-demo = rattail_demo.commands:rattail_demo_typer + paste.app_factory = main = rattail_demo.web.app:main webapi = rattail_demo.web.webapi:main rattail.config.extensions = rattail-demo = rattail_demo.config:DemoConfigExtension - -console_scripts = - rattail-demo = rattail_demo.commands:main - -rattail_demo.subcommands = - export-shopfoo = rattail_demo.commands:ExportShopfoo - import-self = rattail_demo.commands:ImportSelf - install = rattail_demo.commands:Install - -# TODO: remove this legacy group once the above works okay -rattail_demo.commands = - export-shopfoo = rattail_demo.commands:ExportShopfoo - import-self = rattail_demo.commands:ImportSelf - install = rattail_demo.commands:Install