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
# Copyright © 2010-2023 Lance Edgar
# Copyright © 2010-2024 Lance Edgar
#
# This file is part of Rattail.
#
@ -262,7 +262,7 @@ class PythonProjectGenerator(ProjectGenerator):
key = 'python'
def make_schema(self, **kwargs):
schema = super(PythonProjectGenerator, self).make_schema(**kwargs)
schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='name',
typ=colander.String()))
@ -276,7 +276,7 @@ class PythonProjectGenerator(ProjectGenerator):
return schema
def normalize_context(self, context):
context = super(PythonProjectGenerator, self).normalize_context(context)
context = super().normalize_context(context)
if 'description' not in context:
context['description'] = ""
@ -378,7 +378,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
key = 'rattail_adjacent'
def make_schema(self, **kwargs):
schema = super(RattailAdjacentProjectGenerator, self).make_schema(**kwargs)
schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='extends_config',
typ=colander.Boolean()))
@ -395,7 +395,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
return schema
def normalize_context(self, context):
context = super(RattailAdjacentProjectGenerator, self).normalize_context(context)
context = super().normalize_context(context)
context['requires'].setdefault('rattail', True)
@ -410,11 +410,7 @@ class RattailAdjacentProjectGenerator(PythonProjectGenerator):
if context['has_cli']:
context['entry_points'].setdefault('console_scripts', []).extend([
"{0} = {0}.commands:main".format(context['pkg_name']),
])
context['entry_points'].setdefault('{}.commands'.format(context['pkg_name']), []).extend([
"hello = {}.commands:HelloWorld".format(context['pkg_name']),
"install = {}.commands:Install".format(context['pkg_name']),
f"{context['pkg_name']} = {context['pkg_name']}.commands:{context['pkg_name']}_typer",
])
# 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.command import revision as alembic_revision
super(RattailAdjacentProjectGenerator, self).generate_project(
output, context, **kwargs)
super().generate_project(output, context, **kwargs)
##############################
# root package dir
@ -558,7 +553,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
key = 'poser'
def make_schema(self, **kwargs):
schema = super(PoserProjectGenerator, self).make_schema(**kwargs)
schema = super().make_schema(**kwargs)
schema.add(colander.SchemaNode(name='organization',
typ=colander.String()))
@ -584,7 +579,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
return schema
def normalize_context(self, context):
context = super(PoserProjectGenerator, self).normalize_context(context)
context = super().normalize_context(context)
if not context.get('description'):
context['description'] = "Rattail/Poser project for {}".format(
@ -631,8 +626,7 @@ class PoserProjectGenerator(RattailAdjacentProjectGenerator):
return context
def generate_project(self, output, context, **kwargs):
super(PoserProjectGenerator, self).generate_project(
output, context, **kwargs)
super().generate_project(output, context, **kwargs)
package = os.path.join(output, context['pkg_name'])

View file

@ -4,53 +4,43 @@
${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__
def main(*args):
"""
Main entry point for ${name} command system
"""
args = list(args or sys.argv[1:])
cmd = Command()
cmd.run(*args)
${pkg_name}_typer = make_typer(
name='${pkg_name}',
help="${name} -- ${description}"
)
class Command(commands.Command):
"""
Main command for ${name}
"""
name = '${pkg_name}'
version = __version__
description = "${name} (custom Rattail system)"
long_description = ''
class HelloWorld(commands.Subcommand):
@${pkg_name}_typer.command()
def hello(
ctx: typer.Context,
):
"""
The requisite 'hello world' example
"""
name = 'hello'
description = __doc__.strip()
def run(self, args):
self.rprint("\n\t[blue]Welcome to ${name} {}![/blue]\n".format(__version__))
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
"""
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. `${pkg_name} -n install`
app_title = "${name}"
app_package = '${pkg_name}'
app_eggname = '${egg_name}'
app_pypiname = '${pypi_name}'
config = ctx.parent.rattail_config
handler = InstallHandler(config,
app_title="${name}",
app_package='${pkg_name}',
app_eggname='${egg_name}',
app_pypiname='${pypi_name}')
handler.run()