-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into kl/hp-tests
- Loading branch information
Showing
19 changed files
with
873 additions
and
743 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (C) 2024 CVAT.ai Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import argparse | ||
import types | ||
from collections.abc import Mapping | ||
from typing import Callable, Protocol | ||
|
||
|
||
class Command(Protocol): | ||
@property | ||
def description(self) -> str: ... | ||
|
||
def configure_parser(self, parser: argparse.ArgumentParser) -> None: ... | ||
|
||
# The exact parameters accepted by `execute` vary between commands, | ||
# so we're forced to declare it like this instead of as a method. | ||
@property | ||
def execute(self) -> Callable[..., None]: ... | ||
|
||
|
||
class CommandGroup: | ||
def __init__(self, *, description: str) -> None: | ||
self._commands: dict[str, Command] = {} | ||
self.description = description | ||
|
||
def command_class(self, name: str): | ||
def decorator(cls: type): | ||
self._commands[name] = cls() | ||
return cls | ||
|
||
return decorator | ||
|
||
def add_command(self, name: str, command: Command) -> None: | ||
self._commands[name] = command | ||
|
||
@property | ||
def commands(self) -> Mapping[str, Command]: | ||
return types.MappingProxyType(self._commands) | ||
|
||
def configure_parser(self, parser: argparse.ArgumentParser) -> None: | ||
subparsers = parser.add_subparsers(required=True) | ||
|
||
for name, command in self._commands.items(): | ||
subparser = subparsers.add_parser(name, description=command.description) | ||
subparser.set_defaults(_executor=command.execute) | ||
command.configure_parser(subparser) | ||
|
||
def execute(self) -> None: | ||
# It should be impossible for a command group to be executed, | ||
# because configure_parser requires that a subcommand is specified. | ||
assert False, "unreachable code" |
Oops, something went wrong.