Skip to content

Commit

Permalink
add completers, try fix github CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jan 26, 2024
1 parent 7a153f0 commit 4b91902
Show file tree
Hide file tree
Showing 26 changed files with 687 additions and 90 deletions.
76 changes: 35 additions & 41 deletions django_typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
"initialize",
"command",
"group",
"get_command",
"COMPLETE_VAR",
"get_command"
]

"""
Expand All @@ -80,19 +79,29 @@
behavior should align with native django commands
"""

# def get_color_system(default):
# ctx = click.get_current_context(silent=True)
# if ctx:
# return None if ctx.django_command.style == no_style() else default
# return default

# COLOR_SYSTEM = lazy(get_color_system, str)
# rich_utils.COLOR_SYSTEM = COLOR_SYSTEM(rich_utils.COLOR_SYSTEM)

COMPLETE_VAR = "_COMPLETE_INSTRUCTION"

# try:
# from typer import rich_utils
# def get_color_system(default):
# return None
# ctx = click.get_current_context(silent=True)
# if ctx:
# return None if ctx.django_command.style == no_style() else default
# return default

# COLOR_SYSTEM = lazy(get_color_system, str)
# rich_utils.COLOR_SYSTEM = COLOR_SYSTEM(rich_utils.COLOR_SYSTEM)
# except ImportError:
# pass

def traceback_config():
"""
Fetch the rich traceback installation parameters from our settings. By default
rich tracebacks are on with show_locals = True. If the config is set to False
or None rich tracebacks will not be installed even if the library is present.
This allows us to have a common traceback configuration for all commands. If rich
tracebacks are managed separately this setting can also be switched off.
"""
cfg = getattr(settings, "DT_RICH_TRACEBACK_CONFIG", {"show_locals": True})
if cfg:
return {"show_locals": True, **cfg}
Expand Down Expand Up @@ -129,7 +138,7 @@ def _common_options(
force_color: ForceColor = False,
skip_checks: SkipChecks = False,
):
pass
pass # pragma: no cover


# cache common params to avoid this extra work on every command
Expand Down Expand Up @@ -243,6 +252,9 @@ def shell_complete(self, ctx: Context, incomplete: str) -> t.List[CompletionItem
By default if the incomplete string is a space and there are no completions
the click infrastructure will return _files. We'd rather return parameters
for the command if there are any available.
TODO - remove parameters that are already provided and do not allow multiple
specifications.
"""
completions = super().shell_complete(ctx, incomplete)
if (
Expand Down Expand Up @@ -320,7 +332,7 @@ def common_params(self):
return [
param
for param in _get_common_params()
if param.name in (self.django_command.django_params or [])
if param.name not in (self.django_command.suppressed_base_arguments or [])
]
return super().common_params()

Expand All @@ -336,7 +348,7 @@ def common_params(self):
return [
param
for param in _get_common_params()
if param.name in (self.django_command.django_params or [])
if param.name not in (self.django_command.suppressed_base_arguments or [])
]
return super().common_params()

Expand Down Expand Up @@ -689,7 +701,7 @@ def handle(self, *args, **options):
"_handle": attrs.pop("handle", None),
**attrs,
"handle": handle,
"typer_app": typer_app,
"typer_app": typer_app
}

return super().__new__(mcs, name, bases, attrs)
Expand All @@ -700,6 +712,10 @@ def __init__(cls, name, bases, attrs, **kwargs):
"""
if cls.typer_app is not None:
cls.typer_app.info.name = cls.__module__.rsplit(".", maxsplit=1)[-1]
cls.suppressed_base_arguments = {
arg.lstrip('--').replace('-', '_')
for arg in cls.suppressed_base_arguments
} # per django docs - allow these to be specified by either the option or param name

def get_ctor(attr):
return getattr(
Expand Down Expand Up @@ -811,17 +827,7 @@ def parse_args(self, args=None, namespace=None):
django_command=self.django_command,
args=list(args or []),
) as ctx:
params = ctx.params

# def discover_parsed_args(ctx):
# # todo is this necessary?
# for child in ctx.children:
# discover_parsed_args(child)
# params.update(child.params)

# discover_parsed_args(ctx)

return _ParsedArgs(args=args or [], **{**COMMON_DEFAULTS, **params})
return _ParsedArgs(args=args or [], **{**COMMON_DEFAULTS, **ctx.params})
except click.exceptions.Exit:
sys.exit()

Expand Down Expand Up @@ -863,19 +869,7 @@ class Command(TyperCommand, attach='app_label.command_name.subcommand1.subcomman
# we do not use verbosity because the base command does not do anything with it
# if users want to use a verbosity flag like the base django command adds
# they can use the type from django_typer.types.Verbosity
django_params: t.Optional[
t.List[
t.Literal[
"version",
"settings",
"pythonpath",
"traceback",
"no_color",
"force_color",
"skip_checks",
]
]
] = [name for name in COMMON_DEFAULTS.keys() if name != "verbosity"]
suppressed_base_arguments: t.Optional[t.Iterable[str]] = {'verbosity'}

class CommandNode:
name: str
Expand Down
15 changes: 14 additions & 1 deletion django_typer/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Django Typer app config. This module includes settings check and rich traceback
installation logic.
"""
import inspect

from django.apps import AppConfig
Expand All @@ -12,6 +16,7 @@

tb_config = traceback_config()
if isinstance(tb_config, dict) and not tb_config.get("no_install", False):
# install rich tracebacks if we've been configured to do so (default)
rich.traceback.install(
**{
param: value
Expand All @@ -26,6 +31,10 @@

@register("settings")
def check_traceback_config(app_configs, **kwargs):
"""
A system check that validates that the traceback config is valid and
contains only the expected parameters.
"""
warnings = []
tb_cfg = traceback_config()
if isinstance(tb_cfg, dict):
Expand All @@ -51,6 +60,10 @@ def check_traceback_config(app_configs, **kwargs):


class DjangoTyperConfig(AppConfig):
"""
Django Typer app config.
"""

name = "django_typer"
label = name.replace(".", "_")
label = name
verbose_name = "Django Typer"
Loading

0 comments on commit 4b91902

Please sign in to comment.