-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
executable file
·217 lines (176 loc) · 6.54 KB
/
render.py
File metadata and controls
executable file
·217 lines (176 loc) · 6.54 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
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
#!/usr/bin/env python3
import os
import os.path
import jinja2
import csv
import json
import pandas as pd
from bin.jinja_setup import env
import requests
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
from collections import OrderedDict
from analyse_dataset import BrownfieldDatasetAnalyser
from brownfield import (
brownfield_dataset_path,
resources_by_org,
get_latest_brownfield_resource,
get_brownfield_resource_list,
)
from utils import get_csv_as_json
from organisation import get_boundaries
session = CacheControl(requests.session(), cache=FileCache(".cache"))
dataset_csv = "https://raw.githubusercontent.com/digital-land/dataset-collection/master/dataset/dataset.csv"
# dataset_csv = "data/test.csv"
organisation_csv = "https://raw.githubusercontent.com/digital-land/organisation-dataset/master/collection/organisation.csv"
organisation_tag_csv = "https://raw.githubusercontent.com/digital-land/organisation-dataset/master/data/tag.csv"
docs = "docs/"
# fetch from a local file
def get_from_file(path):
file = open(path, mode="r")
all_of_it = file.read()
file.close()
return all_of_it
# fetch from a URL
def get(url):
r = session.get(url)
r.raise_for_status()
return r.text
def render(
path, template, organisations, tags, dataset=None, organisation=None, **kwargs
):
path = os.path.join(docs, path)
directory = os.path.dirname(path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(path, "w") as f:
f.write(
template.render(
organisations=organisations,
tags=tags,
dataset=dataset,
organisation=organisation,
**kwargs,
)
)
datasets_template = env.get_template("datasets.html")
dataset_template = env.get_template("dataset.html")
dataset_organisations_template = env.get_template("dataset-organisations.html")
dataset_organisation_template = env.get_template("dataset-organisation.html")
tags = OrderedDict()
for o in csv.DictReader(get(organisation_tag_csv).splitlines()):
o["organisations"] = []
tags[o["tag"]] = o
organisations = OrderedDict()
for o in csv.DictReader(get(organisation_csv).splitlines()):
o["path-segments"] = list(filter(None, o["organisation"].split(":")))
prefix = o["prefix"] = o["path-segments"][0]
o["id"] = o["path-segments"][1]
o.setdefault("tags", [])
o["tags"].append(prefix)
organisations[o["organisation"]] = o
def brownfield_land_dataset(d):
bf_idx = resources_by_org()
d["organisation"] = bf_idx
for organisation in d["organisation"]:
# check there are resource(s) associated with org
if len(d["organisation"][organisation]["resource"]):
d["organisation"][organisation][
"latest-resource"
] = get_latest_brownfield_resource(
d["organisation"][organisation]["resource"], data_preview=True
)
if dataset == "brownfield-land":
da = BrownfieldDatasetAnalyser(brownfield_dataset_path)
d["summary"] = da.summary()
d["sample"] = da.sample(5, 2340)
# page per-organisation
for organisation in d["organisation"]:
o = organisations[organisation]
o["path"] = "/".join(o["path-segments"])
p = dataset + "/organisation/" + o["path"]
render(
p + "/index.html",
dataset_organisation_template,
organisations,
tags,
dataset=d,
organisation=o,
)
# dataset indexes
render(dataset + "/index.html", dataset_template, organisations, tags, dataset=d)
render(
dataset + "/organisation/index.html",
dataset_organisations_template,
organisations,
tags,
dataset=d,
)
datasets = OrderedDict()
for d in csv.DictReader(get(dataset_csv).splitlines()):
# for d in csv.DictReader(get_from_file(dataset_csv).splitlines()):
dataset = d["dataset"]
datasets[dataset] = {
"name": d["name"],
"url": d["resource-url"],
"documentation": d["documentation-url"],
}
if dataset == "brownfield-land":
# generate pages for brownfield land dataset
brownfield_land_dataset(d)
else:
dataset_template = env.get_template(f"dataset-templates/{dataset}.html")
reader = csv.DictReader(get(datasets[dataset]["url"]).splitlines())
# might be slow to add all data for each dataset
datasets[dataset]["data"] = [row for row in reader]
# sort but put plans without name at the end
datasets[dataset]["data"].sort(
key=lambda x: "z" if x["name"] == "" else x["name"]
)
if dataset == "local-plans":
local_plan_template = env.get_template(f"dataset-templates/local-plan.html")
dev_plan_docs = get_csv_as_json(
"https://raw.githubusercontent.com/digital-land/alpha-data/master/local-plans/development-plan-document.csv"
)
for plan in datasets[dataset]["data"]:
plan["document"] = [
doc
for doc in dev_plan_docs
if doc["development-plan"] == plan["development-plan"]
]
for d in datasets[dataset]["data"]:
print(f"render page for local plan: {d['development-plan']}")
plan_organisations = d["organisations"].split(";")
render(
f"{dataset}/{d['development-plan']}/index.html",
local_plan_template,
organisations,
tags,
plan=d,
boundaries=get_boundaries(organisations, plan_organisations),
)
render(
dataset + "/index.html",
dataset_template,
organisations,
tags,
dataset=datasets[dataset],
)
# datasets
# temporary solution for list of all data on site
datasets_csv = "data/index_list.csv"
datasets = {
d["dataset"]: d for d in csv.DictReader(get_from_file(datasets_csv).splitlines())
}
mhclg_lists = {k: v for k, v in datasets.items() if v["publisher"] == "mhclg"}
non_mhclg_datasets = {
k: v for k, v in datasets.items() if not v["publisher"] == "mhclg"
}
with open("docs/index.html", "w") as f:
f.write(
datasets_template.render(
datasets=dict(sorted(non_mhclg_datasets.items())),
mhclg_lists=dict(sorted(mhclg_lists.items())),
download_url=dataset_csv,
)
)