-
Notifications
You must be signed in to change notification settings - Fork 2
/
asknot-ng.py
executable file
·154 lines (124 loc) · 5 KB
/
asknot-ng.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
#!/usr/bin/env python
""" asknot-ng.py [OPTIONS] template.html questions.yml path/to/locale
Ask not what $ORG can do for you... but what can you do for $ORG.
"""
from __future__ import print_function
import argparse
import copy
import gettext
import json
import os
import shutil
import sys
import traceback
from asknot_lib import (
defaults,
load_template,
load_yaml,
prepare_code_items,
prepare_tree,
prepare_next_child,
gather_ids,
produce_graph,
)
def work(question_filename, template, lang, languages, graph, build, _, **kw):
""" Main work function. Called once per 'lang' from ``main``.
The function does all the things needed to build a copy of the site.
Shortly, it:
- loads the template used to render the html
- loads the tree of questions from a questions file
- recursively pulls in any other included questions files
- prepares the tree by adding unique ids to each node
- renders the template using the in-memory copy of the questions
- writes out a copy of the rendered html to disk
"""
template = load_template(template)
data = load_yaml(question_filename)
prepare_code_items(data)
data['tree'] = prepare_tree(data, data['tree'], _=_)
prepare_next_child(data, data['tree'], _=_)
data['all_ids'] = list(gather_ids(data['tree']))
data['all_ids_as_json'] = json.dumps(data['all_ids'], indent=4)
data['tree_as_json'] = json.dumps(data['tree'], indent=4)
kwargs = copy.copy(defaults)
kwargs.update(data)
kwargs.update(kw)
kwargs['lang'] = lang
kwargs['languages'] = languages
if graph:
dot = produce_graph(kwargs['tree'])
dot.layout()#prog='dot')
filename = '%s.svg' % kwargs.get('theme', 'asknot')
dot.draw(filename)
print("Wrote", filename)
html = template.render(**kwargs)
outdir = os.path.join(build, lang)
if not os.path.exists(outdir):
os.makedirs(outdir)
outfile = os.path.join(outdir, 'index.html')
with open(outfile, 'wb') as f:
f.write(html)
print("Wrote", outfile)
def main(localedir, languages, strict, build, static, **kw):
""" Main entry point for for the command line tool.
This function loops over all translated copies of the site that it can find
and renders a copy of the site for each language by calling ``work``.
"""
if languages is None:
languages = [
d for d in os.listdir(localedir)
if os.path.isdir(os.path.join(localedir, d))]
# Default to english..
if 'en' not in languages and not strict:
languages = languages + ['en']
else:
languages = languages.split(',')
if not languages:
print("No languages found.")
fallback = not strict
for lang in languages:
try:
translation = gettext.translation(
'asknot-ng', localedir, languages=[lang], fallback=fallback)
except IOError:
traceback.print_exc()
raise IOError("No translation found for %r" % lang)
except ValueError:
print("Got the following error for language %r" % lang)
traceback.print_exc()
continue
translation.install()
if sys.version_info[0] == 3:
_ = translation.gettext
else:
_ = translation.ugettext
work(_=_, lang=lang, languages=languages, build=build, **kw)
staticdir = os.path.abspath(static)
global_staticdir = os.path.join(build, "static")
shutil.copytree(staticdir, global_staticdir, symlinks=True)
shutil.copyfile(os.path.join(staticdir, 'index.html'), os.path.join(build, 'index.html'))
print("Copied %s to %s" % (staticdir, global_staticdir))
def process_args():
parser = argparse.ArgumentParser(__doc__)
parser.add_argument("template", help="Path to a mako template "
"for the site.")
parser.add_argument("question_filename", help="Path to a .yaml file "
"containing the config and question tree.")
parser.add_argument("localedir", help="Location of the locale directory.")
parser.add_argument("-t", "--theme", default="default",
help="Theme name to use.")
parser.add_argument("-s", "--static", default="static",
help="Directory of static files (js, css..).")
parser.add_argument("-b", "--build", default="build",
help="Directory to write output.")
parser.add_argument("-l", "--languages", default=None,
help="List of languages to use. Defaults to all.")
parser.add_argument("-S", "--strict", default=False, action="store_true",
help="Fail if no translation is found.")
parser.add_argument("-g", "--graph", default=False, action="store_true",
help="Also generate a graph of the question tree.")
return parser.parse_args()
if __name__ == '__main__':
args = process_args()
args = vars(args)
main(**args)