Skip to content

Commit

Permalink
code cov work, streamline command resolution code
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Dec 24, 2023
1 parent 0578ac6 commit 00593bc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ poetry.lock
tests/**/build
tests/click/callback_record.json
tests/click/cache.json
tests/click/validation/resized_html.png
57 changes: 19 additions & 38 deletions sphinxcontrib/typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@
__copyright__ = 'Copyright 2023 Brian Kohan'


def _get_lazyload_commands(ctx: click.Context) -> t.Dict[str, click.Command]:
commands = {}
for command in ctx.command.list_commands(ctx):
commands[command] = ctx.command.get_command(ctx, command)

return commands


def _filter_commands(
ctx: click.Context,
commands: t.Optional[t.List[str]] = None,
) -> t.List[click.Command]:
"""Return list of used commands."""
lookup = getattr(ctx.command, 'commands', {})
if not lookup and isinstance(ctx.command, click.MultiCommand):
lookup = _get_lazyload_commands(ctx)

if commands is None:
return sorted(lookup.values(), key=lambda item: item.name)

return [lookup[command] for command in commands if command in lookup]
ctx: click.Context,
cmd_filter: t.Optional[t.List[str]] = None
):
return sorted([
cmd for name, cmd in getattr(
ctx.command,
'commands',
{
name:
ctx.command.get_command(ctx, name)
for name in getattr(
ctx.command,
'list_commands',
lambda _: []
)(ctx) or cmd_filter
}
).items()
if not cmd_filter or name in cmd_filter
], key=lambda item: item.name)


class RenderTarget(str, Enum):
Expand All @@ -86,15 +85,6 @@ class RenderTarget(str, Enum):
def __str__(self) -> str:
return self.value

@classmethod
def __missing__(cls, argument) -> str:
if argument:
raise ValueError(
f'"{argument}" is not a valid RenderTarget: '
f'{[str(target) for target in cls]}'
)
return None


class RenderTheme(str, Enum):
LIGHT = 'light'
Expand All @@ -106,15 +96,6 @@ class RenderTheme(str, Enum):
def __str__(self) -> str:
return self.value

@classmethod
def __missing__(cls, argument) -> str:
if argument:
raise ValueError(
f'"{argument}" is not a valid RenderTheme: '
f'{[str(target) for target in cls]}'
)
return None

@property
def terminal_theme(self) -> rich_theme.TerminalTheme:
return {
Expand Down
Binary file removed tests/click/validation/resized_html.png
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,12 @@ def test_click_latex_build_works():

if bld_dir.exists():
shutil.rmtree(bld_dir.parent)


def test_enums():
from sphinxcontrib.typer import RenderTarget, RenderTheme
for target in RenderTarget:
assert target.value == str(target)
for theme in RenderTheme:
assert theme.value == str(theme)

0 comments on commit 00593bc

Please sign in to comment.