• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

abravalheri / validate-pyproject / 5226968556240896

pending completion
5226968556240896

push

cirrus-ci

GitHub
Typo: validate-project => validate-pyproject (#78)

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.95
/src/validate_pyproject/cli.py
1
# The code in this module is based on a similar code from `ini2toml` (originally
2
# published under the MPL-2.0 license)
3
# https://github.com/abravalheri/ini2toml/blob/49897590a9254646434b7341225932e54f9626a3/LICENSE.txt
4

5
import argparse
8✔
6
import io
8✔
7
import json
8✔
8
import logging
8✔
9
import sys
8✔
10
from contextlib import contextmanager
8✔
11
from itertools import chain
8✔
12
from textwrap import dedent, wrap
8✔
13
from typing import (
8✔
14
    Callable,
15
    Dict,
16
    Iterator,
17
    List,
18
    NamedTuple,
19
    Sequence,
20
    Tuple,
21
    Type,
22
    TypeVar,
23
)
24

25
from . import __version__
8✔
26
from .api import Validator
8✔
27
from .errors import ValidationError
8✔
28
from .plugins import PluginWrapper
8✔
29
from .plugins import list_from_entry_points as list_plugins_from_entry_points
8✔
30

31
_logger = logging.getLogger(__package__)
8✔
32
T = TypeVar("T", bound=NamedTuple)
8✔
33

34

35
try:  # pragma: no cover
36
    if sys.version_info[:2] >= (3, 11):
37
        from tomllib import TOMLDecodeError, loads
38
    else:
39
        from tomli import TOMLDecodeError, loads
40
except ImportError:  # pragma: no cover
41
    try:
42
        from toml import TomlDecodeError as TOMLDecodeError  # type: ignore
43
        from toml import loads  # type: ignore
44
    except ImportError as ex:
45
        raise ImportError("Please install `tomli` (TOML parser)") from ex
46

47
_REGULAR_EXCEPTIONS = (ValidationError, TOMLDecodeError)
8✔
48

49

50
@contextmanager
8✔
51
def critical_logging():
6 all except 5226968556240896.2 and 5226968556240896.4 ✔
52
    """Make sure the logging level is set even before parsing the CLI args"""
53
    try:
8✔
54
        yield
8✔
55
    except Exception:  # pragma: no cover
56
        if "-vv" in sys.argv or "--very-verbose" in sys.argv:
57
            setup_logging(logging.DEBUG)
58
        raise
59

60

61
META: Dict[str, dict] = {
8✔
62
    "version": dict(
63
        flags=("-V", "--version"),
64
        action="version",
65
        version=f"{__package__} {__version__}",
66
    ),
67
    "input_file": dict(
68
        dest="input_file",
69
        nargs="*",
70
        default=[argparse.FileType("r")("-")],
71
        type=argparse.FileType("r"),
72
        help="TOML file to be verified (`stdin` by default)",
73
    ),
74
    "enable": dict(
75
        flags=("-E", "--enable-plugins"),
76
        nargs="+",
77
        default=(),
78
        dest="enable",
79
        metavar="PLUGINS",
80
        help="Enable ONLY the given plugins (ALL plugins are enabled by default).",
81
    ),
82
    "disable": dict(
83
        flags=("-D", "--disable-plugins"),
84
        nargs="+",
85
        dest="disable",
86
        default=(),
87
        metavar="PLUGINS",
88
        help="Enable ALL plugins, EXCEPT the ones given.",
89
    ),
90
    "verbose": dict(
91
        flags=("-v", "--verbose"),
92
        dest="loglevel",
93
        action="store_const",
94
        const=logging.INFO,
95
        help="set logging level to INFO",
96
    ),
97
    "very_verbose": dict(
98
        flags=("-vv", "--very-verbose"),
99
        dest="loglevel",
100
        action="store_const",
101
        const=logging.DEBUG,
102
        help="set logging level to DEBUG",
103
    ),
104
    "dump_json": dict(
105
        flags=("--dump-json",),
106
        action="store_true",
107
        help="Print the JSON equivalent to the given TOML",
108
    ),
109
}
110

111

112
class CliParams(NamedTuple):
8✔
113
    input_file: List[io.TextIOBase]
8✔
114
    plugins: List[PluginWrapper]
8✔
115
    loglevel: int = logging.WARNING
8✔
116
    dump_json: bool = False
8✔
117

118

119
def __meta__(plugins: Sequence[PluginWrapper]) -> Dict[str, dict]:
8✔
120
    """'Hyper parameters' to instruct :mod:`argparse` how to create the CLI"""
121
    meta = {k: v.copy() for k, v in META.items()}
8✔
122
    meta["enable"]["choices"] = set([p.tool for p in plugins])
8✔
123
    return meta
8✔
124

125

126
@critical_logging()
8✔
127
def parse_args(
8✔
128
    args: Sequence[str],
129
    plugins: Sequence[PluginWrapper],
130
    description: str = "Validate a given TOML file",
131
    get_parser_spec: Callable[[Sequence[PluginWrapper]], Dict[str, dict]] = __meta__,
132
    params_class: Type[T] = CliParams,  # type: ignore[assignment]
133
) -> T:
134
    """Parse command line parameters
135

136
    Args:
137
      args: command line parameters as list of strings (for example  ``["--help"]``).
138

139
    Returns: command line parameters namespace
140
    """
141
    epilog = ""
8✔
142
    if plugins:
8✔
143
        epilog = f"The following plugins are available:\n\n{plugins_help(plugins)}"
8✔
144

145
    parser = argparse.ArgumentParser(
8✔
146
        description=description, epilog=epilog, formatter_class=Formatter
147
    )
148
    for cli_opts in get_parser_spec(plugins).values():
8✔
149
        parser.add_argument(*cli_opts.pop("flags", ()), **cli_opts)
8✔
150

151
    parser.set_defaults(loglevel=logging.WARNING)
8✔
152
    params = vars(parser.parse_args(args))
8✔
153
    enabled = params.pop("enable", ())
8✔
154
    disabled = params.pop("disable", ())
8✔
155
    params["plugins"] = select_plugins(plugins, enabled, disabled)
8✔
156
    return params_class(**params)
8✔
157

158

159
def select_plugins(
8✔
160
    plugins: Sequence[PluginWrapper],
161
    enabled: Sequence[str] = (),
162
    disabled: Sequence[str] = (),
163
) -> List[PluginWrapper]:
164
    available = list(plugins)
8✔
165
    if enabled:
8✔
166
        available = [p for p in available if p.tool in enabled]
8✔
167
    if disabled:
8✔
168
        available = [p for p in available if p.tool not in disabled]
8✔
169
    return available
8✔
170

171

172
def setup_logging(loglevel: int):
8✔
173
    """Setup basic logging
174

175
    Args:
176
      loglevel: minimum loglevel for emitting messages
177
    """
178
    logformat = "[%(levelname)s] %(message)s"
8✔
179
    logging.basicConfig(level=loglevel, stream=sys.stderr, format=logformat)
8✔
180

181

182
@contextmanager
8✔
183
def exceptions2exit():
6 all except 5226968556240896.2 and 5226968556240896.4 ✔
184
    try:
8✔
185
        yield
8✔
186
    except _ExceptionGroup as group:
8✔
187
        for prefix, ex in group:
8✔
188
            print(prefix)
8✔
189
            _logger.error(str(ex) + "\n")
8✔
190
        raise SystemExit(1)
8✔
191
    except _REGULAR_EXCEPTIONS as ex:
8✔
192
        _logger.error(str(ex))
8✔
193
        raise SystemExit(1)
8✔
194
    except Exception as ex:  # pragma: no cover
195
        _logger.error(f"{ex.__class__.__name__}: {ex}\n")
196
        _logger.debug("Please check the following information:", exc_info=True)
197
        raise SystemExit(1)
198

199

200
def run(args: Sequence[str] = ()):
8✔
201
    """Wrapper allowing :obj:`Translator` to be called in a CLI fashion.
202

203
    Instead of returning the value from :func:`Translator.translate`, it prints the
204
    result to the given ``output_file`` or ``stdout``.
205

206
    Args:
207
      args (List[str]): command line parameters as list of strings
208
          (for example  ``["--verbose", "setup.cfg"]``).
209
    """
210
    args = args or sys.argv[1:]
8✔
211
    plugins: List[PluginWrapper] = list_plugins_from_entry_points()
8✔
212
    params: CliParams = parse_args(args, plugins)
8✔
213
    setup_logging(params.loglevel)
8✔
214
    validator = Validator(plugins=params.plugins)
8✔
215

216
    exceptions = _ExceptionGroup()
8✔
217
    for file in params.input_file:
8✔
218
        try:
8✔
219
            toml_equivalent = loads(file.read())
8✔
220
            validator(toml_equivalent)
8✔
221
            if params.dump_json:
8✔
222
                print(json.dumps(toml_equivalent, indent=2))
8✔
223
            else:
224
                print(f"Valid {_format_file(file)}")
8✔
225
        except _REGULAR_EXCEPTIONS as ex:
8✔
226
            exceptions.add(f"Invalid {_format_file(file)}", ex)
8✔
227

228
    exceptions.raise_if_any()
8✔
229

230
    return 0
8✔
231

232

233
main = exceptions2exit()(run)
8✔
234

235

236
class Formatter(argparse.RawTextHelpFormatter):
8✔
237
    # Since the stdlib does not specify what is the signature we need to implement in
238
    # order to create our own formatter, we are left no choice other then overwrite a
239
    # "private" method considered to be an implementation detail.
240

241
    def _split_lines(self, text, width):
8✔
242
        return list(chain.from_iterable(wrap(x, width) for x in text.splitlines()))
8✔
243

244

245
def plugins_help(plugins: Sequence[PluginWrapper]) -> str:
8✔
246
    return "\n".join(_format_plugin_help(p) for p in plugins)
8✔
247

248

249
def _flatten_str(text: str) -> str:
8✔
250
    text = " ".join(x.strip() for x in dedent(text).splitlines()).strip()
×
251
    text = text.rstrip(".,;").strip()
×
252
    return (text[0].lower() + text[1:]).strip()
×
253

254

255
def _format_plugin_help(plugin: PluginWrapper) -> str:
8✔
256
    help_text = plugin.help_text
8✔
257
    help_text = f": {_flatten_str(help_text)}" if help_text else ""
8✔
258
    return f'* "{plugin.tool}"{help_text}'
8✔
259

260

261
def _format_file(file: io.TextIOBase) -> str:
8✔
262
    if hasattr(file, "name") and file.name:  # type: ignore[attr-defined]
8✔
263
        return f"file: {file.name}"  # type: ignore[attr-defined]
8✔
264
    return "file"  # pragma: no cover
265

266

267
class _ExceptionGroup(Exception):
8✔
268
    def __init__(self):
8✔
269
        self._members: List[Tuple[str, Exception]] = []
8✔
270
        super().__init__()
8✔
271

272
    def add(self, prefix: str, ex: Exception):
8✔
273
        self._members.append((prefix, ex))
8✔
274

275
    def __iter__(self) -> Iterator[Tuple[str, Exception]]:
8✔
276
        return iter(self._members)
8✔
277

278
    def raise_if_any(self):
8✔
279
        number = len(self._members)
8✔
280
        if number == 1:
8✔
281
            print(self._members[0][0])
8✔
282
            raise self._members[0][1]
8✔
283
        if number > 0:
8✔
284
            raise self
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc