Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-dupe extensions to improve performance #209

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/doccmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from enum import Enum, auto, unique
from importlib.metadata import PackageNotFoundError, version
from pathlib import Path
from typing import TYPE_CHECKING, overload
from typing import TYPE_CHECKING, TypeVar, overload

import click
from beartype import beartype
Expand All @@ -33,6 +33,23 @@
from ._setuptools_scm_version import __version__


T = TypeVar("T")


@beartype
def _deduplicate(
ctx: click.Context,
param: click.Parameter,
sequence: Sequence[T],
) -> Sequence[T]:
"""
De-duplicate a sequence while keeping the order.
"""
assert ctx
assert param
return tuple(dict.fromkeys(sequence).keys())


@overload
def _validate_file_extension(
ctx: click.Context,
Expand Down Expand Up @@ -71,11 +88,14 @@ def _validate_file_extension(
def _validate_file_extensions(
ctx: click.Context,
param: click.Parameter,
values: tuple[str, ...],
) -> tuple[str, ...]:
values: Sequence[str],
) -> Sequence[str]:
"""
Validate that the input strings start with a dot.
"""
# This is not necessary but it saves us later working with
# duplicate values.
values = _deduplicate(ctx=ctx, param=param, sequence=values)
# We could just return `values` as we know that `_validate_file_extension`
# does not modify the given value, but to be safe, we use the returned
# values.
Expand Down Expand Up @@ -360,6 +380,7 @@ def _run_args_against_docs(
"Give multiple times for multiple languages."
),
multiple=True,
callback=_deduplicate,
)
@click.option("command", "-c", "--command", type=str, required=True)
@click.option(
Expand Down Expand Up @@ -412,6 +433,7 @@ def _run_args_against_docs(
"""
),
multiple=True,
callback=_deduplicate,
)
@click.option(
"--pad-file/--no-pad-file",
Expand All @@ -430,6 +452,7 @@ def _run_args_against_docs(
"document_paths",
type=click.Path(exists=True, path_type=Path, dir_okay=True),
nargs=-1,
callback=_deduplicate,
)
@click.version_option(version=__version__)
@click.option(
Expand Down Expand Up @@ -533,10 +556,6 @@ def main(
This works with Markdown and reStructuredText files.
"""
args = shlex.split(s=command)
# De-duplicate some choices, keeping the order.
languages = tuple(dict.fromkeys(languages).keys())
skip_markers = tuple(dict.fromkeys(skip_markers).keys())
document_paths = tuple(dict.fromkeys(document_paths).keys())
use_pty = use_pty_option.use_pty()

_validate_file_suffix_overlaps(
Expand Down