forked from geotagx/geotagx-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·276 lines (218 loc) · 9 KB
/
build.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
import os, sys, codecs, locale, json, htmlmin
from jinja2 import Environment, FileSystemLoader
from slimit import minify
from rcssmin import cssmin
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
USAGE = """Usage: """ + __file__ + """ directory [OPTIONS]
Build the task presenter for the GeoTag-X project located in the specified directory.
The following is a list of OPTIONS you can use to modify the script's behavior:\n
\r -f, --force overwrites the file 'template.html' in the specified directory.
\r -c, --compress compresses the generated task presenter.
\r -s, --no-static-inline disables inlining GeoTag-X's common CSS and JS files, adding external links to them instead.
\r -p, --no-pybossa-run disables automatic generation of the pybossa launcher. In this case, the launcher must be implemented in project.js.
\r -h, --help prints this help message.\n"""
LAYOUT_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "layout")
def has_valid_project(path):
"""Returns true if the directory with the specified path is writable, and contains a valid GeoTag-X project, false otherwise."""
return os.path.isdir(path) \
and os.access(path, os.W_OK) \
and os.access(os.path.join(path, "project.json"), os.F_OK | os.R_OK)
def is_valid_project_json(json):
"""Returns true if the specified JSON object has a valid project structure, false otherwise."""
# TODO
return True
def get_project_json(filename):
"""Converts the project.json file into a JSON object and returns it."""
with open(filename) as file:
data = file.read()
return json.loads(data)
def get_template_css(compress):
"""Collects CSS files from the template static folder, optionally minifies them,
and then embeds them directly into the rendered output.
"""
CSS_DIR = os.path.join(LAYOUT_DIR, *["static","css"])
css = ""
for root, dirs, filenames in os.walk(CSS_DIR, topdown=False):
for filename in filter(lambda f: f.endswith(".css"), filenames):
css += open(os.path.join(root, filename), "r").read()
return css if not compress else cssmin(css, keep_bang_comments=False)
def get_template_js(compress):
"""Collects JS files from the template static folder, optionally minifies them,
and then embeds them directly into the rendered output
"""
JS_DIR = os.path.join(LAYOUT_DIR, *["static","js"])
js = ""
for root, dirs, filenames in os.walk(JS_DIR, topdown=False):
for filename in filter(lambda f: f.endswith(".js"), filenames):
js += open(os.path.join(root, filename), "r").read()
return js if not compress else minify(js)
def get_project_css(project_dir, compress):
"""Returns the project's custom stylesheet, possibly minified, if it exists."""
try:
with open(os.path.join(project_dir, "project.css"), "r") as f:
css = f.read()
return css if not compress else cssmin(css, keep_bang_comments=False)
except:
return ""
def get_project_js(project_dir, compress):
"""Returns the project's custom script, possibly minified, if it exists."""
try:
with open(os.path.join(project_dir, "project.js"), "r") as f:
js = f.read()
return js if not compress else minify(js)
except:
return ""
def get_project_help(directory):
"""Returns a dictionary that contains the help provided for a specific question."""
help = {}
if os.path.isdir(directory):
for filename in filter(lambda f: f.endswith(".html"), os.listdir(directory)):
with open(os.path.join(directory, filename)) as file:
filedata = file.read().strip()
if filedata:
# Associate the file's content with a question identifier.
id = os.path.splitext(os.path.basename(filename))[0]
help[id] = filedata
return help
def get_questionnaire_flow_handler(questions):
"""Generates an anonymous javascript function that determines the next question based on the current question and it's anwer."""
def decode_question_id(id):
if isinstance(id, int) and id >= 0:
return id
elif isinstance(id, unicode) and id == "finish":
return len(questions) + 1
else:
return 0
def get_statements(branch, question_type):
if isinstance(branch, list):
statements = ""
for entry in branch:
for key in entry:
answer = key.lower()
next_question = decode_question_id(entry.get(key))
if question_type == "binary" and answer == "no":
statements += "else if (answer !== \"yes\"){{ return {}; }} ".format(next_question)
else:
statements += "else if (answer === \"{0}\"){{ return {1}; }} ".format(answer, next_question)
# Append default action.
statements += "else { return question + 1; }"
return statements.lstrip("else ") # Remember to remove the leading "else".
else:
return "return {};".format(decode_question_id(branch))
cases = ""
for question in questions:
branch = question.get("branch")
if branch is not None:
condition = "case {}: ".format(question.get("id"))
statements = get_statements(branch, question.get("type"))
cases += "{0}{1}\n\t\t\t".format(condition, statements)
return """
function(question, answer){{
answer = $.type(answer) === "string" ? answer.toLowerCase() : answer; // toLowerCase for case-insensitive string comparisons.
switch(question){{
{}
default: return question + 1;
}}
}}
""".strip().format(cases.rstrip("\n\t\t\t"))
def get_project_tutorial(project_dir):
"""Generates an array object containing assertions about a specified image."""
def parse_assertions(assertions):
def parse_messages(messages):
output = ""
for key in messages:
message = messages[key]
output += "\"{0}\":\"{1}\",\n{2}".format(key, message, "\t"*6)
return output.strip()
output = ""
for question in assertions:
assertion = assertions[question]
expects = assertion["expects"].lower() # lower-case for case-insensitive string comparisons.
default_message = assertion["default_message"]
messages = parse_messages(assertion["messages"])
output += """
{0}:{{
"expects":"{1}",
"default_message":"{2}",
"messages":{{
{3}
}}
\t}},
""".format(question, expects, default_message, messages).strip()
return output.rstrip(",")
try:
tutorial = ""
with open(os.path.join(project_dir, "tutorial.json")) as file:
data = file.read()
for entry in json.loads(data)["tutorial"]:
image = entry["image"]
image_source = entry["image_source"]
assertions = parse_assertions(entry["assertions"])
tutorial += "\n\t\t"
tutorial += """
{{
"image":"{0}",
"image_source":"{1}",
"assertions":{{
{2}
}}\r\t\t}},
""".format(image, image_source, assertions).strip()
return "[{}\n\t]".format(tutorial.strip().rstrip(",")) # "[{}]".format(tutorial)
except:
return None
def build(path, compress=False):
"""Builds the task presenter for the project located at the specified path."""
project_dir = os.path.realpath(path)
json = get_project_json(os.path.join(project_dir, "project.json"))
if not is_valid_project_json(json):
print "Error! The 'project.json' file is not valid."
sys.exit(1)
template = Environment(loader=FileSystemLoader(searchpath=LAYOUT_DIR)).get_template("base.html")
short_name = json["short_name"].strip()
why_ = json["why"].strip()
questions_ = json["questions"]
get_next_question_ = get_questionnaire_flow_handler(questions_)
# Assign the help to its corresponding question.
help = get_project_help(os.path.join(project_dir, "help"))
if len(help) > 0:
for question in questions_:
key = str(question["id"])
try:
question[u"help"] = help[key]
except:
pass
# Build the template and tutorial.
js_ = get_template_js(compress) # Collects JS common to the whole template
css_ = get_template_css(compress) # Collects CSS common to the whole template
js_ += get_project_js(project_dir, compress)
css_ += get_project_css(project_dir, compress)
with open(os.path.join(project_dir, "template.html"), "w") as output:
html = template.render(is_tutorial=False, questions=questions_, css=css_, js=js_, slug=short_name, why=why_, get_next_question=get_next_question_)
# if compress:
# html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True)
output.write(html.encode("UTF-8"))
# Build the tutorial.
with open(os.path.join(project_dir, "tutorial.html"), "w") as output:
tutorial_ = get_project_tutorial(project_dir)
if tutorial_ is not None:
html = ""
html = template.render(is_tutorial=True, questions=questions_, css=css_, js=js_, slug=short_name, why=why_, get_next_question=get_next_question_, tutorial=tutorial_)
# if compress:
# html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True)
output.write(html.encode("UTF-8"))
def main(argv):
argc = len(argv)
if argc != 2:
print USAGE
else:
path = argv[1]
if not path or path in ("-h", "--help"):
print USAGE
else:
if has_valid_project(path):
build(path)
else:
print "The directory '" + path + "' does not contain a valid GeoTag-X project, or you may not have read/write permissions."
if __name__ == "__main__":
main(sys.argv)