Project generator should make typer commands, not old-style

This commit is contained in:
Lance Edgar 2024-06-04 22:07:02 -05:00
parent 0439db869f
commit 462791b804
2 changed files with 34 additions and 50 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.
# #
@ -262,7 +262,7 @@ class PythonProjectGenerator(ProjectGenerator):
key = 'python' key = 'python'
def make_schema(self, **kwargs): def make_schema(self, **kwargs):
schema = super(PythonProjectGenerator, self).make_schema(**kwargs) schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='name', schema.add(colander.SchemaNode(name='name',
typ=colander.String())) typ=colander.String()))
@ -276,7 +276,7 @@ class PythonProjectGenerator(ProjectGenerator):
return schema return schema
def normalize_context(self, context): def normalize_context(self, context):
context = super(PythonProjectGenerator, self).normalize_context(context) context = super().normalize_context(context)
if 'description' not in context: if 'description' not in context:
context['description'] = "" context['description'] = ""
@ -378,7 +378,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
key = 'rattail_adjacent' key = 'rattail_adjacent'
def make_schema(self, **kwargs): def make_schema(self, **kwargs):
schema = super(RattailAdjacentProjectGenerator, self).make_schema(**kwargs) schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='extends_config', schema.add(colander.SchemaNode(name='extends_config',
typ=colander.Boolean())) typ=colander.Boolean()))
@ -395,7 +395,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
return schema return schema
def normalize_context(self, context): def normalize_context(self, context):
context = super(RattailAdjacentProjectGenerator, self).normalize_context(context) context = super().normalize_context(context)
context['requires'].setdefault('rattail', True) context['requires'].setdefault('rattail', True)
@ -410,11 +410,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
if context['has_cli']: if context['has_cli']:
context['entry_points'].setdefault('console_scripts', []).extend([ context['entry_points'].setdefault('console_scripts', []).extend([
"{0} = {0}.commands:main".format(context['pkg_name']), f"{context['pkg_name']} = {context['pkg_name']}.commands:{context['pkg_name']}_typer",
])
context['entry_points'].setdefault('{}.commands'.format(context['pkg_name']), []).extend([
"hello = {}.commands:HelloWorld".format(context['pkg_name']),
"install = {}.commands:Install".format(context['pkg_name']),
]) ])
# nb. these alembic values are only needed for installer # nb. these alembic values are only needed for installer
@ -435,8 +431,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
from alembic.config import Config as AlembicConfig from alembic.config import Config as AlembicConfig
from alembic.command import revision as alembic_revision from alembic.command import revision as alembic_revision
super(RattailAdjacentProjectGenerator, self).generate_project( super().generate_project(output, context, **kwargs)
output, context, **kwargs)
############################## ##############################
# root package dir # root package dir
@ -558,7 +553,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
key = 'poser' key = 'poser'
def make_schema(self, **kwargs): def make_schema(self, **kwargs):
schema = super(PoserProjectGenerator, self).make_schema(**kwargs) schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='organization', schema.add(colander.SchemaNode(name='organization',
typ=colander.String())) typ=colander.String()))
@ -584,7 +579,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
return schema return schema
def normalize_context(self, context): def normalize_context(self, context):
context = super(PoserProjectGenerator, self).normalize_context(context) context = super().normalize_context(context)
if not context.get('description'): if not context.get('description'):
context['description'] = "Rattail/Poser project for {}".format( context['description'] = "Rattail/Poser project for {}".format(
@ -631,8 +626,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
return context return context
def generate_project(self, output, context, **kwargs): def generate_project(self, output, context, **kwargs):
super(PoserProjectGenerator, self).generate_project( super().generate_project(output, context, **kwargs)
output, context, **kwargs)
package = os.path.join(output, context['pkg_name']) package = os.path.join(output, context['pkg_name'])

View file

@ -4,53 +4,43 @@
${name} commands ${name} commands
""" """
import sys import typer
from rattail import commands from rattail.commands.typer import make_typer
from rattail.commands.util import rprint
from ${pkg_name} import __version__ from ${pkg_name} import __version__
def main(*args): ${pkg_name}_typer = make_typer(
""" name='${pkg_name}',
Main entry point for ${name} command system help="${name} -- ${description}"
""" )
args = list(args or sys.argv[1:])
cmd = Command()
cmd.run(*args)
class Command(commands.Command): @${pkg_name}_typer.command()
""" def hello(
Main command for ${name} ctx: typer.Context,
""" ):
name = '${pkg_name}'
version = __version__
description = "${name} (custom Rattail system)"
long_description = ''
class HelloWorld(commands.Subcommand):
""" """
The requisite 'hello world' example The requisite 'hello world' example
""" """
name = 'hello' rprint("\n\t[blue]Welcome to ${name} {}![/blue]\n".format(__version__))
description = __doc__.strip()
def run(self, args):
self.rprint("\n\t[blue]Welcome to ${name} {}![/blue]\n".format(__version__))
class Install(commands.InstallSubcommand): @${pkg_name}_typer.command()
def install(
ctx: typer.Context,
):
""" """
Install the ${name} app Install the ${name} app
""" """
name = 'install' from rattail.install import InstallHandler
description = __doc__.strip()
# nb. these must be explicitly set b/c config is not available config = ctx.parent.rattail_config
# when running normally, e.g. `${pkg_name} -n install` handler = InstallHandler(config,
app_title = "${name}" app_title="${name}",
app_package = '${pkg_name}' app_package='${pkg_name}',
app_eggname = '${egg_name}' app_eggname='${egg_name}',
app_pypiname = '${pypi_name}' app_pypiname='${pypi_name}')
handler.run()