User:CX Zoom AWB/Task 2

{{User:CX Zoom AWB/infobox

| task = 2

| desc = Implementing behavior change for {{PresFoot}}, compliance with MOS:CHRONO

| status = brfa

| freq = non-rec

| type = AWB, Python, Manual

| editnum =

| editlink =

| lastrun =

| lastdiff =

}}

= Background =

{{main|Special:Permalink/1296419942#Behavior change for {{PresFoot}}}}

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

= Process =

  1. If {{tl|PresFoot}} is present, change all existing cases of PresFoot(with parameters) to PresRow(with parameters). (2950 cases)
  2. Add {{tl|PresFoot}} with no parameters to a new line after final {{tl|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 =

= Python code =

import re

f = "\\file.txt"

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

text = file.read()

  1. 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:]

  1. 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:]

  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 "")

  1. Write the file

with open(f, "w", encoding = 'cp850') as file:

file.write(textfin)