Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions newapi/DB_bots/pymysql_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

import copy
import logging

import pymysql
import pymysql.cursors

from ..api_utils.except_err import exception_err
logger = logging.getLogger(__name__)


def sql_connect_pymysql(
Expand All @@ -24,7 +25,7 @@ def sql_connect_pymysql(
try:
connection = pymysql.connect(**args, **credentials)
except Exception as e:
exception_err(e)
logger.exception(e)
return []

with connection as conn, conn.cursor() as cursor:
Expand All @@ -36,13 +37,14 @@ def sql_connect_pymysql(
cursor.execute(query, params)

except Exception as e:
exception_err(e)
logger.exception(e)
return []

try:
results = cursor.fetchall()
except Exception as e:
exception_err(e)
logger.exception(e)
logger.exception('Exception during fetchall', exc_info=True)
return []

return results
42 changes: 29 additions & 13 deletions newapi/api_utils/ask_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
from ...api_utils.ask_bot import ASK_BOT

"""

import sys
import difflib

from . import printe
import logging
logger = logging.getLogger(__name__)

yes_answer = ["y", "a", "", "Y", "A", "all", "aaa"]

Save_or_Ask = {}


def showDiff(text, newtext):
diff = difflib.unified_diff(text.splitlines(), newtext.splitlines(), lineterm="")
for line in diff:
logger.info(line)


class ASK_BOT:
def __init__(self):
pass

def ask_put(self, nodiff=False, newtext="", text="", message="", job="Genral", username="", summary=""):
def ask_put(
self,
nodiff=False,
newtext="",
text="",
message="",
job="Genral",
username="",
summary="",
):
"""
Prompts the user to confirm saving changes to a page, optionally displaying a diff.

Expand All @@ -36,29 +52,29 @@ def ask_put(self, nodiff=False, newtext="", text="", message="", job="Genral", u
if text or newtext:
if "nodiff" not in sys.argv and not nodiff:
if len(newtext) < 70000 and len(text) < 70000 or "diff" in sys.argv:
printe.showDiff(text, newtext)
showDiff(text, newtext)
else:
printe.output("showDiff error..")
logger.info("showDiff error..")
# ---
printe.output(f"diference in bytes: {len(newtext) - len(text):,}")
printe.output(f"len of text: {len(text):,}, len of newtext: {len(newtext):,}")
logger.info(f"diference in bytes: {len(newtext) - len(text):,}")
logger.info(f"len of text: {len(text):,}, len of newtext: {len(newtext):,}")
# ---
if summary:
printe.output(f"-Edit summary: {summary}")
logger.info(f"-Edit summary: {summary}")
# ---
printe.output(f"<<lightyellow>>ASK_BOT: {message}? (yes, no) {username=}")
logger.info(f"<<lightyellow>>ASK_BOT: {message}? (yes, no) {username=}")
# ---
sa = input("([y]es, [N]o, [a]ll)?")
# ---
if sa == "a":
Save_or_Ask[job] = True
# ---
printe.output("<<lightgreen>> ---------------------------------")
printe.output(f"<<lightgreen>> save all:{job} without asking.")
printe.output("<<lightgreen>> ---------------------------------")
logger.info("<<lightgreen>> ---------------------------------")
logger.info(f"<<lightgreen>> save all:{job} without asking.")
logger.info("<<lightgreen>> ---------------------------------")
# ---
if sa not in yes_answer:
printe.output("wrong answer")
logger.info("wrong answer")
return False
# ---
return True
14 changes: 7 additions & 7 deletions newapi/api_utils/bot_edit/bot_edit_by_time.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
""" """

import datetime
import logging

from .. import printe

logger = logging.getLogger(__name__)
Created_Cache = {}


Expand Down Expand Up @@ -42,8 +42,8 @@ def check_create_time(page, title_page):
wait_time = delay_hours - diff
# ---
if diff < delay_hours:
printe.output(f"<<yellow>>Page:{title_page} create at ({create_time}).")
printe.output(f"<<invert>>Page Created before {diff:.2f} hours by: {user}, wait {wait_time:.2f}H.")
logger.info(f"<<yellow>>Page:{title_page} create at ({create_time}).")
logger.info(f"<<invert>>Page Created before {diff:.2f} hours by: {user}, wait {wait_time:.2f}H.")
return False
# ---
return True
Expand Down Expand Up @@ -76,13 +76,13 @@ def check_last_edit_time(page, title_page, delay):
# ---
diff_minutes = (now - ts_time).total_seconds() / 60
# ---
# printe.output(f"<<grey>> last-edit Δ={diff_minutes:.2f} min for {title_page}")
# logger.info(f"<<grey>> last-edit Δ={diff_minutes:.2f} min for {title_page}")
# ---
wait_time = delay - diff_minutes
# ---
if diff_minutes < delay:
printe.output(f"<<yellow>>Page:{title_page} last edit ({timestamp}).")
printe.output(
logger.info(f"<<yellow>>Page:{title_page} last edit ({timestamp}).")
logger.info(
f"<<invert>>Page Last edit before {delay} minutes, Wait {wait_time:.2f} minutes. title:{title_page}"
)
return False
Expand Down
23 changes: 7 additions & 16 deletions newapi/api_utils/except_err.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
"""

python3 bots/new/newapi/except_err.py

from .except_err import exception_err
from .except_err import exception_err, warn_err

"""

import inspect
import logging
import traceback
from warnings import warn

try:
from . import printe
except Exception:
import printe
logger = logging.getLogger(__name__)


def warn_err(err):
Expand All @@ -36,17 +28,16 @@ def exception_err(e, text=""):
if not isinstance(text, str):
text = str(text)
# ---
printe.output("<<yellow>> start exception_err:")
logger.info("<<yellow>> start :")
# ---
printe.error("Traceback (most recent call last):")
logger.error("Traceback (most recent call last):")
warn(warn_err(f"Exception:{str(e)}"), UserWarning, stacklevel=3)
printe.warn(text)
logger.warning(text)
# ---
if str(e) not in common_errors:
# ---
err = traceback.format_exc(limit=4)
err = str(err).replace("Traceback (most recent call last):", "").strip()
printe.warn(err)
logger.warning(err)
# ---
printe.warn("CRITICAL:")
# printe.info("====")
logger.critical("CRITICAL:")
30 changes: 18 additions & 12 deletions newapi/api_utils/printe.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
"""
This module provides functions for printing colored text and showing differences between two texts.
The main functions are `output` and `showDiff`.

Example usage:
# To print colored text
printe.output('<<red>>red') # prints 'red' in red color
# To show differences between two texts
printe.showDiff('old text', 'new text') # prints the differences between 'old text' and 'new text'
"""

import difflib
Expand Down Expand Up @@ -344,7 +336,10 @@ class Hunk:
PENDING = 0

def __init__(
self, a: str | Sequence[str], b: str | Sequence[str], grouped_opcode: Sequence[tuple[str, int, int, int, int]]
self,
a: str | Sequence[str],
b: str | Sequence[str],
grouped_opcode: Sequence[tuple[str, int, int, int, int]],
) -> None:
"""
Initializer.
Expand Down Expand Up @@ -539,7 +534,12 @@ def get_header_text(a_rng: tuple[int, int], b_rng: tuple[int, int], affix: str =

class PatchManager:
def __init__(
self, text_a: str, text_b: str, context: int = 0, by_letter: bool = False, replace_invisible: bool = False
self,
text_a: str,
text_b: str,
context: int = 0,
by_letter: bool = False,
replace_invisible: bool = False,
) -> None:
self.a = text_a.splitlines(True)
self.b = text_b.splitlines(True)
Expand Down Expand Up @@ -633,8 +633,14 @@ def _get_context_range(self, super_hunk: _Superhunk) -> tuple[tuple[int, int], t
a0, a1 = super_hunk.a_rng
b0, b1 = super_hunk.b_rng
return (
(a0 - min(super_hunk.pre_context, self.context), a1 + min(super_hunk.post_context, self.context)),
(b0 - min(super_hunk.pre_context, self.context), b1 + min(super_hunk.post_context, self.context)),
(
a0 - min(super_hunk.pre_context, self.context),
a1 + min(super_hunk.post_context, self.context),
),
(
b0 - min(super_hunk.pre_context, self.context),
b1 + min(super_hunk.post_context, self.context),
),
)

def _generate_diff(self, hunks: _Superhunk) -> str:
Expand Down
4 changes: 1 addition & 3 deletions newapi/api_utils/txtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
# for temp in temps: name, namestrip, params, template = temp['name'], temp['namestrip'], temp['params'], temp['item']

"""
# from ..other_bots import printe

from functools import lru_cache

import wikitextparser as wtp
Expand Down Expand Up @@ -42,7 +40,7 @@ def extract_templates_and_params(text):
# ---
name = str(template.normal_name()).strip()
pa_item = template.string
# printe.output( "<<lightyellow>> pa_item: %s" % pa_item )
# logger.info( "<<lightyellow>> pa_item: %s" % pa_item )
# ---
namestrip = name
# ---
Expand Down
2 changes: 1 addition & 1 deletion newapi/api_utils/user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ def default_user_agent():
# ---
li = f"{tool} bot/1.0 (https://{tool}.toolforge.org/; tools.{tool}@toolforge.org)"
# ---
# printe.output(f"default_user_agent: {li}")
# logger.info(f"default_user_agent: {li}")
# ---
return li
12 changes: 8 additions & 4 deletions newapi/api_utils/wd_sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

"""

import logging
import sys

from SPARQLWrapper import JSON, SPARQLWrapper

logger = logging.getLogger(__name__)


def get_query_data(query):
"""Retrieve query data from the Wikidata SPARQL endpoint.
Expand All @@ -34,7 +37,10 @@ def get_query_data(query):
# endpoint_url = "https://query-main.wikidata.org/sparql"
endpoint_url = "https://query.wikidata.org/sparql"
# ---
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
user_agent = "WDQS-example Python/%s.%s" % (
sys.version_info[0],
sys.version_info[1],
)
# ---
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
# ---
Expand All @@ -46,9 +52,7 @@ def get_query_data(query):
try:
data = sparql.query().convert()
except Exception as e:
# exception_err(e, text=f"API/tools.py quoteurl: Exception: {e}")
print("API/tools.py get_query_data: Exception: e:")
print(e)
logger.error(f"API/tools.py : Exception: {e}")
# ---
return data

Expand Down
Loading