-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate2.py
More file actions
executable file
·159 lines (127 loc) · 5.24 KB
/
generate2.py
File metadata and controls
executable file
·159 lines (127 loc) · 5.24 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
#!/usr/bin/env python3
import sqlite3
import argparse
import locale
import itertools
import string
import random
import extract2
import layout2
from helpers import station_id
CACHEBUST = "".join(random.choices(string.ascii_letters, k=10))
def render_station(station, lang):
address_html = ""
if station["strasse"]:
address_html = f'<address>{station["strasse"]}, {station["plz"]} {station["ort"]} ({station["bundesland"]})</address>'
subs_html = ""
if station["kategorie"] or station["aufgabentraeger"]:
subs_html += '<p class="sm">'
subs_html += " | ".join(
[
f"<b>{lang[key]}</b> {station[key]}"
for key in ("kategorie", "aufgabentraeger")
if station[key]
]
)
subs_html += "</p>"
for i, sub in enumerate(station["subs"]):
subs_html += '<p class="sm">'
if len(station["subs"]) > 1 or station["subs"][0]["name"] != station["name"]:
subs_html += f'<b>{sub["name"]}:</b> '
subs_html += " | ".join(
[
f"<b>{lang[key]}</b> {sub[key]}"
for key in ("ds100", "ibnr", "ifopt", "verkehr", "betreiber_name")
if sub[key]
]
)
if sub["lat"] and sub["lon"] and i > 0:
subs_html += f' | <a href="https://www.openstreetmap.org/#map=17/{sub["lat"]}/{sub["lon"]}">OpenStreetMap</a>'
subs_html += "</p>"
links_html = (
'<p class="button-row">'
f'<a href="https://dbf.finalrewind.org/{station["name"]}">db-infoscreen</a>'
)
if station["subs"][0]["betreiber_name"] == "DB Station und Service AG":
links_html += f' <a href="https://www.bahnhof.de/id/{station["bfnr"]}">bahnhof.de</a>'
if station["subs"][0]["lat"] and station["subs"][0]["lon"]:
links_html += f' <a href="https://www.openstreetmap.org/#map=17/{sub["lat"]}/{sub["lon"]}">OpenStreetMap</a>'
links_html += "</p>"
platforms_html = ""
if station["platforms"]:
if station["platforms"][0]["quelle"] == "rni" and len(station["platforms"]) > 1:
platforms_html += lang["rni_warn_html"]
for platform, rows in itertools.groupby(
sorted(station["platforms"], key=lambda row: row["bahnsteig"]),
key=lambda row: row["bahnsteig"],
):
platforms_html += f'<div class="platform"><h3><span class="invis">{lang["platform"]}</span> {platform}</h3>'
for row in rows:
platforms_html += (
f'<p><em><span>{lang["track"]} </span>{row["gleis"]}</em> '
f'{row["hoehe"]}cm {lang["high"]}, {row["laenge"]}m {lang["long"]} </p>'
)
platforms_html += "</div>"
if not platforms_html:
platforms_html = lang["notrackinfo_html"]
cite_html = ""
if station.get("_cite"):
cite_html += f'<h3 class="sm">{lang["patches"]}</h3><ul class="sm">'
for cite in station["_cite"]:
cite_html += f"<li>{cite}</li>"
cite_html += "</ul>"
return f"""<section data-name="{station["name"]}">
<h2 id="{station_id(station)}" lang="de"><a href="#{station_id(station)}">{station["name"]}</a></h2>
{address_html}
{links_html}
{subs_html}
{platforms_html}
{cite_html}
</section>"""
def render_station_for_list(station, lang):
sub_html = ""
if sub_names := [
sub["name"] for sub in station["subs"] if not sub["name"] == station["name"]
]:
sub_html = f'<i>{lang["also"]} {", ".join(sub_names)}</i>'
return f"""<li id="{station_id(station)}">
<a lang="de" href="./details/{lang["lang"]}/{station_id(station)}.html?{CACHEBUST}">{station["name"]}</a>
{sub_html}</li>"""
def generate(db, stations):
for lang in (layout2.LANG["de"], layout2.LANG["en"]):
with open(f'web/{lang["lang"]}.html', "w", encoding="utf-8") as f:
f.write(
layout2.LAYOUT_TOP.substitute(**lang)
+ layout2.LAYOUT_LINKLIST_PREFIX.substitute(**lang)
)
for station in stations:
f.write(render_station_for_list(station, lang))
f.write(
layout2.LAYOUT_LINKLIST_SUFFIX
+ layout2.LAYOUT_BOTTOM.substitute(**lang)
)
for station in stations:
with open(
f'web/details/{lang["lang"]}/{station_id(station)}.html',
"w",
encoding="utf-8",
) as f:
f.write(render_station(station, lang))
with open("web/complete.html", "w", encoding="utf-8") as f:
f.write(layout2.LAYOUT_TOP.substitute(**layout2.LANG["de"]))
for station in stations:
f.write(render_station(station, layout2.LANG["de"]))
f.write(layout2.LAYOUT_BOTTOM.substitute(**layout2.LANG["de"]))
if __name__ == "__main__":
locale.setlocale(locale.LC_ALL, "de_DE")
p = argparse.ArgumentParser()
p.add_argument("database", help="database path")
args = p.parse_args()
db = sqlite3.connect(args.database)
db.row_factory = sqlite3.Row
generate(
db,
sorted(
extract2.extract_stations(db), key=lambda bf: locale.strxfrm(bf["name"])
),
)