Browse Source

More Python 3.11+ fixes

main
Ben Kurtovic 8 months ago
parent
commit
2b66476ff7
3 changed files with 25 additions and 23 deletions
  1. +4
    -4
      tasks/afc_copyvios.py
  2. +8
    -7
      tasks/afc_statistics.py
  3. +13
    -12
      tasks/drn_clerkbot.py

+ 4
- 4
tasks/afc_copyvios.py View File

@@ -21,10 +21,10 @@
from hashlib import sha256
from os.path import expanduser
from threading import Lock
from urllib import quote
from urllib.parse import quote

import mwparserfromhell
import oursql
import pymysql

from earwigbot.tasks import Task

@@ -70,7 +70,7 @@ class AfCCopyvios(Task):
title = kwargs["page"]
page = self.bot.wiki.get_site().get_page(title)
with self.db_access_lock:
self.conn = oursql.connect(**self.conn_data)
self.conn = pymysql.connect(**self.conn_data)
self.process(page)

def process(self, page):
@@ -139,7 +139,7 @@ class AfCCopyvios(Task):
msg = "Found violation: [[{0}]] -> {1} ({2} confidence)"
self.logger.info(msg.format(title, url, new_conf))
safeurl = quote(url.encode("utf8"), safe="/:").decode("utf8")
template = "\{\{{0}|url={1}|confidence={2}\}\}\n"
template = r"\{\{{0}|url={1}|confidence={2}\}\}\n"
template = template.format(self.template, safeurl, new_conf)
newtext = template + content
if "{url}" in self.summary:


+ 8
- 7
tasks/afc_statistics.py View File

@@ -26,7 +26,8 @@ from threading import Lock
from time import sleep

import mwparserfromhell
import oursql
import pymysql
import pymysql.cursors

from earwigbot import exceptions, wiki
from earwigbot.tasks import Task
@@ -49,7 +50,7 @@ class AfCStatistics(Task):
"""A task to generate statistics for WikiProject Articles for Creation.

Statistics are stored in a MySQL database ("u_earwig_afc_statistics")
accessed with oursql. Statistics are synchronied with the live database
accessed with pymysql. Statistics are synchronied with the live database
every four minutes and saved once an hour, on the hour, to subpages of
self.pageroot. In the live bot, this is "Template:AfC statistics".
"""
@@ -109,7 +110,7 @@ class AfCStatistics(Task):

try:
self.site = self.bot.wiki.get_site()
self.conn = oursql.connect(**self.conn_data)
self.conn = pymysql.connect(**self.conn_data)
self.revision_cache = {}
try:
if action == "save":
@@ -139,7 +140,7 @@ class AfCStatistics(Task):
summary = self.summary

statistics = self._compile_charts()
for name, chart in statistics.iteritems():
for name, chart in statistics.items():
self._save_page(name, chart, summary)

def _save_page(self, name, chart, summary):
@@ -171,7 +172,7 @@ class AfCStatistics(Task):
def _compile_charts(self):
"""Compile and return all statistics information from our local db."""
stats = OrderedDict()
with self.conn.cursor(oursql.DictCursor) as cursor:
with self.conn.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute("SELECT * FROM chart")
for chart in cursor:
name = chart["chart_name"]
@@ -186,7 +187,7 @@ class AfCStatistics(Task):
chart = "{{" + chart + "}}"

query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE row_chart = ?"
with self.conn.cursor(oursql.DictCursor) as cursor:
with self.conn.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute(query, (chart_info["chart_id"],))
rows = cursor.fetchall()
skipped = max(0, len(rows) - _PER_CHART_LIMIT)
@@ -449,7 +450,7 @@ class AfCStatistics(Task):
return

query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE page_id = ?"
with self.conn.cursor(oursql.DictCursor) as dict_cursor:
with self.conn.cursor(pymysql.cursors.DictCursor) as dict_cursor:
dict_cursor.execute(query, (pageid,))
result = dict_cursor.fetchall()[0]



+ 13
- 12
tasks/drn_clerkbot.py View File

@@ -24,7 +24,8 @@ from os.path import expanduser
from threading import RLock
from time import mktime, sleep, time

import oursql
import pymysql
import pymysql.cursors
from mwparserfromhell import parse as mw_parse

from earwigbot import exceptions
@@ -112,7 +113,7 @@ class DRNClerkBot(Task):
action = kwargs.get("action", "all")
try:
start = time()
conn = oursql.connect(**self.conn_data)
conn = pymysql.connect(**self.conn_data)
site = self.bot.wiki.get_site()
if action in ["all", "update_volunteers"]:
self.update_volunteers(conn, site)
@@ -155,7 +156,7 @@ class DRNClerkBot(Task):
text = text.split(marker)[1]
additions = set()
for line in text.splitlines():
user = re.search("\# \{\{User\|(.+?)\}\}", line)
user = re.search(r"\# \{\{User\|(.+?)\}\}", line)
if user:
uname = user.group(1).replace("_", " ").strip()
additions.add((uname[0].upper() + uname[1:],))
@@ -193,7 +194,7 @@ class DRNClerkBot(Task):
"""Read the noticeboard content and update the list of _Cases."""
nextid = self.select_next_id(conn)
tl_status_esc = re.escape(self.tl_status)
split = re.split("(^==\s*[^=]+?\s*==$)", text, flags=re.M | re.U)
split = re.split(r"(^==\s*[^=]+?\s*==$)", text, flags=re.M | re.U)
for i in range(len(split)):
if i + 1 == len(split):
break
@@ -201,17 +202,17 @@ class DRNClerkBot(Task):
continue
title = split[i][2:-2].strip()
body = old = split[i + 1]
if not re.search("\s*\{\{" + tl_status_esc, body, re.U):
if not re.search(r"\s*\{\{" + tl_status_esc, body, re.U):
continue
status = self.read_status(body)
re_id = "<!-- Bot Case ID \(please don't modify\): (.*?) -->"
re_id = r"<!-- Bot Case ID \(please don't modify\): (.*?) -->"
try:
id_ = int(re.search(re_id, body).group(1))
case = [case for case in cases if case.id == id_][0]
except (AttributeError, IndexError, ValueError):
id_ = nextid
nextid += 1
re_id2 = "(\{\{" + tl_status_esc
re_id2 = r"(\{\{" + tl_status_esc
re_id2 += (
r"(.*?)\}\})(<!-- Bot Case ID \(please don't modify\): .*? -->)?"
)
@@ -281,7 +282,7 @@ class DRNClerkBot(Task):
def read_status(self, body):
"""Parse the current status from a case body."""
templ = re.escape(self.tl_status)
status = re.search("\{\{" + templ + "\|?(.*?)\}\}", body, re.S | re.U)
status = re.search(r"\{\{" + templ + r"\|?(.*?)\}\}", body, re.S | re.U)
if not status:
return self.STATUS_NEW
for option, names in self.ALIASES.iteritems():
@@ -518,7 +519,7 @@ class DRNClerkBot(Task):
re_parties = "<span.*?>'''Users involved'''</span>(.*?)<span.*?>"
text = re.search(re_parties, case.body, re.S | re.U)
for line in text.group(1).splitlines():
user = re.search("[:*#]{,5} \{\{User\|(.*?)\}\}", line)
user = re.search(r"[:*#]{,5} \{\{User\|(.*?)\}\}", line)
if user:
party = user.group(1).replace("_", " ").strip()
if party.startswith("User:"):
@@ -550,7 +551,7 @@ class DRNClerkBot(Task):
case.last_action = case.status
new = self.ALIASES[case.status][0]
tl_status_esc = re.escape(self.tl_status)
search = "\{\{" + tl_status_esc + "(\|?.*?)\}\}"
search = r"\{\{" + tl_status_esc + r"(\|?.*?)\}\}"
repl = "{{" + self.tl_status + "|" + new + "}}"
case.body = re.sub(search, repl, case.body)

@@ -617,7 +618,7 @@ class DRNClerkBot(Task):

def save_existing_case(self, conn, case):
"""Save an existing case to the database, updating as necessary."""
with conn.cursor(oursql.DictCursor) as cursor:
with conn.cursor(pymysql.cursors.DictCursor) as cursor:
query = "SELECT * FROM cases WHERE case_id = ?"
cursor.execute(query, (case.id,))
stored = cursor.fetchone()
@@ -754,7 +755,7 @@ class DRNClerkBot(Task):
+ "|small={{{small|}}}|collapsed={{{collapsed|}}}}}\n"
)
query = "SELECT * FROM cases WHERE case_status != ?"
with conn.cursor(oursql.DictCursor) as cursor:
with conn.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute(query, (self.STATUS_UNKNOWN,))
for case in cursor:
chart += self.compile_row(case)


Loading…
Cancel
Save