This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_factions.py
87 lines (71 loc) · 3.35 KB
/
build_factions.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
from datapackage_pipelines.wrapper import ingest, spew
from template_functions import get_jinja_env
import logging, os, subprocess
from datetime import datetime
from template_functions import build_template, get_context
from constants import FACTION_HOME_URL, FACTION_URL, MEMBER_URL, MEMBERS_HOME_URL
def main():
parameters, datapackage, resources = ingest()
jinja_env = get_jinja_env()
members = {}
factions = {}
knessets = {}
counts = {}
for descriptor, resource in zip(datapackage["resources"], resources):
if descriptor["name"] == "members":
for member in resource:
members[member["mk_individual_id"]] = member
elif descriptor["name"] == "positions":
for position in resource:
if position["object_type"] == "faction":
factions[position["object_id"]] = {
"faction_num": position["object_id"],
"faction_name": position["object_name"],
"mks": []
}
for id in position["mk_individual_ids"]:
factions[position["object_id"]]["mks"].append(members[id])
elif descriptor["name"] == "knessets":
for knesset in resource:
knessets[knesset["KnessetNum"]] = []
for id in knesset["faction"]:
knessets[knesset["KnessetNum"]].append(factions[id])
for knesset_num, factions in knessets.items():
mks = []
build_template(jinja_env, "factions_index.html",
get_context({
"knesset_num": knesset_num,
"factions": factions,
"faction_url": FACTION_URL.format(knesset_num=knesset_num,faction_id="{faction_id}"),
"member_url": MEMBER_URL
}),
FACTION_HOME_URL.format(knesset_num=knesset_num))
for faction in factions:
mks.extend([mk["mk_individual_id"] for mk in faction["mks"]])
build_template(jinja_env, "faction_detail.html",
get_context({
"knesset_num": knesset_num,
"faction_num": faction["faction_num"],
"faction_name": faction["faction_name"],
"mks": faction["mks"],
"faction_home_url": FACTION_HOME_URL.format(knesset_num=knesset_num),
"member_url": MEMBER_URL
}),
FACTION_URL.format(knesset_num=knesset_num,faction_id=faction["faction_num"]))
counts[knesset_num] = {
'factions': len(factions),
'mks': len(set(mks))
}
build_template(jinja_env, "members_index.html",
get_context({
"keys": sorted(counts, reverse=True),
"knessets": counts,
"url": FACTION_HOME_URL
}),
MEMBERS_HOME_URL)
if os.environ.get("SKIP_STATIC") != "1":
subprocess.check_call(["mkdir", "-p", "dist"])
subprocess.check_call(["cp", "-rf", "static", "dist/"])
spew(dict(datapackage, resources=[]), [], {})
if __name__ == "__main__":
main()