-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixpl.py
105 lines (86 loc) · 3.08 KB
/
fixpl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Convert plstem from 5th argument to 4th
import pywikibot, re, sys, codecs, argparse
import blib
from blib import getparam, rmparam
import rulib as ru
site = pywikibot.Site()
def msg(text):
print text.encode("utf-8")
def errmsg(text):
print >>sys.stderr, text.encode("utf-8")
def arg1_is_stress(arg1):
if not arg1:
return False
for arg in re.split(",", arg1):
if not (re.search("^[a-f]'?'?$", arg) or re.search(r"^[1-6]\*?$", arg)):
return False
return True
def process_page(templates, index, page, save=False, verbose=False):
pagetitle = unicode(page.title())
def pagemsg(txt):
msg("Page %s %s: %s" % (index, pagetitle, txt))
if not page.exists():
pagemsg("WARNING: Page doesn't exist")
return
parsed = blib.parse(page)
should_save = False
for t in parsed.filter_templates():
if unicode(t.name) in templates:
origt = unicode(t)
# Punt if multi-arg-set, can't handle yet
should_continue = False
for param in t.params:
if not param.showkey:
val = unicode(param.value)
if val == "or":
pagemsg("WARNING: Can't handle multi-decl templates: %s" % unicode(t))
should_continue = True
break
if val == "-" or val == "_" or val.startswith("join:"):
pagemsg("WARNING: Can't handle multi-word templates: %s" % unicode(t))
should_continue = True
break
if should_continue:
continue
if arg1_is_stress(getparam(t, "1")):
oldplarg = "5"
newplarg = "4"
else:
oldplarg = "4"
newplarg = "3"
plstem = getparam(t, oldplarg)
if plstem:
if getparam(t, newplarg):
pagemsg("WARNING: Something wrong, found args in both positions %s and %s: %s" %
(newplarg, oldplarg, unicode(t)))
continue
rmparam(t, oldplarg)
t.add(newplarg, plstem)
should_save = True
pagemsg("Replacing %s with %s" % (origt, unicode(t)))
if should_save:
comment = "Move plstem from 5th/4th argument to 4th/3rd"
if save:
pagemsg("Saving with comment = %s" % comment)
page.text = unicode(parsed)
page.save(comment=comment)
else:
pagemsg("Would save with comment = %s" % comment)
pa = argparse.ArgumentParser()
pa.add_argument("start", nargs="?", help="Start index", type=int)
pa.add_argument("end", nargs="?", help="Start index", type=int)
pa.add_argument("--save", help="Save pages", action="store_true")
pa.add_argument("--verbose", help="Output verbose messages", action="store_true")
pargs = pa.parse_args()
templates_to_do = ["ru-noun-table", "ru-noun-old", "ru-noun+", "ru-proper noun+"]
def yield_ref_pages():
for template in templates_to_do:
for i, page in blib.references("Template:" + template, pargs.start or None,
pargs.end or None):
yield i, page
do_pages = yield_ref_pages()
for i, page in do_pages:
msg("Page %s %s: Processing" % (i, unicode(page.title())))
process_page(templates_to_do, i, page, save=pargs.save, verbose=pargs.verbose)