-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapsplat.py
165 lines (147 loc) · 5.74 KB
/
mapsplat.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
import argparse
import jinja2
import yaml
import sys
from pathlib import Path
from pprint import pprint
from os import makedirs
from os.path import relpath, dirname
import pycountry
from operator import itemgetter
from shutil import rmtree
START_PEOPLE_STRING = "[//]: # (START_PEOPLE_LIST_DNR)"
END_PEOPLE_STRING = "[//]: # (END_PEOPLE_LIST_DNR)"
INSTITUTION_NAME = """
{% if institution.longname -%}
{{ institution.longname }} ({{ institution.name }})
{%- else -%}
{{ institution.name }}
{%- endif %}
""".strip()
INDEX_TEMPLATE = jinja2.Template("""
[//]: # (WARNING: Automatically generated. Do not modify.)
[//]: # (Instead modify mapsplat or the map data and re-generate.)
{% for country_info in tree %}
* {{ country_info.name }}
{% for institution in country_info.institutions %}
* [""" + INSTITUTION_NAME + """]({{ by_institution_path }}/{{ institution.name|lower }}) [{{ institution.people|length }}]
{% endfor %}
{% endfor %}
""", trim_blocks=True, lstrip_blocks=True)
INSTITUTION_PAGE = jinja2.Template("""
+++
+++
[//]: # (WARNING: Automatically generated. Do not modify.)
[//]: # (Instead modify mapsplat or the map data and re-generate.)
# """ + INSTITUTION_NAME + """ RSEs
{% for person in institution.people %}
* {{ person.name }}
{% if person.institutional_page %}
* Institutional page: [{{ person.institutional_page }}]({{ person.institutional_page }})
{% endif %}
{%- if person.homepage %}
* Homepage: [{{ person.homepage }}]({{ person.homepage }})
{% endif %}
{%- if person.twitter %}
* Twitter: [@{{ person.twitter }}](https://twitter.com/{{ person.twitter }})
{% endif %}
{%- if person.github %}
* GitHub: [{{ person.github }}](https://github.com/{{ person.github }})
{% endif %}
{%- if person.gitlab %}
* GitLab: [{{ person.gitlab }}](https://gitlab.com/{{ person.gitlab }})
{% endif %}
{%- if person.skills %}
* Skills: {{ person.skills|join(", ") }}
{% endif %}
{% endfor %}
""", trim_blocks=True, lstrip_blocks=True)
def iso2_to_country_name(country_iso2):
return pycountry.countries.get(alpha_2=country_iso2).name
def mapsplat(input, output, insert_index):
with open(input) as f:
map_data = yaml.safe_load(f)
data_tree = {}
by_place = {}
for place in map_data["places"]:
if not place["country"]:
print(f"Warning: skipping institution {place['name']} because it is missing a country")
continue
by_place[place["name"]] = data_tree\
.setdefault(
place["country"],
{
"name": iso2_to_country_name(place["country"]),
"institutions": {}
}
)["institutions"]\
.setdefault(place["name"], place)
for person in map_data["persons"]:
if person["place"] not in by_place:
print(f"Warning: skipping person {person['name']} because could not find institution {person['place']}")
continue
if "name" not in person:
print(f"Warning: skipping nameless person")
continue
by_place[person["place"]].setdefault("people", []).append(person)
data_tree = sorted((
{
"iso2": country_iso2,
"name": country_info["name"],
"institutions": sorted((
{
"short": institution_short,
**institution_info,
"people": sorted(institution_info["people"], key=itemgetter("name"))
}
for institution_short, institution_info in country_info["institutions"].items()
if institution_info.get("people")
), key=itemgetter("short"))
}
for country_iso2, country_info in data_tree.items()
), key=itemgetter("name"))
rmtree(output)
makedirs(output, exist_ok=True)
up_one = insert_index is not None and insert_index != "_index.md"
by_institution_path = ("../" if up_one else "") + relpath(output, dirname(insert_index))
index_text = INDEX_TEMPLATE.render({
"tree": data_tree,
"by_institution_path": by_institution_path,
})
if insert_index:
with open(insert_index, "r+") as index_f:
lines = index_f.read().splitlines(keepends=True)
start_line_idx = None
end_line_idx = None
for idx, line in enumerate(lines):
if line.startswith(START_PEOPLE_STRING):
start_line_idx = idx
elif start_line_idx is not None and line.startswith(END_PEOPLE_STRING):
end_line_idx = idx
if start_line_idx is not None and end_line_idx is not None:
lines = [*lines[:start_line_idx + 1], index_text, "\n", *lines[end_line_idx:]]
else:
print("Could not find place to insert people list", file=sys.stderr)
sys.exit(-1)
index_f.seek(0)
index_f.write("".join(lines))
index_f.truncate()
with open(output / "_index.md", "w") as index_f:
index_f.write("+++\n+++")
else:
with open(output / "_index.md", "w") as index_f:
index_f.write(index_text)
for place_info in by_place.values():
with open(output / (place_info["name"].lower() + ".md"), "w") as place_f:
place_f.write(INSTITUTION_PAGE.render({
"institution": place_info
}))
def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser()
parser.add_argument('input', help="input yaml", type=Path)
parser.add_argument('output', help="output directory", type=Path)
parser.add_argument('--insert-index', help="insert index into this file", type=Path)
args = parser.parse_args(argv)
mapsplat(args.input, args.output, args.insert_index)
if __name__ == "__main__":
main()