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
|
# stdlib
from typing import Dict, List, Type
# 3rd party
import pytest
from coincidence.regressions import AdvancedDataRegressionFixture
from domdf_python_tools.paths import PathPlus
# this package
import dom_toml
from dom_toml.decoder import TomlPureDecoder
from dom_toml.parser import TOML_TYPES, AbstractConfigParser
class PEP621Parser(AbstractConfigParser):
"""
Parser for :pep:`621` metadata from ``pyproject.toml``.
"""
defaults = {
"description": None,
}
factories = {
"keywords": list,
"classifiers": list,
"urls": dict,
"scripts": dict,
"gui_scripts": dict,
"entry_points": dict,
"dependencies": list,
"optional-dependencies": dict,
}
def parse_description(self, config: Dict[str, TOML_TYPES]) -> str:
"""
Parse the `description <https://www.python.org/dev/peps/pep-0621/#description>`_ key.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
description = config["description"]
self.assert_type(description, str, ["project", "description"])
return description
def parse_keywords(self, config: Dict[str, TOML_TYPES]) -> List[str]:
"""
Parse the `keywords <https://www.python.org/dev/peps/pep-0621/#keywords>`_ key.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
parsed_keywords = set()
for idx, keyword in enumerate(config["keywords"]):
self.assert_indexed_type(keyword, str, ["project", "keywords"], idx=idx)
parsed_keywords.add(keyword)
return sorted(parsed_keywords)
def parse_classifiers(self, config: Dict[str, TOML_TYPES]) -> List[str]:
"""
Parse the `classifiers <https://www.python.org/dev/peps/pep-0621/#classifiers>`_ key.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
parsed_classifiers = set()
for idx, keyword in enumerate(config["classifiers"]):
self.assert_indexed_type(keyword, str, ["project", "classifiers"], idx=idx)
parsed_classifiers.add(keyword)
return sorted(parsed_classifiers)
def parse_urls(self, config: Dict[str, TOML_TYPES]) -> Dict[str, str]:
"""
Parse the `urls <https://www.python.org/dev/peps/pep-0621/#urls>`_ table.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
parsed_urls = {}
project_urls = config["urls"]
self.assert_type(project_urls, dict, ["project", "urls"])
for category, url in project_urls.items():
self.assert_value_type(url, str, ["project", "urls", category])
parsed_urls[category] = url
return parsed_urls
def parse_scripts(self, config: Dict[str, TOML_TYPES]) -> Dict[str, str]:
"""
Parse the `scripts <https://www.python.org/dev/peps/pep-0621/#entry-points>`_ table.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
scripts = config["scripts"]
self.assert_type(scripts, dict, ["project", "scripts"])
for name, func in scripts.items():
self.assert_value_type(func, str, ["project", "scripts", name])
return scripts
def parse_gui_scripts(self, config: Dict[str, TOML_TYPES]) -> Dict[str, str]:
"""
Parse the `gui-scripts <https://www.python.org/dev/peps/pep-0621/#entry-points>`_ table.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
gui_scripts = config["gui-scripts"]
self.assert_type(gui_scripts, dict, ["project", "gui-scripts"])
for name, func in gui_scripts.items():
self.assert_value_type(func, str, ["project", "gui-scripts", name])
return gui_scripts
def parse_entry_points(self, config: Dict[str, TOML_TYPES]) -> Dict[str, str]:
"""
Parse the `entry-points <https://www.python.org/dev/peps/pep-0621/#entry-points>`_ table.
:param config: The unparsed TOML config for the ``[project]`` table.
"""
entry_points = config["entry-points"]
self.assert_type(entry_points, dict, ["project", "entry-points"])
for group, sub_table in entry_points.items():
self.assert_value_type(sub_table, dict, ["project", "entry-points", group])
for name, func in sub_table.items():
self.assert_value_type(func, str, ["project", "entry-points", group, name])
return entry_points
def parse_dependencies(self, config: Dict[str, TOML_TYPES]) -> List[str]:
"""
Parse the
`dependencies <https://www.python.org/dev/peps/pep-0621/#dependencies-optional-dependencies>`_ key.
:param config: The unparsed TOML config for the ``[project]`` table.
""" # noqa: D400
parsed_dependencies = set()
for idx, keyword in enumerate(config["dependencies"]):
self.assert_indexed_type(keyword, str, ["project", "dependencies"], idx=idx)
parsed_dependencies.add(keyword)
return sorted(parsed_dependencies)
@property
def keys(self) -> List[str]:
"""
The keys to parse from the TOML file.
"""
return [
"name",
"description",
"keywords",
"classifiers",
"urls",
"scripts",
"gui-scripts",
"dependencies",
]
MINIMAL_CONFIG = '[project]\nname = "spam"\nversion = "2020.0.0"'
KEYWORDS = f"""\
{MINIMAL_CONFIG}
keywords = ["egg", "bacon", "sausage", "tomatoes", "Lobster Thermidor"]
"""
CLASSIFIERS = f"""\
{MINIMAL_CONFIG}
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python"
]
"""
DEPENDENCIES = f"""\
{MINIMAL_CONFIG}
dependencies = [
"httpx",
"gidgethub[httpx]>4.0.0",
"django>2.1; os_name != 'nt'",
"django>2.0; os_name == 'nt'"
]
"""
URLS = f"""\
{MINIMAL_CONFIG}
[project.urls]
homepage = "example.com"
documentation = "readthedocs.org"
repository = "github.com"
changelog = "github.com/me/spam/blob/master/CHANGELOG.md"
"""
UNICODE = f"""\
{MINIMAL_CONFIG}
description = "Factory ⸻ A code generator 🏭"
authors = [{{name = "Łukasz Langa"}}]
"""
@pytest.mark.parametrize(
"config, expects, match",
[
pytest.param(
f'{MINIMAL_CONFIG}\nkeywords = [1, 2, 3, 4, 5]',
TypeError,
r"Invalid type for 'project.keywords\[0\]': expected <class 'str'>, got <class 'int'>",
id="keywords_wrong_type"
),
pytest.param(
f'{MINIMAL_CONFIG}\ndescription = [1, 2, 3, 4, 5]',
TypeError,
r"Invalid type for 'project.description': expected <class 'str'>, got <class 'list'>",
id="description_wrong_type"
),
pytest.param(
f'{MINIMAL_CONFIG}\ndescription = 12345',
TypeError,
r"Invalid type for 'project.description': expected <class 'str'>, got <class 'int'>",
id="description_wrong_type"
),
pytest.param(
f'{MINIMAL_CONFIG}\nclassifiers = [1, 2, 3, 4, 5]',
TypeError,
r"Invalid type for 'project.classifiers\[0\]': expected <class 'str'>, got <class 'int'>",
id="classifiers_wrong_type"
),
pytest.param(
f'{MINIMAL_CONFIG}\ndependencies = [1, 2, 3, 4, 5]',
TypeError,
r"Invalid type for 'project.dependencies\[0\]': expected <class 'str'>, got <class 'int'>",
id="dependencies_wrong_type"
),
pytest.param(
f'{MINIMAL_CONFIG}\nurls = {{foo = 1234}}',
TypeError,
r"Invalid value type for 'project.urls.foo': expected <class 'str'>, got <class 'int'>",
id="urls_wrong_type"
),
]
)
def test_parse_config_errors(config: str, expects: Type[Exception], match: str, tmp_pathplus: PathPlus):
with pytest.raises(expects, match=match):
PEP621Parser().parse(dom_toml.loads(config)["project"], set_defaults=True)
@pytest.mark.parametrize(
"toml_config",
[
pytest.param(MINIMAL_CONFIG, id="minimal"),
pytest.param(f'{MINIMAL_CONFIG}\ndescription = "Lovely Spam! Wonderful Spam!"', id="description"),
pytest.param(KEYWORDS, id="keywords"),
pytest.param(CLASSIFIERS, id="classifiers"),
pytest.param(DEPENDENCIES, id="dependencies"),
pytest.param(URLS, id="urls"),
pytest.param(UNICODE, id="unicode"),
]
)
def test_parse_valid_config(
toml_config: str,
tmp_pathplus: PathPlus,
advanced_data_regression: AdvancedDataRegressionFixture,
):
(tmp_pathplus / "pyproject.toml").write_clean(toml_config)
config = PEP621Parser().parse(
dom_toml.loads(toml_config, decoder=TomlPureDecoder)["project"],
set_defaults=True,
)
advanced_data_regression.check(config)
|