-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
113 lines (90 loc) · 3.6 KB
/
config.py
File metadata and controls
113 lines (90 loc) · 3.6 KB
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
from __future__ import annotations
from pathlib import Path
from tomllib import loads
class PyProjectReader:
def __init__(self, path: Path):
self._path = path
def exists(self) -> bool:
return self._path.exists()
def read(self) -> dict:
if not self.exists():
return {}
with open(self._path) as file:
return loads(file.read())
class ScriptValueParser:
@staticmethod
def parse(script_name: str, script_value: str | list[str] | dict) -> tuple[str | list[str], str]:
if isinstance(script_value, str):
return script_value, ""
elif isinstance(script_value, list):
return script_value, ""
elif isinstance(script_value, dict):
if "command" not in script_value:
return str(script_value), ""
cmd_value = script_value["command"]
description = script_value.get("description", "")
if isinstance(cmd_value, list):
return cmd_value, description
return cmd_value, description
raise ValueError(f"Invalid script value: {script_value}")
class RunScriptSectionReader:
@staticmethod
def get_run_script_section(tool_section: dict) -> dict:
if "uvtask" in tool_section and "run-script" in tool_section["uvtask"]:
return tool_section["uvtask"]["run-script"]
return tool_section.get("run-script", {})
class ScriptLoader:
def __init__(
self,
reader: PyProjectReader,
script_parser: ScriptValueParser,
section_reader: RunScriptSectionReader,
):
self._reader = reader
self._parser = script_parser
self._section_reader = section_reader
def load_scripts(self) -> dict[str, str]:
if not self._reader.exists():
return {}
data = self._reader.read()
tool_section = data.get("tool", {})
run_script = self._section_reader.get_run_script_section(tool_section)
# Convert all to strings for backward compatibility
scripts = {}
for script_name, script_value in run_script.items():
command, _ = self._parser.parse(script_name, script_value)
if isinstance(command, list):
scripts[script_name] = str(command[0]) if command else ""
else:
scripts[script_name] = command
return scripts
def load_scripts_with_descriptions(
self,
) -> tuple[dict[str, str | list[str]], dict[str, str]]:
if not self._reader.exists():
return {}, {}
data = self._reader.read()
tool_section = data.get("tool", {})
run_script = self._section_reader.get_run_script_section(tool_section)
scripts: dict[str, str | list[str]] = {}
descriptions: dict[str, str] = {}
for script_name, script_value in run_script.items():
command, description = self._parser.parse(script_name, script_value)
scripts[script_name] = command
descriptions[script_name] = description
return scripts, descriptions
class VersionLoader:
def __init__(self, reader: PyProjectReader):
self._reader = reader
def get_version(self) -> str:
if not self._reader.exists():
return "unknown"
data = self._reader.read()
return data.get("project", {}).get("version", "unknown")
pyproject_reader = PyProjectReader(Path("pyproject.toml"))
script_loader = ScriptLoader(
reader=pyproject_reader,
script_parser=ScriptValueParser(),
section_reader=RunScriptSectionReader(),
)
version_loader = VersionLoader(pyproject_reader)