|
| 1 | +"""Container classes and enums. |
| 2 | +
|
| 3 | +.. module:: containers |
| 4 | + :synopsis: Container classes and enums. |
| 5 | +
|
| 6 | +.. moduleauthor:: Simon Larsén |
| 7 | +""" |
| 8 | +import collections |
| 9 | +import enum |
| 10 | +import argparse |
| 11 | +import pluggy |
| 12 | +import typing |
| 13 | + |
| 14 | +from repobee_plug import exception |
| 15 | + |
| 16 | + |
| 17 | +hookspec = pluggy.HookspecMarker(__package__) |
| 18 | +hookimpl = pluggy.HookimplMarker(__package__) |
| 19 | + |
| 20 | +HookResult = collections.namedtuple("HookResult", ("hook", "status", "msg")) |
| 21 | + |
| 22 | + |
| 23 | +class ExtensionParser(argparse.ArgumentParser): |
| 24 | + """An ArgumentParser specialized for RepoBee extension commands.""" |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + super().__init__(add_help=False) |
| 28 | + |
| 29 | + |
| 30 | +class ExtensionCommand( |
| 31 | + collections.namedtuple( |
| 32 | + "ExtensionCommand", |
| 33 | + ("parser", "name", "help", "description", "callback", "requires_api"), |
| 34 | + ) |
| 35 | +): |
| 36 | + """Class defining an extension command for the RepoBee CLI.""" |
| 37 | + |
| 38 | + def __new__( |
| 39 | + cls, |
| 40 | + parser: ExtensionParser, |
| 41 | + name: str, |
| 42 | + help: str, |
| 43 | + description: str, |
| 44 | + callback: typing.Callable[[argparse.Namespace, "apimeta.API"], None], |
| 45 | + requires_api: bool = False, |
| 46 | + ): |
| 47 | + """ |
| 48 | + Args: |
| 49 | + parser: The parser to use for the CLI. |
| 50 | + name: Name of the command. |
| 51 | + help: Text that will be displayed when running ``repobee -h`` |
| 52 | + description: Text that will be displayed when calling the ``-h`` |
| 53 | + option for this specific command. Should be elaborate in |
| 54 | + describing the usage of the command. |
| 55 | + callback: A callback function that is called if this command is |
| 56 | + used on the CLI. It is passed the parsed namespace and the |
| 57 | + platform API, and is expected to return nothing. |
| 58 | + requires_api: If True, the base arguments required for the platform |
| 59 | + API are added as options to the extension command, and the |
| 60 | + platform API is then passed to the callback function. It is |
| 61 | + then important not to have clashing option names. If False, the |
| 62 | + base arguments are not added to the CLI, and None is passed in |
| 63 | + place of the API. |
| 64 | + """ |
| 65 | + if not isinstance(parser, ExtensionParser): |
| 66 | + raise exception.ExtensionCommandError( |
| 67 | + "parser must be a {.__name__}".format(ExtensionParser) |
| 68 | + ) |
| 69 | + if not callable(callback): |
| 70 | + raise exception.ExtensionCommandError("callback must be a callable") |
| 71 | + return super().__new__( |
| 72 | + cls, parser, name, help, description, callback, requires_api |
| 73 | + ) |
| 74 | + |
| 75 | + def __eq__(self, other): |
| 76 | + """Two ExtensionCommands are equal if they compare equal in all |
| 77 | + respects except for the parser, as argpars.ArgumentParser instances do |
| 78 | + not implement __eq__. |
| 79 | + """ |
| 80 | + _, *rest = self |
| 81 | + _, *other_rest = other |
| 82 | + return rest == other_rest |
| 83 | + |
| 84 | + |
| 85 | +class Status(enum.Enum): |
| 86 | + """Status codes enum.""" |
| 87 | + |
| 88 | + SUCCESS = "success" |
| 89 | + WARNING = "warning" |
| 90 | + ERROR = "error" |
0 commit comments