Debian Codegraph @

updatetranslations.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # updatetranslations.py
3 #
4 # tl;dr this script updates our translation file with the newest, coolest strings we've added!
5 # it manually searches the source code, extracts strings and then updates the language files.
6
7 # Written in 2018 by Daniel Oaks <[email protected]>
8 #
9 # To the extent possible under law, the author(s) have dedicated all copyright
10 # and related and neighboring rights to this software to the public domain
11 # worldwide. This software is distributed without any warranty.
12 #
13 # You should have received a copy of the CC0 Public Domain Dedication along
14 # with this software. If not, see
15 # <http://creativecommons.org/publicdomain/zero/1.0/>.
16
17 """updatetranslations.py
18
19 Usage:
20 updatetranslations.py run <irc-dir> <languages-dir>
21 updatetranslations.py --version
22 updatetranslations.py (-h | --help)
23
24 Options:
25 <irc-dir> Oragono's irc subdirectory where the Go code is kept.
26 <languages-dir> Languages directory."""
27 import os
28 import re
29 import json
30
31 from docopt import docopt
32 import yaml
33
34 ignored_strings = [
35 'none', 'saset'
36 ]
37
38 if __name__ == '__main__':
39 arguments = docopt(__doc__, version="0.1.0")
40
41 if arguments['run']:
42 # general IRC strings
43 irc_strings = []
44
45 for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
46 for fname in files:
47 filepath = subdir + os.sep + fname
48 if filepath.endswith('.go'):
49 content = open(filepath, 'r', encoding='UTF-8').read()
50
51 matches = re.findall(r'\.t\("((?:[^"]|\\")+)"\)', content)
52 for match in matches:
53 if match not in irc_strings:
54 irc_strings.append(match)
55
56 matches = re.findall(r'\.t\(\`([^\`]+)\`\)', content)
57 for match in matches:
58 if match not in irc_strings:
59 irc_strings.append(match)
60
61 for s in ignored_strings:
62 try:
63 irc_strings.remove(s)
64 except ValueError:
65 # ignore any that don't exist
66 ...
67
68 print("irc strings:", len(irc_strings))
69 with open(os.path.join(arguments['<languages-dir>'], 'example', 'irc.lang.json'), 'w') as f:
70 f.write(json.dumps({k:k for k in irc_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
71 f.write('\n')
72
73 for string in irc_strings:
74 if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
75 print(' confirm:', string)
76
77 # help entries
78 help_strings = []
79
80 for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
81 for fname in files:
82 filepath = subdir + os.sep + fname
83 if fname == 'help.go':
84 content = open(filepath, 'r', encoding='UTF-8').read()
85
86 matches = re.findall(r'\`([^\`]+)\`', content)
87 for match in matches:
88 if '\n' in match and match not in help_strings:
89 help_strings.append(match)
90
91 for s in ignored_strings:
92 try:
93 help_strings.remove(s)
94 except ValueError:
95 # ignore any that don't exist
96 ...
97
98 print("help strings:", len(help_strings))
99 with open(os.path.join(arguments['<languages-dir>'], 'example', 'help.lang.json'), 'w') as f:
100 f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
101 f.write('\n')
102
103 for string in help_strings:
104 if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
105 print(' confirm:', string.split('\n')[0])
106
107 # nickserv help entries
108 help_strings = []
109
110 for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
111 for fname in files:
112 filepath = subdir + os.sep + fname
113 if fname == 'nickserv.go':
114 content = open(filepath, 'r', encoding='UTF-8').read()
115
116 matches = re.findall(r'\`([^\`]+)\`', content)
117 for match in matches:
118 if match not in help_strings:
119 help_strings.append(match)
120
121 for s in ignored_strings:
122 try:
123 help_strings.remove(s)
124 except ValueError:
125 # ignore any that don't exist
126 ...
127
128 print("nickserv help strings:", len(help_strings))
129 with open(os.path.join(arguments['<languages-dir>'], 'example', 'nickserv.lang.json'), 'w') as f:
130 f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
131 f.write('\n')
132
133 for string in help_strings:
134 if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
135 print(' confirm:', string)
136
137 # chanserv help entries
138 help_strings = []
139
140 for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
141 for fname in files:
142 filepath = subdir + os.sep + fname
143 if fname == 'chanserv.go':
144 content = open(filepath, 'r', encoding='UTF-8').read()
145
146 matches = re.findall(r'\`([^\`]+)\`', content)
147 for match in matches:
148 if match not in help_strings:
149 help_strings.append(match)
150
151 for s in ignored_strings:
152 try:
153 help_strings.remove(s)
154 except ValueError:
155 # ignore any that don't exist
156 ...
157
158 print("chanserv help strings:", len(help_strings))
159 with open(os.path.join(arguments['<languages-dir>'], 'example', 'chanserv.lang.json'), 'w') as f:
160 f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
161 f.write('\n')
162
163 for string in help_strings:
164 if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
165 print(' confirm:', string)
166
167 # hostserv help entries
168 help_strings = []
169
170 for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
171 for fname in files:
172 filepath = subdir + os.sep + fname
173 if fname == 'hostserv.go':
174 content = open(filepath, 'r', encoding='UTF-8').read()
175
176 matches = re.findall(r'\`([^\`]+)\`', content)
177 for match in matches:
178 if match not in help_strings:
179 help_strings.append(match)
180
181 for s in ignored_strings:
182 try:
183 help_strings.remove(s)
184 except ValueError:
185 # ignore any that don't exist
186 ...
187
188 print("hostserv help strings:", len(help_strings))
189 with open(os.path.join(arguments['<languages-dir>'], 'example', 'hostserv.lang.json'), 'w') as f:
190 f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
191 f.write('\n')
192
193 for string in help_strings:
194 if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
195 print(' confirm:', string)