forked from aitatanit/python-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_urls.py
More file actions
71 lines (54 loc) · 1.73 KB
/
Copy pathmake_urls.py
File metadata and controls
71 lines (54 loc) · 1.73 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
import json
import sys
import os
# -------------------------------------------------------------------------------
#
# Script that makes a django urls file containing all the
# (using ./inputs/translate.json)
#
# -------------------------------------------------------------------------------
NAME = "make_urls" # name of this script
tab = " " # tab in space
# Get chapter names from translate.json
def get_chapters(translate):
return translate.values()
# Get urls patterns
# N.B.: r'(?P<lang>python)/' is in streambed/shelly/api_docs/__init__.py
def get_urls(chapters):
urls = []
for chapter in chapters:
urls += [r'(?P<user_guide_chapter>{})/$'.format(chapter)]
return urls
# Generate python_urls.py file
# See streambed/api_docs/ for more info
def get_urls_py(urls):
urls_py = (
"from django.conf.urls import patterns, url\n\n"
"import api_docs.views\n\n\n"
"urlpatterns = patterns(\n"
"{tab}'',\n"
).format(tab=tab)
for url in urls:
urls_py += (
'{tab}url("'+url+'",\n'
'{tab}{tab}api_docs.views.user_guide_template)'
).format(tab=tab)
if url != urls[-1]:
urls_py += ",\n"
urls_py += "\n)\n"
return urls_py
# Replace python_urls.py
def replace_urls(urls_py):
f_urls = "./published/python_urls.py"
with open(f_urls, "w") as f:
print "[{}]".format(NAME), '... writes in', f_urls
f.write(urls_py)
return
# -------------------------------------------------------------------------------
def make_urls(translate):
chapters = get_chapters(translate)
urls = get_urls(chapters)
urls_py = get_urls_py(urls)
replace_urls(urls_py)
if __name__ == "__main__":
main()