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
|
#!/usr/bin/env python3
#
# cli.py
"""
Command line interface.
.. versionadded:: 0.2.0
.. extras-require:: cli
:pyproject:
"""
#
# Copyright © 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.
#
# stdlib
import functools
import importlib
import re
import sys
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Pattern, TextIO, Type, Union
# 3rd party
import click # nodep
from consolekit.tracebacks import TracebackHandler # nodep
from dom_toml.parser import BadConfigError
from packaging.specifiers import InvalidSpecifier
from packaging.version import InvalidVersion
# this package
from pyproject_parser.utils import PyProjectDeprecationWarning
if sys.version_info >= (3, 7) or TYPE_CHECKING:
# stdlib
from typing import NoReturn
__all__ = ["resolve_class", "ConfigTracebackHandler", "prettify_deprecation_warning"]
class_string_re: Pattern[str] = re.compile("([A-Za-z_][A-Za-z_0-9.]+):([A-Za-z_][A-Za-z_0-9]+)")
def resolve_class(raw_class_string: str, name: str) -> Type:
"""
Resolve the class name for the :option:`-P / --parser-class <pyproject-parser check -P>`
and :option:`-E / --encoder-class <pyproject-parser reformat -E>` options.
:param raw_class_string:
:param name: The name of the option, e.g. ``encoder-class``. Used for error messages.
""" # noqa: D400
class_string_m = class_string_re.match(raw_class_string)
if class_string_m:
module_name, class_name = class_string_m.groups()
else:
raise click.BadOptionUsage(f"{name}", f"Invalid syntax for '--{name}'")
module = importlib.import_module(module_name)
resolved_class: Type = getattr(module, class_name)
return resolved_class
class ConfigTracebackHandler(TracebackHandler):
"""
:class:`consolekit.tracebacks.TracebackHandler` which handles :exc:`dom_toml.parser.BadConfigError`.
"""
has_traceback_option: bool = True
"""
Whether to show the message ``Use '--traceback' to view the full traceback.`` on error.
Enabled by default.
.. versionadded:: 0.5.0 In previous versions this was effectively :py:obj:`False`.
.. versionchanged:: 0.6.0 The message is now indented with four spaces.
"""
@property
def _tb_option_msg(self) -> str:
if self.has_traceback_option:
return "\n Use '--traceback' to view the full traceback."
else:
return ''
def format_exception(self, e: Exception) -> "NoReturn":
"""
Format the exception, showing the explanatory note and documentation link if applicable.
.. versionadded:: 0.6.0
:param e:
"""
msg = [f"{e.__class__.__name__}: {e}"]
if getattr(e, "note", None) is not None:
msg.append(f"\n Note: {e.note}") # type: ignore[attr-defined]
if getattr(e, "documentation", None) is not None:
msg.append(f"\n Documentation: {e.documentation}") # type: ignore[attr-defined]
msg.append(self._tb_option_msg)
self.abort(msg)
def handle_BadConfigError(self, e: "BadConfigError") -> "NoReturn": # noqa: D102
self.format_exception(e)
def handle_ValueError(self, e: ValueError) -> "NoReturn": # noqa: D102
# Also covers InvalidRequirement
self.format_exception(e)
def handle_InvalidSpecifier(self, e: InvalidSpecifier) -> "NoReturn": # noqa: D102
if str(e).startswith("Invalid specifier: "):
e.args = (str(e)[len("Invalid specifier: "):], )
self.format_exception(e)
def handle_InvalidVersion(self, e: InvalidVersion) -> "NoReturn": # noqa: D102
if str(e).startswith("Invalid version: "):
e.args = (str(e)[len("Invalid version: "):], )
self.format_exception(e)
def handle_KeyError(self, e: KeyError) -> "NoReturn": # noqa: D102
self.format_exception(e)
def handle_TypeError(self, e: TypeError) -> "NoReturn": # noqa: D102
self.format_exception(e)
def handle_AttributeError(self, e: AttributeError) -> "NoReturn": # noqa: D102
self.format_exception(e)
def handle_ImportError(self, e: ImportError) -> "NoReturn": # noqa: D102
self.format_exception(e)
def handle_FileNotFoundError(self, e: FileNotFoundError) -> "NoReturn": # noqa: D102
msg = e.strerror
no_such_file = "No such file or directory"
if msg == "The system cannot find the file specified":
msg = no_such_file
if msg == no_such_file:
# Probably from Python itself.
if e.filename is not None:
msg += f": {Path(e.filename).as_posix()!r}"
if e.filename2 is not None:
msg += f" -> {Path(e.filename2).as_posix()!r}"
self.abort(msg)
else:
# Probably from 3rd party code.
super().handle_FileNotFoundError(e)
def prettify_deprecation_warning() -> None:
"""
Catch :class:`PyProjectDeprecationWarnings <.PyProjectDeprecationWarning>`
and format them prettily for the command line.
.. versionadded:: 0.5.0
""" # noqa: D400
orig_showwarning = warnings.showwarning
if orig_showwarning is prettify_deprecation_warning:
return
@functools.wraps(warnings.showwarning)
def showwarning(
message: Union[Warning, str],
category: Type[Warning],
filename: str,
lineno: int,
file: Optional[TextIO] = None,
line: Optional[str] = None,
) -> None:
if isinstance(message, PyProjectDeprecationWarning):
if file is None:
file = sys.stderr
s = f"WARNING: {message.args[0]}\n"
file.write(s)
else:
orig_showwarning(message, category, filename, lineno, file, line)
warnings.showwarning = showwarning
|