This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.py
executable file
·234 lines (190 loc) · 7.03 KB
/
build.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
#!/usr/bin/env python3
import json
import locale
import logging
import warnings
from datetime import date
from html.parser import HTMLParser
from http import HTTPStatus
from operator import itemgetter
from pathlib import Path
from time import perf_counter
import httpx
import toml
from minicli import cli, run, wrap
from construction.markdown import create_markdown_parser, render_markdown_file
from construction.questions import build_questions_index
from construction.templates import render_template
from construction.thematiques import get_thematiques
from construction.utils import each_file_from, each_folder_from
LOGGER = logging.getLogger(__name__)
HERE = Path(__file__).parent
SRC_DIR = HERE / "src"
CONTENUS_DIR = HERE / "contenus"
STATIC_DIR = HERE / "static"
NB_PAGES_THEMATIQUES_ACCUEIL = 8
NB_PAGES_THEMATIQUES_NAVIGATION = 6
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
# Ignore dateparser warnings regarding pytz
warnings.filterwarnings(
"ignore",
message=(
"The localize method is no longer necessary, "
"as this time zone supports the fold attribute"
),
)
@cli
def all():
index()
thematiques()
sitemap()
@cli
def index():
"""Build the index with contents from markdown dedicated folder."""
markdown_parser = create_markdown_parser()
responses = build_responses(CONTENUS_DIR, markdown_parser)
thematiques = get_thematiques(markdown_parser)
fr_thematiques = [
thematique for thematique in thematiques if thematique.lang != "en"
]
thematiques = fr_thematiques[:NB_PAGES_THEMATIQUES_ACCUEIL]
responses["thematiques"] = thematiques
responses["autres_thematiques"] = [
thematique
for thematique in thematiques
if thematique.name != "j-ai-des-symptomes-covid"
]
content = render_template(
"index.html", actualites=extrait_actualites(), **responses
)
content = cache_external_pdfs(content)
(SRC_DIR / "index.html").write_text(content)
def extrait_actualites():
actualites = [toml.load(f) for f in each_file_from(CONTENUS_DIR / "actualites")]
return sorted(actualites, key=itemgetter("date"), reverse=True)
@cli
def thematiques():
"""Build the theme pages with contents from thematiques folder."""
questions_index = build_questions_index()
markdown_parser = create_markdown_parser(questions_index=questions_index)
responses = build_responses(CONTENUS_DIR, markdown_parser)
version = get_version()
meta_pied_de_page = str(responses["meta_pied_de_page"]).replace(
'<span class="js-latest-update"></span>',
f" - Mise à jour le {version.strftime('%d-%m-%Y')}",
)
thematiques = get_thematiques(markdown_parser)
for thematique in thematiques:
autres_thematiques = [t for t in thematiques if t != thematique]
content = render_template(
"thematique.html",
**{
"thematique": thematique,
"thematiques": autres_thematiques[:NB_PAGES_THEMATIQUES_NAVIGATION],
"config_stats_url": responses["config_stats_url"],
"meta_feedback_conseils": responses["meta_feedback_conseils"],
"meta_unsupported_browser": responses["meta_unsupported_browser"],
"meta_bandeau_elections": responses["meta_bandeau_elections"],
"meta_pied_de_page": meta_pied_de_page,
},
)
(SRC_DIR / f"{thematique.name}.html").write_text(content)
def get_version() -> date:
content = open(HERE / "static" / "version.json").read()
data = json.loads(content)
# We might have a version with more characters than a date
# if multiple releases are required within the same day.
version = data["version"][:10]
return date(*(int(item) for item in version.split("-")))
@cli
def sitemap():
"""Build the sitemap for index + themes pages."""
markdown_parser = create_markdown_parser()
content = render_template(
"sitemap.html",
thematiques=get_thematiques(markdown_parser),
lastmod_date=date.today(),
)
(STATIC_DIR / "sitemap.xml").write_text(content)
def build_responses(source_dir, markdown_parser):
"""Extract and convert markdown from a `source_dir` directory into a dict."""
responses = {}
for folder in each_folder_from(source_dir):
for path in each_file_from(folder, pattern="*.md"):
html_content = render_markdown_file(path, markdown_parser)
responses[path.stem] = html_content
return responses
def cache_external_pdfs(content: str, timeout: int = 10) -> str:
"""
Download external PDFs and replace links with the local copy
"""
for url in _extract_pdf_links(content):
filename = url_to_filename(url)
download_file_if_needed(
url=url,
local_path=SRC_DIR / "pdfs" / filename,
timeout=timeout,
)
content = content.replace(
f'href="{url}"', f'href="pdfs/{filename}" data-original-link="{url}"'
)
return content
def _extract_pdf_links(content):
parser = PDFLinkExtractor()
parser.feed(content)
return sorted(parser.pdf_links)
class PDFLinkExtractor(HTMLParser):
def reset(self):
HTMLParser.reset(self)
self.pdf_links = set()
def handle_starttag(self, tag, attrs):
if tag == "a":
attrs = dict(attrs)
url = attrs.get("href")
if url and url.startswith("http") and url.endswith(".pdf"):
self.pdf_links.add(url)
def url_to_filename(url: str) -> str:
basename, extension = url.rsplit(".", 1)
filename = (
basename.replace("http://", "")
.replace("https://", "")
.replace(".", "-")
.replace("/", "-")
)
return f"{filename}.{extension}"
def download_file_if_needed(url, local_path, timeout):
if local_path.exists():
LOGGER.info(f"SKIP: {url} exists in {local_path}")
else:
LOGGER.info(f"FETCH: {url} to {local_path}")
_download_file(url, local_path, timeout)
def _download_file(url, local_path, timeout):
try:
with httpx.stream(
"GET",
url,
follow_redirects=True,
timeout=timeout,
verify=False, # ignore SSL certificate validation errors
) as response:
if response.status_code != HTTPStatus.OK:
raise Exception(f"{url} is broken! ({response.status_code})")
_save_binary_response(local_path, response)
except httpx.ReadError:
# It (may?) happen in Github CI which prevents
# downloading external/big files.
return
def _save_binary_response(file_path: Path, response: "httpx.Response"):
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True)
with open(file_path, "wb") as download_file:
for chunk in response.iter_bytes():
download_file.write(chunk)
@wrap
def perf_wrapper():
start = perf_counter()
yield
elapsed = perf_counter() - start
print(f"Done in {elapsed:.5f} seconds.")
if __name__ == "__main__":
run()