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
|
from __future__ import annotations
from typing import Any
from hatchling.version.source.plugin.interface import VersionSourceInterface
from .common import PLUGIN_NAME
class ValidationError(Exception):
pass
class _NO:
pass
def extract(
config: dict[str, str], var: str, typ: Any = _NO, optional: Any = _NO
) -> Any:
value = config.get(var, optional)
if value is _NO:
raise ValidationError(f"cannot find variable '{var}' for plugin 'ci'")
try:
new_value = typ(value) if typ is not _NO else value
except Exception as exc:
raise ValidationError(f"cannot convert to {typ=} the {value=}") from exc
return new_value
def get_fixers(txt: str) -> dict[str, str]:
if not isinstance(txt, list):
raise ValidationError("fixers must be list of dicts")
if not all(isinstance(t, list) for t in txt):
raise ValidationError("fixers elements must be lists")
if not all(len(t) == 2 for t in txt):
raise ValidationError("all fixers list elements must be of length 2")
return dict(txt)
class CIVersionSource(VersionSourceInterface):
PLUGIN_NAME = PLUGIN_NAME
def get_version_data(self):
from os import getenv
from pathlib import Path
from hatch_ci import tools
paths = extract(self.config, "paths", typ=tools.list_of_paths)
fixers = extract(self.config, "fixers", typ=get_fixers)
version_file = Path(self.root) / extract(self.config, "version-file")
if not version_file.exists():
raise ValidationError(
f"no 'version-file' key for plugin {self.PLUGIN_NAME}"
)
gdata = tools.process(
version_file, getenv("GITHUB_DUMP"), paths=paths, fixers=fixers
)
return {"version": gdata["version"]}
|