|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import sys |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +if sys.version_info >= (3, 11): # pragma: >=3.11 cover |
| 9 | + import tomllib |
| 10 | +else: # pragma: <3.11 cover |
| 11 | + import tomli as tomllib |
| 12 | + |
5 | 13 | import os |
6 | 14 | from argparse import ( |
7 | 15 | ArgumentDefaultsHelpFormatter, |
|
15 | 23 |
|
16 | 24 | from packaging.version import Version |
17 | 25 |
|
18 | | -from .config import DEFAULT_INDENT, DEFAULT_MAX_SUPPORTED_PYTHON, DEFAULT_MIN_SUPPORTED_PYTHON, Config |
19 | | - |
20 | 26 |
|
21 | 27 | class PyProjectFmtNamespace(Namespace): |
22 | 28 | """Options for pyproject-fmt tool.""" |
23 | 29 |
|
24 | 30 | inputs: list[Path] |
25 | 31 | stdout: bool |
26 | 32 | check: bool |
| 33 | + |
| 34 | + column_width: int |
27 | 35 | indent: int |
28 | 36 | keep_full_version: bool |
29 | 37 | max_supported_python: Version |
30 | 38 | min_supported_python: Version |
31 | 39 |
|
| 40 | + |
| 41 | +@dataclass(frozen=True) |
| 42 | +class Config: |
| 43 | + """Configuration flags for the formatting.""" |
| 44 | + |
| 45 | + pyproject_toml: Path |
| 46 | + stdout: bool # push to standard out |
| 47 | + check: bool # check only |
| 48 | + |
| 49 | + column_width: int #: maximum column width |
| 50 | + indent: int #: indentation to apply |
| 51 | + keep_full_version: bool #: whether to keep full dependency versions |
| 52 | + max_supported_python: Version #: the maximum supported Python version |
| 53 | + min_supported_python: Version #: the minimum supported Python version |
| 54 | + |
32 | 55 | @property |
33 | | - def configs(self) -> list[Config]: |
34 | | - """:return: configurations""" |
35 | | - return [ |
36 | | - Config.from_file( |
37 | | - filename=toml, |
38 | | - indent=self.indent, |
39 | | - keep_full_version=self.keep_full_version, |
40 | | - max_supported_python=self.max_supported_python, |
41 | | - min_supported_python=self.min_supported_python, |
42 | | - ) |
43 | | - for toml in self.inputs |
44 | | - ] |
| 56 | + def toml(self) -> str: |
| 57 | + """:return: the toml files content""" |
| 58 | + return self.pyproject_toml.read_text(encoding="utf-8") |
45 | 59 |
|
46 | 60 |
|
47 | 61 | def pyproject_toml_path_creator(argument: str) -> Path: |
@@ -88,43 +102,84 @@ def _build_cli() -> ArgumentParser: |
88 | 102 | group.add_argument("--check", action="store_true", help=msg) |
89 | 103 | msg = "keep full dependency versions. For example do not change version 1.0.0 to 1" |
90 | 104 | parser.add_argument("--keep-full-version", action="store_true", help=msg) |
| 105 | + parser.add_argument( |
| 106 | + "--column-width", |
| 107 | + type=int, |
| 108 | + default=1, |
| 109 | + help="max column width in the file", |
| 110 | + ) |
91 | 111 | parser.add_argument( |
92 | 112 | "--indent", |
93 | 113 | type=int, |
94 | | - default=DEFAULT_INDENT, |
| 114 | + default=2, |
95 | 115 | help="number of spaces to indent", |
96 | 116 | ) |
97 | 117 | parser.add_argument( |
98 | 118 | "--min-supported-python", |
99 | 119 | type=Version, |
100 | | - default=DEFAULT_MIN_SUPPORTED_PYTHON, |
| 120 | + default="3.8", |
101 | 121 | help="latest Python version the project supports (e.g. 3.8)", |
102 | 122 | ) |
103 | 123 | parser.add_argument( |
104 | 124 | "--max-supported-python", |
105 | 125 | type=Version, |
106 | | - default=DEFAULT_MAX_SUPPORTED_PYTHON, |
| 126 | + default="3.12", |
107 | 127 | help="latest Python version the project supports (e.g. 3.13)", |
108 | 128 | ) |
109 | 129 | msg = "pyproject.toml file(s) to format" |
110 | 130 | parser.add_argument("inputs", nargs="+", type=pyproject_toml_path_creator, help=msg) |
111 | 131 | return parser |
112 | 132 |
|
113 | 133 |
|
114 | | -def cli_args(args: Sequence[str]) -> PyProjectFmtNamespace: |
| 134 | +def cli_args(args: Sequence[str]) -> list[Config]: |
115 | 135 | """ |
116 | 136 | Load the tools options. |
117 | 137 |
|
118 | 138 | :param args: CLI arguments |
119 | 139 | :return: the parsed options |
120 | 140 | """ |
121 | 141 | parser = _build_cli() |
122 | | - result = PyProjectFmtNamespace() |
123 | | - parser.parse_args(namespace=result, args=args) |
124 | | - return result |
| 142 | + opt = PyProjectFmtNamespace() |
| 143 | + parser.parse_args(namespace=opt, args=args) |
| 144 | + res = [] |
| 145 | + for pyproject_toml in opt.inputs: |
| 146 | + column_width = opt.column_width |
| 147 | + indent = opt.indent |
| 148 | + keep_full_version = opt.keep_full_version |
| 149 | + max_supported_python = opt.max_supported_python |
| 150 | + min_supported_python = opt.min_supported_python |
| 151 | + with pyproject_toml.open("rb") as file_handler: |
| 152 | + config = tomllib.load(file_handler) |
| 153 | + if "tool" in config and "pyproject-fmt" in config["tool"]: |
| 154 | + for key, entry in config["tool"]["pyproject-fmt"].items(): |
| 155 | + if key == "column_width": |
| 156 | + column_width = int(entry) |
| 157 | + elif key == "indent": |
| 158 | + indent = int(entry) |
| 159 | + elif key == "keep_full_version": |
| 160 | + keep_full_version = bool(entry) |
| 161 | + elif key == "max_supported_python": |
| 162 | + max_supported_python = Version(entry) |
| 163 | + elif key == "min_supported_python": # pragma: no branch |
| 164 | + min_supported_python = Version(entry) |
| 165 | + res.append( |
| 166 | + Config( |
| 167 | + pyproject_toml=pyproject_toml, |
| 168 | + stdout=opt.stdout, |
| 169 | + check=opt.check, |
| 170 | + column_width=column_width, |
| 171 | + indent=indent, |
| 172 | + keep_full_version=keep_full_version, |
| 173 | + max_supported_python=max_supported_python, |
| 174 | + min_supported_python=min_supported_python, |
| 175 | + ) |
| 176 | + ) |
| 177 | + |
| 178 | + return res |
125 | 179 |
|
126 | 180 |
|
127 | 181 | __all__ = [ |
| 182 | + "Config", |
128 | 183 | "PyProjectFmtNamespace", |
129 | 184 | "cli_args", |
130 | 185 | ] |
0 commit comments