forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_family_file.py
executable file
·301 lines (248 loc) · 9.89 KB
/
generate_family_file.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/python
"""This script generates a family file from a given URL.
Usage::
generate_family_file.py [<url>] [<name>] [<dointerwiki>] [<verify>]
Parameters are optional. They must be given consecutively but may be
omitted if there is no successor parameter. The parameters are::
<url>: an url from where the family settings are loaded
<name>: the family name without "_family.py" tail.
<dointerwiki>: predefined answer (y|n) to add multiple language
<verify>: disable certificate validaton `(y|n)
Example::
generate_family_file.py https://www.mywiki.bogus/wiki/Main_Page mywiki
This will create the file mywiki_family.py in pywikibot/families folder
"""
#
# (C) Pywikibot team, 2010-2021
#
# Distributed under the terms of the MIT license
#
import codecs
import os
import string
import sys
from os import environ, getenv
from typing import Optional
from urllib.parse import urlparse
# see pywikibot.family.py
# Legal characters for Family name and Family langs keys
NAME_CHARACTERS = string.ascii_letters + string.digits
# nds_nl code alias requires "_"n
# dash must be the last char to be reused as regex in update_linktrails
CODE_CHARACTERS = string.ascii_lowercase + string.digits + '_-'
class FamilyFileGenerator:
"""Family file creator object."""
def __init__(self,
url: Optional[str] = None,
name: Optional[str] = None,
dointerwiki: Optional[str] = None,
verify: Optional[str] = None):
"""
Parameters are optional. If not given the script asks for the values.
@param url: an url from where the family settings are loaded
@param name: the family name without "_family.py" tail.
@param dointerwiki: Predefined answer to add multiple language
codes. Pass `Y` or `y` for yes `N` or `n` for no and
`E` or `e` if you want to edit the collection of sites.
@param verify: If a certificate verification failes, you may
pass `Y` or `y` to disable certificate validaton `N` or `n`
to keep it enabled.
"""
# from pywikibot.site_detect import MWSite
# when required but disable user-config checks
# so the family can be created first,
# and then used when generating the user-config
self.Wiki = _import_with_no_user_config(
'pywikibot.site_detect').site_detect.MWSite
self.base_url = url
self.name = name
self.dointerwiki = dointerwiki
self.verify = verify
self.wikis = {} # {'https://wiki/$1': Wiki('https://wiki/$1'), ...}
self.langs = [] # [Wiki('https://wiki/$1'), ...]
def get_params(self):
"""Ask for parameters if necessary."""
if self.base_url is None:
self.base_url = input('Please insert URL to wiki: ')
if not self.base_url:
return False
if self.name is None:
self.name = input('Please insert a short name (eg: freeciv): ')
if not self.name:
return False
if any(x not in NAME_CHARACTERS for x in self.name):
print('ERROR: Name of family "{}" must be ASCII letters and '
'digits [a-zA-Z0-9]'.format(self.name))
return False
return True
def get_wiki(self):
"""Get wiki from base_url."""
import pywikibot
from pywikibot.exceptions import FatalServerError
print('Generating family file from ' + self.base_url)
for verify in (True, False):
try:
w = self.Wiki(self.base_url, verify=verify)
except FatalServerError:
print('ERROR: '
+ pywikibot.comms.http.SSL_CERT_VERIFY_FAILED_MSG)
pywikibot.exception()
if not pywikibot.bot.input_yn(
'Retry with disabled ssl certificate validation',
default=self.verify, automatic_quit=False,
force=self.verify is not None):
break
else:
return w, verify
return None, None
def run(self):
"""Main method, generate family file."""
if not self.get_params():
return
w, verify = self.get_wiki()
if w is None:
return
self.wikis[w.lang] = w
print('\n=================================='
'\nAPI url: {w.api}'
'\nMediaWiki version: {w.version}'
'\n==================================\n'.format(w=w))
self.getlangs(w)
self.getapis()
self.writefile(verify)
def getlangs(self, w):
"""Determine language of a site."""
print('Determining other languages...', end='')
try:
self.langs = w.langs
print(' '.join(sorted(wiki['prefix'] for wiki in self.langs)))
except Exception as e:
self.langs = []
print(e, '; continuing...')
if len([lang for lang in self.langs if lang['url'] == w.iwpath]) == 0:
if w.private_wiki:
w.lang = self.name
self.langs.append({'language': w.lang,
'local': '',
'prefix': w.lang,
'url': w.iwpath})
code_len = len(self.langs)
if code_len > 1:
if self.dointerwiki is None:
makeiw = input(
'\nThere are {} languages available.'
'\nDo you want to generate interwiki links? '
'This might take a long time. ([y]es/[N]o/[e]dit)'
.format(code_len)).lower()
else:
makeiw = self.dointerwiki
if makeiw == 'n':
self.langs = [wiki for wiki in self.langs
if wiki['url'] == w.iwpath]
elif makeiw == 'e':
for wiki in self.langs:
print(wiki['prefix'], wiki['url'])
do_langs = input('Which languages do you want: ')
self.langs = [wiki for wiki in self.langs
if wiki['prefix'] in do_langs
or wiki['url'] == w.iwpath]
for wiki in self.langs:
assert all(x in CODE_CHARACTERS for x in wiki['prefix']), \
'Family {} code {} must be ASCII lowercase ' \
'letters and digits [a-z0-9] or underscore/dash [_-]' \
.format(self.name, wiki['prefix'])
def getapis(self):
"""Load other language pages."""
print('Loading wikis... ')
for lang in self.langs:
key = lang['prefix']
print(' * {}... '.format(key), end='')
if key not in self.wikis:
try:
self.wikis[key] = self.Wiki(lang['url'])
print('downloaded')
except Exception as e:
print(e)
else:
print('in cache')
def writefile(self, verify):
"""Write the family file."""
fn = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'pywikibot', 'families',
'{}_family.py'.format(self.name))
print('Writing %s... ' % fn)
try:
open(fn)
if input('{} already exists. Overwrite? (y/n)'
.format(fn)).lower() == 'n':
print('Terminating.')
sys.exit(1)
except IOError: # file not found
pass
code_hostname_pairs = '\n '.join(
"'{code}': '{hostname}',".format(
code=k, hostname=urlparse(w.server).netloc
) for k, w in self.wikis.items())
code_path_pairs = '\n '.join(
"'{code}': '{path}',".format(code=k, path=w.scriptpath)
for k, w in self.wikis.items())
code_protocol_pairs = '\n '.join(
"'{code}': '{protocol}',".format(
code=k, protocol=urlparse(w.server).scheme
) for k, w in self.wikis.items())
content = family_template % {
'url': self.base_url, 'name': self.name,
'code_hostname_pairs': code_hostname_pairs,
'code_path_pairs': code_path_pairs,
'code_protocol_pairs': code_protocol_pairs}
if not verify:
# assuming this is the same for all codes
content += """
def verify_SSL_certificate(self, code: str) -> bool:
return False
"""
with codecs.open(fn, 'w', 'utf-8') as fh:
fh.write(content)
family_template = """\
\"\"\"
This family file was auto-generated by generate_family_file.py script.
Configuration parameters:
url = %(url)s
name = %(name)s
Please do not commit this to the Git repository!
\"\"\"
from pywikibot import family
class Family(family.Family): # noqa: D101
name = '%(name)s'
langs = {
%(code_hostname_pairs)s
}
def scriptpath(self, code):
return {
%(code_path_pairs)s
}[code]
def protocol(self, code):
return {
%(code_protocol_pairs)s
}[code]
"""
def _import_with_no_user_config(*import_args):
"""Return __import__(*import_args) without loading user-config.py."""
orig_no_user_config = getenv('PYWIKIBOT_NO_USER_CONFIG') or getenv(
'PYWIKIBOT2_NO_USER_CONFIG')
environ['PYWIKIBOT_NO_USER_CONFIG'] = '2'
result = __import__(*import_args)
# Reset this flag
if not orig_no_user_config:
del environ['PYWIKIBOT_NO_USER_CONFIG']
else:
environ['PYWIKIBOT_NO_USER_CONFIG'] = orig_no_user_config
return result
def main():
"""Process command line arguments and generate a family file."""
if len(sys.argv) > 1 and sys.argv[1] == '-help':
print(__doc__)
else:
FamilyFileGenerator(*sys.argv[1:]).run()
if __name__ == '__main__':
main()