-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_user_files.py
189 lines (159 loc) · 5.77 KB
/
generate_user_files.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
# -*- coding: utf-8 -*-
""" Script to create user files (user-config.py, user-fixes.py) """
#
# (C) Pywikibot team, 2008-2013
#
# Distributed under the terms of the MIT license
#
__version__ = '$Id: 8a5263d1d68803be8a0f10875bc4c1889712ba61 $'
#
import codecs
import os
import re
import sys
console_encoding = sys.stdout.encoding
if console_encoding is None or sys.platform == 'cygwin':
console_encoding = "iso-8859-1"
def listchoice(clist=[], message=None, default=None):
if not message:
message = "Select"
if default:
message += " (default: %s)" % default
message += ": "
for n, i in enumerate(clist):
print ("%d: %s" % (n + 1, i))
while True:
choice = raw_input(message)
if choice == '' and default:
return default
try:
choice = int(choice)
except ValueError:
pass
if isinstance(choice, basestring):
if not choice in clist:
print("Invalid response")
else:
return choice
try:
return clist[int(choice) - 1]
except:
if not isinstance(choice, basestring):
print("Invalid response")
return response
def file_exists(filename):
if os.path.exists(filename):
print("'%s' already exists." % filename)
return True
return False
def create_user_config(base_dir):
_fnc = os.path.join(base_dir, "user-config.py")
if not file_exists(_fnc):
known_families = re.findall(r'(.+)_family.py\b',
'\n'.join(os.listdir(
os.path.join(base_dir, "families"))))
fam = listchoice(known_families,
"Select family of sites we are working on, " \
"just enter the number not name",
default='wikipedia')
codesds = codecs.open("families/%s_family.py"
% fam, "r", "utf-8").read()
rre = re.compile("self\.languages\_by\_size *\= *(.+?)\]", re.DOTALL)
known_langs = []
if not rre.findall(codesds):
rre = re.compile("self\.langs *\= *(.+?)\}", re.DOTALL)
if rre.findall(codesds):
import ast
known_langs = ast.literal_eval(
rre.findall(codesds)[0] + u"}").keys()
else:
known_langs = eval(rre.findall(codesds)[0] + u"]")
print "This is the list of known language(s):"
print " ".join(sorted(known_langs))
mylang = raw_input(
"The language code of the site we're working on (default: 'en'): "
) or 'en'
username = raw_input("Username (%s %s): "
% (mylang, fam)) or 'UnnamedBot'
username = unicode(username, console_encoding)
while True:
choice = raw_input(
"Which variant of user_config.py:\n"
"[S]mall or [E]xtended (with further information)? ").upper()
if choice in "SE":
break
#
# I don't like this solution. Temporary for me.
#
f = codecs.open(os.path.join(base_dir, "config.py"), "r", "utf-8")
cpy = f.read()
f.close()
res = re.findall("^(############## (?:LOGFILE|"
"INTERWIKI|"
"SOLVE_DISAMBIGUATION|"
"IMAGE RELATED|"
"TABLE CONVERSION BOT|"
"WEBLINK CHECKER|"
"DATABASE|"
"SEARCH ENGINE|"
"COPYRIGHT|"
"FURTHER) SETTINGS .*?)^(?=#####|# =====)",
cpy, re.MULTILINE | re.DOTALL)
config_text = '\n'.join(res)
f = codecs.open(_fnc, "w", "utf-8")
if choice == 'E':
f.write("""# -*- coding: utf-8 -*-
# This is an automatically generated file. You can find more configuration
# parameters in 'config.py' file.
# The family of sites we are working on. wikipedia.py will import
# families/xxx_family.py so if you want to change this variable,
# you need to write such a file.
family = '%s'
# The language code of the site we're working on.
mylang = '%s'
# The dictionary usernames should contain a username for each site where you
# have a bot account.
usernames['%s']['%s'] = u'%s'
%s""" % (fam, mylang, fam, mylang, username, config_text))
else:
f.write("""# -*- coding: utf-8 -*-
family = '%s'
mylang = '%s'
usernames['%s']['%s'] = u'%s'
""" % (fam, mylang, fam, mylang, username))
f.close()
print("'%s' written." % _fnc)
def create_user_fixes(base_dir):
_fnf = os.path.join(base_dir, "user-fixes.py")
if not file_exists(_fnf):
f = codecs.open(_fnf, "w", "utf-8")
f.write(r"""# -*- coding: utf-8 -*-
#
# This is only an example. Don't use it.
#
fixes['example'] = {
'regex': True,
'msg': {
'_default':u'no summary specified',
},
'replacements': [
(ur'\bword\b', u'two words'),
]
}
""")
f.close()
print("'%s' written." % _fnf)
if __name__ == "__main__":
print("1: Create user_config.py file (required)")
print("2: Create user_fixes.py file (optional, for advanced usage)")
print("3: The two files")
choice = raw_input("What do you do? Just enter the number: ")
if choice == "1":
create_user_config('')
if choice == "2":
create_user_fixes('')
if choice == "3":
create_user_config('')
create_user_fixes('')
if not choice in "123":
print("Nothing to do")