Jump to content

User:CX Zoom AWB/Task 2

From Wikipedia, the free encyclopedia
CX Zoom AWB/ Task 2
Description Implementing behavior change for {{PresFoot}}, compliance with MOS:CHRONO
BRFA Wikipedia:Bots/Requests for approval/CX Zoom AWB 2
Status  In BRFA
Frequency Non-recurring
Type AWB, Python, Manual

Background

[edit]

{{PresFoot}} currently accepts parameters for the last row, as well as closes the table. This behaviour is undesirable. Hence, {{PresFoot}} is to be converted to a pure table closer, and the last row being replaced by the standard {{PresRow}} template.

Process

[edit]
  1. If {{PresFoot}} is present, change all existing cases of PresFoot(with parameters) to PresRow(with parameters). (2950 cases)
  2. Add {{PresFoot}} with no parameters to a new line after final {{PresRow}}. (same articles)
  3. While doing these changes it makes sense to bring these tables' usage into compliance with WP:CHRONO (earliest to latest).
  4. Remove stray table close in articles that have a PresFoot followed by an unnecessary stray table closer.

Resources

[edit]

Python code

[edit]
import re

f = "<masked>\\file.txt"

with open(f, "r", encoding = 'cp850') as file:
    text = file.read()

# Change last row to {{PresRow}} and end with {{PresFoot}}
pf_match = re.finditer(r"\{\{PresFoot\|(.*?)\}\}", text)
for i in pf_match:
    params = i.group(1)
    s = i.start()
    e = i.end()
    textpf = text[:s] + "{{PresRow|" + params + "}}\n{{PresFoot}}" + text[e:]

# Reorder {{PresRow}} chronologically
dic = {}
pr_match = re.finditer(r"\{\{PresRow\|(\d{4})\|(.*?)\}\}", textpf)

for i in pr_match:
    dic[i.group(1)] = i.group(2)
    s = i.start()
    e = i.end()
    break

for i in pr_match:
    dic[i.group(1)] = i.group(2)
    e = i.end()
    
dicl = sorted(dic.items(), key=lambda x: x[0])
textpr = textpf[:s]
for i in dicl:
    textpr += "{{PresRow|" + i[0] + "|" + i[1] + "}}\n"
textpr += textpf[e+1:]

# {| |} balancer
tprl = len(textpr)
textfin = ""
stack = []
i = 0
while i < tprl:
    if textpr[i:i+2] == '{|':
        stack.append(i)
        textfin += textpr[i:i+2]
        i += 2
    elif textpr[i:i+2] == '|}' and textpr[i:i+3] != '|}}':
        if stack:
            if textpr[stack[-1]:stack[-1]+2] == '{|':
                stack.pop()
                textfin += textpr[i:i+2]
        i += 2
    else:
        textfin += textpr[i]
        i += 1
textfin = textfin + (("\n" + ",".join(map(str, stack))) if len(stack) > 0 else "")

# Write the file
with open(f, "w", encoding = 'cp850') as file:
    file.write(textfin)