/
fix_it_noun_invariable.py
68 lines (60 loc) · 1.88 KB
/
fix_it_noun_invariable.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pywikibot, re, sys, argparse
import blib
from blib import getparam, rmparam, msg, site, tname
def process_page(page, index, parsed):
pagetitle = str(page.title())
def pagemsg(txt):
msg("Page %s %s: %s" % (index, pagetitle, txt))
pagemsg("Processing")
notes = []
for t in parsed.filter_templates():
tn = tname(t)
origt = str(t)
if (tn == "head" and getparam(t, "1") == "it" and getparam(t, "2") in ["noun", "nouns"] and
getparam(t, "3") == "invariable"):
must_continue = False
g = None
g2 = None
head = None
for param in t.params:
pname = str(param.name).strip()
pval = str(param.value).strip()
showkey = param.showkey
if pname in ["1", "2", "3"]:
pass
elif pname == "g":
g = pval
elif pname == "g2":
g2 = pval
elif pname == "head":
head = pval
else:
pagemsg("WARNING: Saw unrecognized param %s: %s" % (pname, str(t)))
must_continue = True
break
if must_continue:
continue
if not g:
pagemsg("WARNING: Didn't see gender: %s" % str(t))
continue
origt = str(t)
del t.params[:]
blib.set_template_name(t, "it-noun")
if head:
t.add("head", head)
t.add("1", g)
if g2:
t.add("g2", g2)
t.add("2", "-")
notes.append("replace {{head|it|noun|...|invariable}} with {{it-noun|...|-}}")
newt = str(t)
if origt != newt:
pagemsg("Replaced %s with %s" % (origt, newt))
return str(parsed), notes
parser = blib.create_argparser("Replace {{head|it|noun|...|invariable}} with {{it-noun|...|-}}",
include_pagefile=True)
args = parser.parse_args()
start, end = blib.parse_start_end(args.start, args.end)
blib.do_pagefile_cats_refs(args, start, end, process_page, edit=True)