-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_profile.py
More file actions
228 lines (192 loc) · 8.31 KB
/
project_profile.py
File metadata and controls
228 lines (192 loc) · 8.31 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
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
from __future__ import annotations
import json
import os
from pathlib import Path
from typing import Any
PROFILE_FORMAT = "winstorepackager-project-v1"
SCHEMA_VERSION = 1
def split_capabilities(value: str | list[str] | None) -> list[str]:
if value is None:
return []
if isinstance(value, list):
items = value
else:
items = str(value).split(",")
return [item.strip() for item in items if item and item.strip()]
def join_capabilities(values: list[str] | None) -> str:
if not values:
return ""
return ", ".join(item.strip() for item in values if item and item.strip())
def validate_project_profile(data: Any) -> list[str]:
errors: list[str] = []
if not isinstance(data, dict):
return ["Projektprofil muss ein JSON-Objekt sein."]
if data.get("format") != PROFILE_FORMAT:
errors.append(f"Unbekanntes Format: {data.get('format')!r}")
if data.get("schema_version") != SCHEMA_VERSION:
errors.append(f"Nicht unterstützte Schema-Version: {data.get('schema_version')!r}")
for key in ("metadata", "paths", "store", "documents", "settings"):
if key not in data:
errors.append(f"Abschnitt fehlt: {key}")
elif not isinstance(data[key], dict):
errors.append(f"Abschnitt {key} muss ein Objekt sein.")
return errors
def detect_project_root(path_values: list[str]) -> Path | None:
candidates: list[str] = []
for raw_value in path_values:
value = str(raw_value or "").strip()
if not value:
continue
path = Path(value).expanduser()
if not path.is_absolute():
continue
base = path if path.is_dir() else path.parent
candidates.append(str(base.resolve(strict=False)))
if not candidates:
return None
try:
return Path(os.path.commonpath(candidates))
except ValueError:
# Mixed-drive Windows paths have no common root; keep absolute paths instead.
return None
def serialize_project_profile(state: dict[str, Any]) -> dict[str, Any]:
project_root = detect_project_root(
[
state.get("script_path", ""),
state.get("icon_path", ""),
state.get("source_path", ""),
state.get("installer_path", ""),
state.get("output_dir", ""),
]
)
return {
"format": PROFILE_FORMAT,
"schema_version": SCHEMA_VERSION,
"project_root": str(project_root.as_posix()) if project_root else "",
"metadata": {
"app_name": _clean_text(state.get("app_name")),
"publisher_display": _clean_text(state.get("publisher_display")),
"identity_name": _clean_text(state.get("identity_name")),
"version": _clean_text(state.get("version")),
},
"paths": {
"script_path": _serialize_path(state.get("script_path"), project_root),
"icon_path": _serialize_path(state.get("icon_path"), project_root),
"source_path": _serialize_path(state.get("source_path"), project_root),
"installer_path": _serialize_path(state.get("installer_path"), project_root),
"output_dir": _serialize_path(state.get("output_dir"), project_root),
"exe_name": _clean_text(state.get("exe_name")),
},
"store": {
"privacy_url": _clean_text(state.get("privacy_url")),
"support_url": _clean_text(state.get("support_url")),
"capabilities": split_capabilities(state.get("capabilities")),
"category": _clean_text(state.get("category")),
"age_rating": _clean_text(state.get("age_rating")),
"description": _clean_text(state.get("description")),
"changelog": _clean_text(state.get("changelog")),
},
"documents": {
"readme": _clean_text(state.get("readme")),
"license_files": [
_serialize_path(path_value, project_root)
for path_value in state.get("license_files", [])
if str(path_value or "").strip()
],
"license_text_entries": [
_clean_text(text_entry)
for text_entry in state.get("license_text_entries", [])
if _clean_text(text_entry)
],
},
"settings": {
"enable_i18n": bool(state.get("enable_i18n", True)),
},
}
def deserialize_project_profile(
data: dict[str, Any],
*,
profile_path: str | Path | None = None,
) -> dict[str, Any]:
errors = validate_project_profile(data)
if errors:
raise ValueError("\n".join(errors))
profile_dir = Path(profile_path).resolve(strict=False).parent if profile_path else None
project_root = _resolve_project_root(str(data.get("project_root", "")).strip(), profile_dir)
metadata = data.get("metadata", {})
paths = data.get("paths", {})
store = data.get("store", {})
documents = data.get("documents", {})
settings = data.get("settings", {})
return {
"app_name": _clean_text(metadata.get("app_name")),
"publisher_display": _clean_text(metadata.get("publisher_display")),
"identity_name": _clean_text(metadata.get("identity_name")),
"version": _clean_text(metadata.get("version")),
"script_path": _resolve_path(paths.get("script_path"), project_root, profile_dir),
"icon_path": _resolve_path(paths.get("icon_path"), project_root, profile_dir),
"source_path": _resolve_path(paths.get("source_path"), project_root, profile_dir),
"installer_path": _resolve_path(paths.get("installer_path"), project_root, profile_dir),
"output_dir": _resolve_path(paths.get("output_dir"), project_root, profile_dir),
"exe_name": _clean_text(paths.get("exe_name")),
"privacy_url": _clean_text(store.get("privacy_url")),
"support_url": _clean_text(store.get("support_url")),
"capabilities": join_capabilities(store.get("capabilities", [])),
"category": _clean_text(store.get("category")),
"age_rating": _clean_text(store.get("age_rating")),
"description": _clean_text(store.get("description")),
"changelog": _clean_text(store.get("changelog")),
"readme": _clean_text(documents.get("readme")),
"license_files": [
_resolve_path(path_value, project_root, profile_dir)
for path_value in documents.get("license_files", [])
if str(path_value or "").strip()
],
"license_text_entries": [
_clean_text(text_entry)
for text_entry in documents.get("license_text_entries", [])
if _clean_text(text_entry)
],
"enable_i18n": bool(settings.get("enable_i18n", True)),
}
def write_project_profile(path: str | Path, state: dict[str, Any]) -> None:
target = Path(path)
data = serialize_project_profile(state)
target.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
def read_project_profile(path: str | Path) -> dict[str, Any]:
target = Path(path)
data = json.loads(target.read_text(encoding="utf-8"))
return deserialize_project_profile(data, profile_path=target)
def _clean_text(value: Any) -> str:
return str(value or "").strip()
def _serialize_path(raw_value: Any, project_root: Path | None) -> str:
value = _clean_text(raw_value)
if not value:
return ""
path = Path(value).expanduser()
if project_root and path.is_absolute():
try:
return path.resolve(strict=False).relative_to(project_root.resolve(strict=False)).as_posix()
except ValueError:
return path.as_posix()
return value.replace("\\", "/")
def _resolve_project_root(project_root: str, profile_dir: Path | None) -> Path | None:
if not project_root:
return profile_dir
path = Path(project_root)
if path.is_absolute():
return path.resolve(strict=False)
if profile_dir is None:
return path
return (profile_dir / path).resolve(strict=False)
def _resolve_path(raw_value: Any, project_root: Path | None, profile_dir: Path | None) -> str:
value = _clean_text(raw_value)
if not value:
return ""
path = Path(value)
if path.is_absolute():
return str(path)
base_dir = project_root or profile_dir
if base_dir is None:
return value
return str((base_dir / path).resolve(strict=False))