-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_website_content.py
More file actions
129 lines (104 loc) · 3.9 KB
/
generate_website_content.py
File metadata and controls
129 lines (104 loc) · 3.9 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
#!/usr/bin/env python3
"""
Coding Da Vinci Data Presentation Generator
This script is a helper to generate the codingdavinci website data presentation
from yaml files. maintainability is better than with the suggested method of
putting it all into one large HTML file.
Copyright: 2018, Universitätsbibliothek Leipzig <info@ub.uni-leipzig.de>
Author: F. Rämisch <raemisch@ub.uni-leipzig.de>
License: http://opensource.org/licenses/gpl-3.0.php GNU GPLv3
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import os.path
import sys
import yaml
from os import mkdir, listdir
from jinja2 import Template
BASEPATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE = os.path.join(BASEPATH, "template.html")
BUILDDIR = os.path.join(BASEPATH, "build")
DATADIR = os.path.join(BASEPATH, "data")
def gen_builddir(builddir):
"""
Creates the build dir if not existent.
:param builddir: path where built files are save to
:return: the build path
"""
if not os.path.isdir(builddir):
mkdir(builddir)
return builddir
def get_template(templatepath):
"""
Takes a template filename and returns a jinja2-Template object
:param template: full path to template
:return: jinja2 template
"""
with open(templatepath) as tfile:
return Template(tfile.read())
def get_data(datadir):
"""
Reads all files from the data directory and returns the content as a list of dicts.
Each file contains the content as a yaml file.
:param datadir: the directory where the data is located
:return: list of dicts with the data
"""
data = []
for data_file in os.listdir(datadir):
with open(os.path.join(datadir, data_file)) as dfile:
data.append(yaml.load(dfile.read()))
return data
def make_website_content(builddir=BUILDDIR, templatepath=TEMPLATE, datadir=DATADIR):
"""
Main function wrapping the whole build process.
:param builddir: output directory where the files are saved to
:param template:
:return:
"""
output = []
out_dict = {}
gen_builddir(builddir)
template = get_template(templatepath)
data = get_data(datadir)
# Collect Data to be rendered
# This is required, to be able to bundle data entries of one provider
for entry in data:
if entry['build']:
provider_slug = entry['provider_slug']
if provider_slug not in out_dict.keys():
out_dict[provider_slug] = entry
out_dict[provider_slug]['data_points'] = [entry,]
else:
out_dict[provider_slug]['data_points'].append(entry)
# Build the template
for entry in out_dict.values():
output.append(template.render(entry))
with open(os.path.join(builddir, "daten.html"), "w") as buildfile:
buildfile.write("\n".join(output))
def main(argv):
"""
Wrapper around the make website content. Parses command line arguments to ease
the build process from the shell.
:param argv: sys.argv arguments (optionally) containing builddir and template file
:return:
"""
builddir = BUILDDIR
template = TEMPLATE
datadir = DATADIR
if len(argv) > 1:
builddir = argv[1]
if len(argv) > 2:
template = argv[2]
if len(argv) > 3:
datadir = argv[3]
make_website_content(builddir, template, datadir)
if __name__ == "__main__":
main(sys.argv)