Migrate commands to use 'typer' framework

This commit is contained in:
Lance Edgar 2024-05-15 16:24:01 -05:00
parent f18e0d3c3c
commit 2b2703ea4e
2 changed files with 47 additions and 66 deletions

View file

@ -27,6 +27,7 @@ install_requires =
flet
psycopg2
rattail[db]
typer
packages = find:
include_package_data = True
@ -36,12 +37,7 @@ include_package_data = True
[options.entry_points]
console_scripts =
wuttapos = wuttapos.commands:main
wuttapos = wuttapos.commands:wuttapos_typer
rattail.config.extensions =
wuttapos = wuttapos.config:WuttaConfigExtension
wuttapos.subcommands =
open = wuttapos.commands:Open
serve = wuttapos.commands:Serve
status = wuttapos.commands:Status

View file

@ -2,7 +2,7 @@
################################################################################
#
# WuttaPOS -- Pythonic Point of Sale System
# Copyright © 2023 Lance Edgar
# Copyright © 2023-2024 Lance Edgar
#
# This file is part of WuttaPOS.
#
@ -25,90 +25,75 @@ WuttaPOS commands
"""
import logging
import sys
from rattail import commands
import typer
from rattail.files import resource_path
from wuttapos import __version__
from rattail.commands.typer import typer_callback
log = logging.getLogger(__name__)
def main(*args):
"""
Main entry point for WuttaPOS command system
"""
args = list(args or sys.argv[1:])
cmd = Command()
cmd.run(*args)
wuttapos_typer = typer.Typer(
callback=typer_callback,
help="WuttaPOS (point of sale)"
)
class Command(commands.Command):
"""
Top-level command for WuttaPOS
"""
name = 'wuttapos'
version = __version__
description = "WuttaPOS (point of sale)"
long_description = ''
class Open(commands.Subcommand):
@wuttapos_typer.command('open')
def typer_open(
ctx: typer.Context,
):
"""
Open the Point of Sale app
"""
name = 'open'
description = __doc__.strip()
from wuttapos.app import run_app
def run(self, args):
from wuttapos.app import run_app
run_app(self.config)
config = ctx.parent.rattail_config
run_app(config)
class Serve(commands.Subcommand):
@wuttapos_typer.command()
def serve(
ctx: typer.Context,
):
"""
Run the POS app as a web service
"""
name = 'serve'
description = __doc__.strip()
import flet as ft
from wuttapos.app import main
def run(self, args):
import flet as ft
from wuttapos.app import main
config = ctx.parent.rattail_config
kw = {}
kw = {}
host = config.get('wuttapos', 'serve.host',
default='0.0.0.0')
kw['host'] = host
host = self.config.get('wuttapos', 'serve.host',
default='0.0.0.0')
kw['host'] = host
port = config.getint('wuttapos', 'serve.port',
default=8332)
kw['port'] = port
port = self.config.getint('wuttapos', 'serve.port',
default=8332)
kw['port'] = port
# TODO: we technically "support" this, in that we do pass the
# value on to Flet, but in practice it does not work right
path = config.get('wuttapos', 'serve.path', default='')
if path:
path = path.strip('/') + '/'
kw['name'] = path
# kw['route_url_strategy'] = 'hash'
# TODO: we technically "support" this, in that we do pass the
# value on to Flet, but in practice it does not work right
path = self.config.get('wuttapos', 'serve.path', default='')
if path:
path = path.strip('/') + '/'
kw['name'] = path
# kw['route_url_strategy'] = 'hash'
log.info(f"will serve WuttaPOS on http://{host}:{port}/{path}")
ft.app(target=main, view=None,
assets_dir=resource_path('wuttapos:assets'),
**kw)
log.info(f"will serve WuttaPOS on http://{host}:{port}/{path}")
ft.app(target=main, view=None,
assets_dir=resource_path('wuttapos:assets'),
**kw)
class Status(commands.Subcommand):
@wuttapos_typer.command()
def status(
ctx: typer.Context,
):
"""
Show status of the POS lane
"""
name = 'status'
description = __doc__.strip()
def run(self, args):
print("TODO: show status")
print("TODO: show status")