1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
#!/usr/bin/env python3
#
# commands.py
"""
Customised click commands and command groups.
.. versionadded:: 0.8.0
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# MarkdownHelpCommand.parse_args and the docstrings of ContextInheritingGroup
# based on https://github.com/pallets/click
# Copyright 2014 Pallets
# | Redistribution and use in source and binary forms, with or without modification,
# | are permitted provided that the following conditions are met:
# |
# | * Redistributions of source code must retain the above copyright notice,
# | this list of conditions and the following disclaimer.
# | * Redistributions in binary form must reproduce the above copyright notice,
# | this list of conditions and the following disclaimer in the documentation
# | and/or other materials provided with the distribution.
# | * Neither the name of the copyright holder nor the names of its contributors
# | may be used to endorse or promote products derived from this software without
# | specific prior written permission.
# |
# | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
# | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# stdlib
import difflib
import inspect
from textwrap import indent
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple
# 3rd party
import click
from click.core import iter_params_for_processing
from click.parser import split_opt
from click.utils import make_str
from domdf_python_tools.stringlist import DelimitedList
from domdf_python_tools.words import Plural
from mistletoe import block_token # type: ignore[import]
# this package
from consolekit.terminal_colours import ColourTrilean, resolve_color_default, strip_ansi
from consolekit.utils import TerminalRenderer
__all__ = (
"MarkdownHelpCommand",
"MarkdownHelpGroup",
"MarkdownHelpMixin",
"RawHelpCommand",
"RawHelpGroup",
"RawHelpMixin",
"SuggestionGroup",
"ContextInheritingGroup",
)
_argument = Plural("argument", "arguments")
class RawHelpMixin:
"""
Mixin class for :class:`click.Command` and :class:`click.Group` which leaves the help text unformatted.
.. seealso:: :class:`~.RawHelpCommand` and :class:`~.RawHelpGroup`
.. tip:: This can be combined with groups such as :class:`~.SuggestionGroup`.
.. versionadded:: 0.8.0
"""
help: Optional[str] # noqa: A003 # pylint: disable=redefined-builtin
def format_help_text(self, ctx: click.Context, formatter: click.formatting.HelpFormatter) -> None:
"""
Writes the help text to the formatter if it exists.
:param ctx:
:param formatter:
"""
if not self.help:
return
text = inspect.cleandoc(self.help or '').partition('\x0c')[0]
if getattr(self, "deprecated", False):
text = f"(Deprecated) {text}"
formatter.write('\n')
formatter.write(indent(text, " "))
formatter.write('\n')
class RawHelpCommand(RawHelpMixin, click.Command): # lgtm [py/conflicting-attributes]
"""
Subclass of :class:`click.Command` which leaves the help text unformatted.
.. versionadded:: 0.8.0
"""
class RawHelpGroup(RawHelpMixin, click.Group): # lgtm [py/conflicting-attributes]
"""
Subclass of :class:`click.Group` which leaves the help text unformatted.
.. versionadded:: 0.8.0
"""
class MarkdownHelpMixin:
"""
Mixin class for :class:`click.Command` and :class:`click.Group` which treats the help text as markdown
and prints a rendered representation.
.. seealso:: :class:`~.MarkdownHelpCommand` and :class:`~.MarkdownHelpGroup`
.. tip:: This can be combined with groups such as :class:`~.SuggestionGroup`.
Tested in Gnome Terminal and Terminator (both libVTE-based), and PyCharm.
libVTE has the best support.
PyCharm's support for italics and strikethrough is poor.
Support on Windows is, as expected, poor.
Not tested on other terminals, but contributions are welcome to improve support.
.. versionadded:: 0.8.0
""" # noqa: D400
help: Optional[str] # noqa: A003 # pylint: disable=redefined-builtin
no_args_is_help: bool
_colour: ColourTrilean = None
if TYPE_CHECKING:
def get_params(self, ctx: click.Context) -> List[click.Parameter]:
"""
Returns the list of parameters for this command in the order they should appear in the help page.
:param ctx:
"""
raise NotImplementedError
def make_parser(self, ctx: click.Context) -> click.OptionParser:
"""
Creates the underlying option parser for this command.
:param ctx:
"""
raise NotImplementedError
def format_help_text(self, ctx: click.Context, formatter: click.formatting.HelpFormatter) -> None:
"""
Writes the help text to the formatter if it exists.
:param ctx:
:param formatter:
"""
if not self.help:
return
text = inspect.cleandoc(self.help or '').partition('\x0c')[0]
if getattr(self, "deprecated", False):
text = f"(Deprecated) {text}"
doc = block_token.Document(text)
with TerminalRenderer() as renderer:
rendered_doc = indent(renderer.render(doc).strip(), " ")
if resolve_color_default(self._colour) is False:
# Also remove 'COMBINING LONG STROKE OVERLAY', used for strikethrough.
rendered_doc = strip_ansi(rendered_doc).replace('̶', '')
formatter.write('\n')
formatter.write(rendered_doc)
formatter.write('\n')
def parse_help_args(obj: MarkdownHelpMixin, ctx: click.Context, args: List[str]) -> List[str]:
"""
Helper function for markdown help classes to parse the given arguments
and modify the context as necessary.
.. versionadded:: 1.1.0
:param obj:
:param ctx:
:param args:
""" # noqa: D400
# This is necessary to parse any --colour/--no-colour commands before generating the help,
# to ensure the option is honoured.
if not args and obj.no_args_is_help and not ctx.resilient_parsing:
click.echo(ctx.get_help(), color=ctx.color)
ctx.exit()
parser = obj.make_parser(ctx)
opts, args, param_order = parser.parse_args(args=args)
obj._colour = opts.get("colour", ctx.color)
for param in iter_params_for_processing(param_order, obj.get_params(ctx)):
value, args = param.handle_parse_result(ctx, opts, args)
if args and not ctx.allow_extra_args and not ctx.resilient_parsing:
args_string = DelimitedList(map(make_str, args))
ctx.fail(f"Got unexpected extra {_argument(len(args))} ({args_string: })")
ctx.args = args
return args
class MarkdownHelpCommand(MarkdownHelpMixin, click.Command): # lgtm [py/conflicting-attributes]
"""
Subclass of :class:`click.Command` which treats the help text as markdown
and prints a rendered representation.
Tested in Gnome Terminal and Terminator (both libVTE-based), and PyCharm.
libVTE has the best support.
PyCharm's support for italics and strikethrough is poor.
Support on Windows is, as expected, poor.
Not tested on other terminals, but contributions are welcome to improve support.
.. versionadded:: 0.8.0
""" # noqa: D400
def parse_args(self, ctx: click.Context, args: List[str]) -> List[str]:
"""
Parse the given arguments and modify the context as necessary.
:param ctx:
:param args:
"""
return parse_help_args(self, ctx, args)
class MarkdownHelpGroup(MarkdownHelpMixin, click.Group): # lgtm [py/conflicting-attributes]
"""
Subclass of :class:`click.Group` which treats the help text as markdown
and prints a rendered representation.
Tested in Gnome Terminal and Terminator (both libVTE-based), and PyCharm.
libVTE has the best support.
PyCharm's support for italics and strikethrough is poor.
Support on Windows is, as expected, poor.
Not tested on other terminals, but contributions are welcome to improve support.
.. versionadded:: 0.8.0
""" # noqa: D400
def parse_args(self, ctx: click.Context, args: List[str]) -> List[str]:
"""
Parse the given arguments and modify the context as necessary.
:param ctx:
:param args:
"""
# This is necessary to parse any --colour/--no-colour commands before generating the help,
# to ensure the option is honoured.
if not args and self.no_args_is_help and not ctx.resilient_parsing:
click.echo(ctx.get_help(), color=ctx.color)
ctx.exit()
rest = parse_help_args(self, ctx, args)
if self.chain:
ctx.protected_args = rest
ctx.args = []
elif rest:
ctx.protected_args, ctx.args = rest[:1], rest[1:]
return ctx.args
class ContextInheritingGroup(click.Group):
"""
Subclass of :class:`click.Group` whose children inherit its :attr:`~click.BaseCommand.context_settings`.
The group's commands can be given different context settings by passing the
``context_settings`` keyword argument to :meth:`~.command` and :meth:`~.group` as normal.
.. versionadded:: 1.1.0
.. autosummary-widths:: 5/16
"""
def command(self, *args, **kwargs) -> Callable[[Callable[..., Any]], click.Command]: # type: ignore[override]
"""
A shortcut decorator for declaring and attaching a command to the group.
This takes the same arguments as :func:`click.command` but
immediately registers the created command with this instance
by calling into :meth:`click.Group.add_command`.
"""
kwargs.setdefault("context_settings", self.context_settings)
return super().command(*args, **kwargs)
def group(self, *args, **kwargs) -> Callable[[Callable[..., Any]], click.Group]: # type: ignore[override]
"""
A shortcut decorator for declaring and attaching a group to the group.
This takes the same arguments as :func:`click.group` but
immediately registers the created group with this instance
by calling into :meth:`click.Group.add_command`.
"""
kwargs.setdefault("context_settings", self.context_settings)
return super().group(*args, **kwargs)
class SuggestionGroup(ContextInheritingGroup):
"""
Subclass of :class:`click.Group` which suggests the most similar command if the command is not found.
.. versionadded 0.2.0
.. versionchanged:: 0.8.0
Moved to :mod:`consolekit.commands`.
.. versionchanged:: 1.1.0
Now inherits from :class:`~.ContextInheritingGroup`
"""
def resolve_command(
self,
ctx: click.Context,
args: List[str],
) -> Tuple[str, click.Command, List[str]]:
"""
Resolve the requested command belonging to this group, and print a suggestion if it can't be found.
:param ctx:
:param args:
:return: The name of the matching command,
the :class:`click.Command` object itself,
and any remaining arguments.
"""
cmd_name = make_str(args[0])
original_cmd_name = cmd_name
# Get the command
cmd = self.get_command(ctx, cmd_name)
# If we can't find the command but there is a normalization
# function available, we try with that one.
if cmd is None and ctx.token_normalize_func is not None:
cmd_name = ctx.token_normalize_func(cmd_name)
cmd = self.get_command(ctx, cmd_name)
# If we don't find the command we want to show an error message
# to the user that it was not provided.
# However, there is something else we should do:
# if the first argument looks like an option we want to kick off parsing again
# for arguments to resolve things like --help which now should go to the main place.
if cmd is None and not ctx.resilient_parsing:
if split_opt(cmd_name)[0]:
self.parse_args(ctx, ctx.args)
closest = difflib.get_close_matches(original_cmd_name, self.commands, n=1)
message = [f"No such command '{original_cmd_name}'."]
if closest:
message.append(f"The most similar command is {closest[0]!r}.")
ctx.fail('\n'.join(message))
# TODO: cmd here is Optional[click.Command], typeshed says it should be just click.Command
# I think typeshed is wrong.
# https://github.com/python/typeshed/blob/484c014665cdf071b292dd9630f207c03e111895/third_party/2and3/click/core.pyi#L171
return cmd_name, cmd, args[1:] # type: ignore[return-value]
|