-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_user_files.py
152 lines (125 loc) · 4.57 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
# -*- coding: utf-8 -*-
""" Script to create user files (user-config.py, user-fixes.py) """
__version__ = '$Id$'
import codecs, os, re, 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:
return clist[int(choice) - 1]
except:
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",
default='wikipedia')
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")
print("2: Create user_fixes.py file")
print("3: The two files")
choice = raw_input("What do you do? ")
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")