-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate_tabletop_weather_station_substitutions.py
121 lines (93 loc) · 2.98 KB
/
generate_tabletop_weather_station_substitutions.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
DEBUG = False
if __name__ == "__main__" and not DEBUG:
print("Suppressing output of generate_tabletop_weather_station_substitutions.py")
def debug(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
import os
import sys
import generate_tables
bindings_infos = generate_tables.bindings_infos
lang = 'en'
# url_part, display_name
examples = {'c': 'C',
'csharp': 'C#',
'delphi': 'Delphi',
'java': 'Java',
'php': 'PHP',
'python': 'Python',
'ruby': 'Ruby',
'vbnet': 'Visual Basic .NET'}
binding_name = {
'en':
""":ref:`{0} <api_bindings_{1}>`""",
'de':
""":ref:`{0} <api_bindings_{1}>`"""
}
binding_names = {
'en':
"""
.. |bindings| replace:: {0}
""",
'de':
"""
.. |bindings| replace:: {0}
"""
}
example_downloads = {
'en':
"""
.. |examples_download| replace:: {0}
""",
'de':
"""
.. |examples_download| replace:: {0}
"""
}
example_download_simple = {
'en':
"""`{0} <https://github.com/Tinkerforge/tabletop-weather-station/tree/master/examples/{1}>`__""",
'de':
"""`{0} <https://github.com/Tinkerforge/tabletop-weather-station/tree/master/examples/{1}>`__"""
}
def make_substitutions():
substitutions = '\n>>>general\n'
formated_binding_names = []
for bindings_info in bindings_infos:
if bindings_info.is_programming_language and bindings_info.is_released:
formated_binding_names.append(binding_name[lang].format(bindings_info.display_name[lang], bindings_info.url_part))
substitutions += binding_names[lang].format(', '.join(formated_binding_names)) + '\n'
example_download_lines = []
for bindings_info in bindings_infos:
if bindings_info.url_part in examples and bindings_info.is_programming_language and bindings_info.is_released:
example_download_lines.append(example_download_simple[lang].format(examples[bindings_info.url_part], bindings_info.url_part))
substitutions += example_downloads[lang].format(', '.join(example_download_lines))
substitutions += '\n<<<general\n'
substitutions += '\n>>>example_list\n'
substitutions += '* ' + '\n* '.join(example_download_lines)
substitutions += '\n<<<example_list\n'
return substitutions
def write_if_changed(path, content):
if os.path.exists(path):
with open(path, 'r') as f:
existing = f.read()
if existing == content:
return
with open(path, 'w') as f:
f.write(content)
def generate(path):
global lang
if path.endswith('/en'):
lang = 'en'
elif path.endswith('/de'):
lang = 'de'
else:
debug('Wrong working directory')
sys.exit(1)
generate_tables.lang = lang
debug('Generating TabletopWeatherStation.substitutions')
write_if_changed(os.path.join(path, 'source', 'Kits', 'TabletopWeatherStation', 'TabletopWeatherStation.substitutions'), make_substitutions())
if __name__ == "__main__":
generate(os.getcwd())