-
Notifications
You must be signed in to change notification settings - Fork 0
/
demoguy_navigational_templates.py
executable file
·229 lines (176 loc) · 6.98 KB
/
demoguy_navigational_templates.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
#!/usr/bin/env python2
from __future__ import print_function
import sys
import os
import time
import datetime
import argparse
import traceback
import collections
import itertools
import tempfile
import subprocess
import touhouwiki.arrangement_cd
import touhouwiki.arrangement_cd_parser
import touhouwiki.wikitext_utils
import interactive_diff
import pywikibot
import pywikibot.catlib
import pywikibot.pagegenerators
default_category_name = "Category:" + touhouwiki.arrangement_cd_parser.ArrangementCDParser.category_name
def normalize_title(title):
return pywikibot.Link(title).canonical_title()
def get_pages(cat=None):
if not cat:
cat = default_category_name
assert cat.startswith("Category:")
pages = pywikibot.catlib.Category(pywikibot.getSite(), cat).articles()
# TODO filter out pages tha don't belong to `default_category_name'
# if `cat' is set
# TODO could be done better with special generators
return (page for page in pywikibot.pagegenerators.PreloadingGenerator(pages)
if page.namespace() == 0 # only main namespace
)
def interactive_edit(article_name, old_text, new_text):
backup = (old_text, new_text)
while True:
print(article_name)
interactive_diff.diff(old_text, new_text)
print("eo - edit old")
print("en - edit new")
print("s - save")
print("n - next (do nothing)")
print("r - reset")
x = raw_input("Action: ").strip()
if x in ("eo", "edit old"):
new_text = interactive_diff.run_diff_editor([old_text, new_text], ["old", "generated"])
elif x in ("en", "edit new"):
new_text = interactive_diff.run_diff_editor([new_text, old_text], ["generated", "old"])
elif x in ("s", "save"):
return new_text
elif x in ("r", "reset"):
old_text, new_text = backup
elif x in ("n", "next"):
return None
else:
print("unknown command")
class NavigationalTemplateUpdater(object):
def __init__(self, force, cat):
self.force = force
self.cat = cat
self.group_cats = collections.defaultdict(list)
def on_album(self, page, cd):
"""Extracts required data for later batch update"""
assert isinstance(page, pywikibot.Page)
assert isinstance(cd, touhouwiki.arrangement_cd.ArrangementCD)
for group_cat in cd.groupCats:
group_cat = normalize_title("Category:" + group_cat)
self.group_cats[group_cat].append((page, cd))
def is_up_to_date(self, pagecds):
return False
def update_categories(self):
for group_cat, pagecds in self.group_cats.iteritems():
self.update_category(group_cat, pagecds)
def update_category(self, group_cat, pagecds):
assert group_cat.startswith("Category:")
if self.cat and self.cat != group_cat:
return
if not self.force and self.is_up_to_date(pagecds):
return
group_cat = group_cat.replace("Category:", "")
template = pywikibot.Page(
pywikibot.getSite(), u"Template:%s Albums" % group_cat)
try:
pagecds.sort(key=lambda pagecd: (pagecd[1].released or datetime.date(datetime.MINYEAR, 1, 1), pagecd[1].catalogno))
except Exception as e:
print("Category:", group_cat, file=sys.stderr)
traceback.print_exc()
return
try:
old_text = template.get(get_redirect=True)
except pywikibot.exceptions.NoPage:
old_text = u""
new_text = self.generate_template(
pagecds,
template
)
if new_text.strip() == old_text.strip():
print("Skipping not changed", template.title())
return
result = interactive_edit(template.title(), old_text, new_text)
if result is not None:
template.put(result, u"updating (semi-automatic mode)", minorEdit=False, botflag=False)
def generate_template(self, pagecds, old_template):
l = []
if len(pagecds) > 1:
try:
old_dict = touhouwiki.wikitext_utils.find_template(old_template.get(get_redirect=True), "Navbox")
if old_dict is None:
old_dict = {}
except pywikibot.exceptions.NoPage:
old_dict = {}
l.append(u"{{Navbox\n")
l.append(u"|type = music\n")
l.append(u"|name = {{subst:PAGENAME}}\n")
l.append(u"|title = %s\n" %
(old_dict.get("title", "").strip() or u"{{subst:PAGENAME}}"))
for k, v in old_dict.iteritems():
if (k == "name" or
k == "title" or
k == "type" or
k.startswith("group") or
k.startswith("list")):
continue
l.append(u"|%s = %s\n" % (k.strip(), v.strip()))
cur_idx = cur_year = 0
for page, cd in pagecds:
if cd.released is None or cd.released.year > cur_year:
if cd.released is None:
cur_year = None
else:
cur_year = cd.released.year
cur_idx += 1
l.append(u"\n")
l.append(u"|group%d = %s\n" % (cur_idx, unicode(cur_year or "Unknown")))
l.append(u"|list%d = %s" % (cur_idx, self.get_link(page, cd)))
else:
l.append(u" • %s" % self.get_link(page, cd))
l.append(u"\n}}\n")
l.append(u"<noinclude>\n")
l.append(u"[[Category:Arrange CDs navigational templates]]\n")
l.append(u"</noinclude>");
res = u"".join(l)
# replace subst:PAGENAME here to avoid cluttering the diff
res = res.replace(u"{{subst:PAGENAME}}", old_template.title(withNamespace=False))
return res
def get_link(self, page, cd):
if page.title() == cd.title:
return u"[[%s]]" % page.title()
else:
return u"[[%s | %s]]" % (page.title(), cd.title)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("source", nargs="?",
help="Category to load pages from")
parser.add_argument("--force", dest="force", action="store_true")
args = parser.parse_args()
if args.source:
args.source = normalize_title(args.source)
parser = touhouwiki.arrangement_cd_parser.get_parser()
updater = NavigationalTemplateUpdater(
force=args.force,
cat=args.source)
for page in get_pages(args.source):
try:
print(page.title())
cd = parser.parse_page(page.get())
except Exception as exc:
print(exc)
else:
updater.on_album(page, cd)
updater.update_categories()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()