Skip to content

Commit fb0d9f8

Browse files
committed
fix: simplify
1 parent 6bc0e5d commit fb0d9f8

4 files changed

Lines changed: 46 additions & 63 deletions

File tree

src/_pytask/click.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from collections.abc import Sequence
3434

3535

36-
__all__ = ["ColoredCommand", "ColoredGroup", "EnumChoice"]
36+
__all__ = ["ColoredCommand", "ColoredGroup", "EnumChoice", "get_command"]
3737

3838

3939
if importlib.metadata.version("click") < "8.2":
@@ -47,6 +47,14 @@ def split_opt(option: str) -> tuple[str, str]:
4747
return cast("Callable[[str], tuple[str, str]]", _split_opt)(option)
4848

4949

50+
def get_command(cli: click.Group, name: str) -> click.Command:
51+
"""Get a nested command by name."""
52+
command: click.Command = cli
53+
for part in name.split():
54+
command = cast("click.Group", command).commands[part]
55+
return command
56+
57+
5058
class EnumChoice(Choice):
5159
"""An enum-based choice type.
5260

src/_pytask/lock.py

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@
4343
from _pytask.lockfile import LockfileState
4444

4545

46+
def _add_lock_command_options(
47+
*, dry_run_help: str
48+
) -> Callable[[Callable[..., None]], Callable[..., None]]:
49+
def decorator(func: Callable[..., None]) -> Callable[..., None]:
50+
func = click.option(
51+
"--dry-run",
52+
is_flag=True,
53+
default=False,
54+
help=dry_run_help,
55+
)(func)
56+
return click.option(
57+
"-y",
58+
"--yes",
59+
is_flag=True,
60+
default=False,
61+
help="Apply the changes without prompting for confirmation.",
62+
)(func)
63+
64+
return decorator
65+
66+
4667
@dataclass(slots=True)
4768
class _PlannedChange:
4869
kind: str
@@ -237,6 +258,7 @@ def _run_lock_command(
237258
planner: Callable[[Session], list[_PlannedChange]] | None = None,
238259
planner_with_tasks: Callable[[Session, list[PTask]], list[_PlannedChange]]
239260
| None = None,
261+
select_tasks: Callable[[Session], list[PTask]] | None = None,
240262
empty_message: str,
241263
) -> int:
242264
_validate_confirmation_options(raw_config)
@@ -259,11 +281,8 @@ def _run_lock_command(
259281
session.dag = create_dag(session=session)
260282

261283
if planner_with_tasks is not None:
262-
tasks = (
263-
_select_tasks_with_ancestors(session)
264-
if raw_config["subcommand"] == "accept"
265-
else _select_tasks_exact(session)
266-
)
284+
assert select_tasks is not None
285+
tasks = select_tasks(session)
267286
planned_changes = planner_with_tasks(session, tasks)
268287
else:
269288
assert planner is not None
@@ -305,70 +324,40 @@ def lock() -> None:
305324

306325

307326
@lock.command(cls=ColoredCommand)
308-
@click.option(
309-
"--dry-run",
310-
is_flag=True,
311-
default=False,
312-
help="Show which recorded states would be updated without writing changes.",
313-
)
314-
@click.option(
315-
"-y",
316-
"--yes",
317-
is_flag=True,
318-
default=False,
319-
help="Apply the changes without prompting for confirmation.",
327+
@_add_lock_command_options(
328+
dry_run_help="Show which recorded states would be updated without writing changes."
320329
)
321330
def accept(**raw_config: Any) -> None:
322331
"""Accept the current state for selected tasks and their ancestors."""
323-
raw_config["subcommand"] = "accept"
324332
sys.exit(
325333
_run_lock_command(
326334
raw_config,
327335
planner_with_tasks=_plan_accept_changes,
336+
select_tasks=_select_tasks_with_ancestors,
328337
empty_message="No lockfile entries need updating.",
329338
)
330339
)
331340

332341

333342
@lock.command(cls=ColoredCommand)
334-
@click.option(
335-
"--dry-run",
336-
is_flag=True,
337-
default=False,
338-
help="Show which recorded states would be removed without writing changes.",
339-
)
340-
@click.option(
341-
"-y",
342-
"--yes",
343-
is_flag=True,
344-
default=False,
345-
help="Apply the changes without prompting for confirmation.",
343+
@_add_lock_command_options(
344+
dry_run_help="Show which recorded states would be removed without writing changes."
346345
)
347346
def reset(**raw_config: Any) -> None:
348347
"""Remove recorded state for selected tasks."""
349-
raw_config["subcommand"] = "reset"
350348
sys.exit(
351349
_run_lock_command(
352350
raw_config,
353351
planner_with_tasks=_plan_reset_changes,
352+
select_tasks=_select_tasks_exact,
354353
empty_message="No lockfile entries need removing.",
355354
)
356355
)
357356

358357

359358
@lock.command(cls=ColoredCommand)
360-
@click.option(
361-
"--dry-run",
362-
is_flag=True,
363-
default=False,
364-
help="Show which stale entries would be removed without writing changes.",
365-
)
366-
@click.option(
367-
"-y",
368-
"--yes",
369-
is_flag=True,
370-
default=False,
371-
help="Apply the changes without prompting for confirmation.",
359+
@_add_lock_command_options(
360+
dry_run_help="Show which stale entries would be removed without writing changes."
372361
)
373362
def clean(**raw_config: Any) -> None:
374363
"""Remove stale lockfile entries which no longer correspond to collected tasks."""

src/_pytask/mark/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from dataclasses import dataclass
77
from typing import TYPE_CHECKING
88
from typing import Any
9-
from typing import cast
109

1110
import click
1211
from rich.table import Table
1312

1413
from _pytask.click import ColoredCommand
14+
from _pytask.click import get_command
1515
from _pytask.console import console
1616
from _pytask.dag_utils import task_and_preceding_tasks
1717
from _pytask.exceptions import ConfigurationError
@@ -75,13 +75,6 @@ def markers(**raw_config: Any) -> NoReturn:
7575
sys.exit(session.exit_code)
7676

7777

78-
def _get_command(cli: click.Group, name: str) -> click.Command:
79-
command: click.Command = cli
80-
for part in name.split():
81-
command = cast("click.Group", command).commands[part]
82-
return command
83-
84-
8578
@hookimpl
8679
def pytask_extend_command_line_interface(cli: click.Group) -> None:
8780
"""Add marker related options."""
@@ -110,7 +103,7 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
110103
),
111104
]
112105
for command in ("build", "clean", "collect", "lock accept", "lock reset"):
113-
target = _get_command(cli, command)
106+
target = get_command(cli, command)
114107
target.params.extend(additional_build_parameters)
115108

116109

src/_pytask/parameters.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import importlib.util
66
from pathlib import Path
77
from typing import TYPE_CHECKING
8-
from typing import cast
98

109
import click
1110
from click import Context
1211
from sqlalchemy.engine import URL
1312
from sqlalchemy.engine import make_url
1413
from sqlalchemy.exc import ArgumentError
1514

15+
from _pytask.click import get_command
1616
from _pytask.config_utils import set_defaults_from_config
1717
from _pytask.path import import_path
1818
from _pytask.pluginmanager import hookimpl
@@ -180,13 +180,6 @@ def pytask_add_hooks(pm: PluginManager) -> None:
180180
)
181181

182182

183-
def _get_command(cli: click.Group, name: str) -> click.Command:
184-
command: click.Command = cli
185-
for part in name.split():
186-
command = cast("click.Group", command).commands[part]
187-
return command
188-
189-
190183
@hookimpl(trylast=True)
191184
def pytask_extend_command_line_interface(cli: click.Group) -> None:
192185
"""Register general markers."""
@@ -203,7 +196,7 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
203196
"markers",
204197
"profile",
205198
):
206-
target = _get_command(cli, command)
199+
target = get_command(cli, command)
207200
target.params.extend((_CONFIG_OPTION, _HOOK_MODULE_OPTION, _PATH_ARGUMENT))
208201
for command in (
209202
"build",
@@ -214,7 +207,7 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
214207
"lock reset",
215208
"profile",
216209
):
217-
target = _get_command(cli, command)
210+
target = get_command(cli, command)
218211
target.params.extend([_IGNORE_OPTION, _EDITOR_URL_SCHEME_OPTION])
219212
for command in ("build",):
220213
cli.commands[command].params.append(_VERBOSE_OPTION)

0 commit comments

Comments
 (0)