forked from aitatanit/python-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sitemaps.py
More file actions
90 lines (73 loc) · 2.48 KB
/
make_sitemaps.py
File metadata and controls
90 lines (73 loc) · 2.48 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
import json
import sys
import os
# -------------------------------------------------------------------------------
#
# Script that makes a django sitemaps file containing all the user guide items
# (using ./inputs/translate.json)
#
# -------------------------------------------------------------------------------
NAME = "make_sitemaps" # name of this script
tab = " " # tab in space
# Get chapter names from translate.json
def get_chapters(translate):
return translate.values()
# Get sitemaps items
# N.B. To be imported in
def get_items(chapters):
locations = []
lmfiles = []
for chapter in chapters:
locations += ["'/python/{}/'".format(chapter)]
lmfiles += [("os.path.join(\n{tab}{tab}{tab}{tab}"
"settings.TOP_DIR, "
"'shelly',\n{tab}{tab}{tab}{tab}"
"'templates', "
"'api_docs', "
"'includes',\n{tab}{tab}{tab}{tab}"
"'user_guide',\n{tab}{tab}{tab}{tab}"
"'python',\n{tab}{tab}{tab}{tab}"
"'{chapter}',\n{tab}{tab}{tab}{tab}"
"'body.html')"
).format(chapter=chapter,tab=tab)]
return locations, lmfiles
# Generate python_sitemaps.py file
# See streambed/api_docs/ for more info
def get_sitemaps_py(locations, lmfiles):
sitemaps_py = (
"import os\n\n"
"from django.conf import settings\n\n\n"
"def items():\n"
" items = [\n"
)
for location, lmfile in zip(locations,lmfiles):
sitemaps_py += (
"{tab}{tab}dict(\n"
"{tab}{tab}{tab}location={location},\n"
"{tab}{tab}{tab}lmfile={lmfile},\n"
"{tab}{tab}{tab}priority=0.5\n"
"{tab}{tab})"
).format(location=location,lmfile=lmfile,tab=tab)
if location != locations[-1]:
sitemaps_py += ",\n"
sitemaps_py += (
"\n{tab}]"
"\n{tab}return items"
"\n"
).format(tab=tab)
return sitemaps_py
# Replace python_sitemaps.py
def replace_sitemaps(sitemaps_py):
f_urls = "./published/python_sitemaps.py"
with open(f_urls, "w") as f:
print "[{}]".format(NAME), '... writes in', f_urls
f.write(sitemaps_py)
return
# -------------------------------------------------------------------------------
def make_sitemaps(translate):
chapters = get_chapters(translate)
locations, lmfiles = get_items(chapters)
sitemaps_py = get_sitemaps_py(locations, lmfiles)
replace_sitemaps(sitemaps_py)
if __name__ == "__main__":
main()