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

abravalheri / validate-pyproject / 4900349245390848

pending completion
4900349245390848

Pull #82

cirrus-ci

Anderson Bravalheri
Add test case for extra keys in authors
Pull Request #82: Error-out when extra keys are added to project.authos/maintainers

255 of 261 branches covered (97.7%)

Branch coverage included in aggregate %.

736 of 746 relevant lines covered (98.66%)

10.85 hits per line

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

100.0
/src/validate_pyproject/pre_compile/cli.py
1
import json
11✔
2
import logging
11✔
3
import sys
11✔
4
from functools import partial, wraps
11✔
5
from pathlib import Path
11✔
6
from types import MappingProxyType
11✔
7
from typing import Any, Dict, List, Mapping, NamedTuple, Sequence
11✔
8

9
from .. import cli
11✔
10
from ..plugins import PluginWrapper
11✔
11
from ..plugins import list_from_entry_points as list_plugins_from_entry_points
11✔
12
from . import pre_compile
11✔
13

14
if sys.platform == "win32":  # pragma: no cover
15
    from subprocess import list2cmdline as arg_join
16
elif sys.version_info[:2] >= (3, 8):  # pragma: no cover
17
    from shlex import join as arg_join
18
else:  # pragma: no cover
19
    from shlex import quote
20

21
    def arg_join(args: Sequence[str]) -> str:
22
        return " ".join(quote(x) for x in args)
23

24

25
_logger = logging.getLogger(__package__)
11✔
26

27

28
def JSON_dict(name: str, value: str):
11✔
29
    try:
11✔
30
        return ensure_dict(name, json.loads(value))
11✔
31
    except json.JSONDecodeError as ex:
11✔
32
        raise ValueError(f"Invalid JSON: {value}") from ex
11✔
33

34

35
META: Dict[str, dict] = {
11✔
36
    "output_dir": dict(
37
        flags=("-O", "--output-dir"),
38
        default=".",
39
        type=Path,
40
        help="Path to the directory where the files for embedding will be generated "
41
        "(default: current working directory)",
42
    ),
43
    "main_file": dict(
44
        flags=("-M", "--main-file"),
45
        default="__init__.py",
46
        help="Name of the file that will contain the main `validate` function"
47
        "(default: `%(default)s`)",
48
    ),
49
    "replacements": dict(
50
        flags=("-R", "--replacements"),
51
        default="{}",
52
        type=wraps(JSON_dict)(partial(JSON_dict, "replacements")),
53
        help="JSON string (don't forget to quote) representing a map between strings "
54
        "that should be replaced in the generated files and their replacement, "
55
        "for example: \n"
56
        '-R \'{"from packaging import": "from .._vendor.packaging import"}\'',
57
    ),
58
}
59

60

61
def ensure_dict(name: str, value: Any) -> dict:
11✔
62
    if not isinstance(value, dict):
11✔
63
        msg = f"`{value.__class__.__name__}` given (value = {value!r})."
11✔
64
        raise ValueError(f"`{name}` should be a dict. {msg}")
11✔
65
    return value
11✔
66

67

68
class CliParams(NamedTuple):
11✔
69
    plugins: List[PluginWrapper]
11✔
70
    output_dir: Path = Path(".")
11✔
71
    main_file: str = "__init__.py"
11✔
72
    replacements: Mapping[str, str] = MappingProxyType({})
11✔
73
    loglevel: int = logging.WARNING
11✔
74

75

76
def parser_spec(plugins: Sequence[PluginWrapper]) -> Dict[str, dict]:
11✔
77
    common = ("version", "enable", "disable", "verbose", "very_verbose")
11✔
78
    cli_spec = cli.__meta__(plugins)
11✔
79
    meta = {k: v.copy() for k, v in META.items()}
11✔
80
    meta.update({k: cli_spec[k].copy() for k in common})
11✔
81
    return meta
11✔
82

83

84
def run(args: Sequence[str] = ()):
11✔
85
    args = args if args else sys.argv[1:]
11✔
86
    cmd = f"python -m {__package__} " + arg_join(args)
11✔
87
    plugins = list_plugins_from_entry_points()
11✔
88
    desc = 'Generate files for "pre-compiling" `validate-pyproject`'
11✔
89
    prms = cli.parse_args(args, plugins, desc, parser_spec, CliParams)
11✔
90
    cli.setup_logging(prms.loglevel)
11✔
91
    pre_compile(prms.output_dir, prms.main_file, cmd, prms.plugins, prms.replacements)
11✔
92
    return 0
11✔
93

94

95
main = cli.exceptions2exit()(run)
11✔
96

97

98
if __name__ == "__main__":
99
    main()
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