-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-home.py
executable file
·252 lines (200 loc) · 7.09 KB
/
gen-home.py
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
#!/usr/bin/env python3
""" gen-home: generate static home page from Packages YAML
- Reads Packages YAML from `PACKAGES_PATH`
- Prepares an HTML output with templates defined in `SRC_DIR`/templates
- Writes and index.html in `DEST_DIR`
- Optionally (`DEBUG`) outputs index to stdout as well
Dependencies:
- PyYAML
- Jinja2
- humanfriendly
"""
from __future__ import annotations
import collections
import gettext
import os
import pathlib
import re
import traceback
import urllib.parse
import humanfriendly
import pycountry
import yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
# we don't NEED cython ext but it's faster so use it if avail.
from yaml import SafeLoader
LanguageDef = collections.namedtuple(
"LanguageDef", ["alpha_3", "alpha_2", "native", "english"]
)
src_dir = pathlib.Path(os.getenv("SRC_DIR", "/src")).expanduser().resolve()
packages_path = (
pathlib.Path(os.getenv("PACKAGES_PATH", "home.yaml")).expanduser().resolve()
)
dest_dir = pathlib.Path(os.getenv("DEST_DIR", "/var/www")).expanduser().resolve()
templates_dir = src_dir.joinpath("templates")
env = Environment(
loader=FileSystemLoader(templates_dir), autoescape=select_autoescape()
)
def format_fsize(size: str | int) -> str:
one_gib = 2**30
one_mib = 2**20
hundred_mib = one_gib / 10
def round_to(size: int, scale: int) -> int:
return (size // scale) * scale
if not str(size).isdigit():
size = humanfriendly.parse_size(str(size))
size = int(size)
if size > one_gib and size >= 100 * one_gib:
size = round_to(size, one_gib)
elif size > one_gib:
size = round_to(size, hundred_mib)
else:
size = round_to(size, one_mib)
try:
return humanfriendly.format_size(
int(size), keep_width=False, binary=True
).replace("iB", "B")
except Exception:
return str(size)
def get_lang_def(alpha_3: str) -> LanguageDef:
"""LanguageDef tuple with parsed/prepared language info"""
try:
language = pycountry.languages.get(alpha_3=alpha_3)
if not language:
raise ValueError("")
except Exception:
return LanguageDef(alpha_3, alpha_3[:2], alpha_3, alpha_3)
try:
alpha_2 = language.alpha_2
except AttributeError:
alpha_2 = alpha_3[:2]
try:
translator = gettext.translation(
"iso639-3", pycountry.LOCALES_DIR, languages=[alpha_2]
)
native = translator.gettext(language.name).title()
except Exception:
native = language.name
return LanguageDef(
alpha_3=alpha_3, alpha_2=alpha_2, native=native, english=language.name
)
env.filters["fsize"] = format_fsize
def normalize(url: str) -> str:
if not url.strip():
return ""
uri = urllib.parse.urlparse(url)
if not uri.scheme and not url.startswith("//"):
url = f"//{url}"
url = re.sub(r"{([a-z]+)-fqdn}", r"\1.{fqdn}", url)
url = url.replace("{fqdn}", Conf.fqdn)
return url
class Conf:
debug: bool = bool(os.getenv("DEBUG", ""))
fqdn: str = ""
name: str = ""
footer_note: str = ""
@classmethod
def from_doc(cls, document):
for key in ("name", "fqdn", "footer_note"):
setattr(cls, key, document.get(key, "--"))
@classmethod
def to_dict(cls):
return {
key: getattr(cls, key) for key in ("debug", "fqdn", "name", "footer_note")
}
class Link(dict):
MANDATORY_FIELDS = ("name", "url")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self["url"] = normalize(self.get("url", ""))
class Reader(dict):
MANDATORY_FIELDS = ("platform", "download_url", "filename", "size")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self["download_url"] = normalize(self.get("download_url", ""))
@property
def name(self) -> str:
return {
"windows": "Windows",
"android": "Android",
"macos": "macOS",
"linux": "Linux",
}.get(self["platform"].lower(), self["platform"])
@property
def icon(self) -> str:
return {
"windows": "windows",
"android": "android",
"macos": "apple",
"linux": "linux",
}.get(self["platform"].lower(), "robot")
class Package(dict):
MANDATORY_FIELDS = ("title", "url")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self["url"] = normalize(self.get("url", ""))
try:
self["download"]["url"] = normalize(self["download"]["url"])
except KeyError:
...
@property
def tags(self) -> list[str]:
return [tag for tag in self.get("tags", []) if tag and not tag.startswith("_")]
@property
def private_tags(self) -> list[str]:
return [tag for tag in self.get("tags", []) if tag.startswith("_")]
@property
def visible(self):
if self.get("disabled", False):
return False
try:
return all(self[key] for key in self.MANDATORY_FIELDS)
except KeyError:
return False
@property
def langs(self) -> list[LanguageDef]:
return [get_lang_def(lang) for lang in self.get("languages", [])]
def gen_home(fpath: pathlib.Path):
try:
document = yaml.load(fpath.read_text(), Loader=SafeLoader)
except Exception as exc:
print("[CRITICAL] unable to read home YAML document, using fallback homepage")
traceback.print_exception(exc)
return
Conf.from_doc(document.get("metadata", {}))
context = Conf.to_dict()
context["packages"] = list(
filter(lambda p: p.visible, [Package(**item) for item in document["packages"]])
)
context["languages"] = {}
context["categories"] = set()
for package in context["packages"]:
for lang in package.langs:
context["languages"][lang.alpha_3] = lang.native
for tag in package.get("tags", []):
if tag.startswith("_category:"):
package["category"] = tag.split(":", 1)[-1]
context["categories"].add(package["category"])
context["categories"] = sorted(context["categories"])
context["readers"] = [Reader(**item) for item in document.get("readers", [])]
context["links"] = [Link(**item) for item in document.get("links", [])]
try:
with open(dest_dir / "index.html", "w") as fh:
context["page"] = "home"
fh.write(env.get_template("home.html").render(**context))
with open(dest_dir / "download.html", "w") as fh:
context["page"] = "download"
fh.write(env.get_template("download.html").render(**context))
except Exception as exc:
print("[CRITICAL] unable to gen homepage, using fallback")
traceback.print_exception(exc)
return
print("Generated homepage")
if __name__ == "__main__":
gen_home(packages_path)
if Conf.debug:
with open(dest_dir / "index.html") as fh:
print(fh.read())