Added util file and trying to fight the circular import errors

This commit is contained in:
Frisk 2021-04-25 13:37:59 +02:00
parent 2aa8387b72
commit 2b2eeaafba
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
4 changed files with 52 additions and 12 deletions

View file

@ -18,7 +18,6 @@ import math
from src.discord.message import DiscordMessage
from src.api import formatter
from src.i18n import rc_formatters
from src.api.client import Client
from src.api.context import Context
from src.configloader import settings
from src.exceptions import *

View file

@ -13,18 +13,19 @@
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import src.rcgcdw
import src.misc
from typing import Union
from collections import OrderedDict
class Client:
"""
A client for interacting with RcGcDw when creating formatters or hooks.
"""
def __init__(self):
self._formatters = src.rcgcdw.formatter_hooks
self.__recent_changes = src.rcgcdw.wiki
def __init__(self, hooks, wiki):
self._formatters = hooks
self.__recent_changes = wiki
self.WIKI_API_PATH = src.misc.WIKI_API_PATH
self.WIKI_ARTICLE_PATH = src.misc.WIKI_ARTICLE_PATH
self.WIKI_SCRIPT_PATH = src.misc.WIKI_SCRIPT_PATH
@ -61,6 +62,3 @@ class Client:
def get_formatters(self):
return self._formatters
client = Client()

40
src/api/util.py Normal file
View file

@ -0,0 +1,40 @@
# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
#
# RcGcDw is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RcGcDw is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import re
from urllib.parse import quote
from typing import Optional, Callable
from src.discord.message import DiscordMessage
from src.configloader import settings
import src.misc
def default_message(event: str, formatter_hooks: dict) -> Callable:
"""Returns a method of a formatter responsible for the event or None if such does not exist."""
return formatter_hooks.get(event, formatter_hooks.get("generic", formatter_hooks["no_formatter"]))
def link_formatter(link: str) -> str:
"""Formats a link to not embed it"""
return "<" + quote(link.replace(" ", "_"), "/:?=&") + ">"
def escape_formatting(data: str) -> str:
"""Escape Discord formatting"""
return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0)
def create_article_path(article: str) -> str:
"""Takes the string and creates an URL with it as the article name"""
return src.misc.WIKI_ARTICLE_PATH.replace("$1", article)

View file

@ -24,12 +24,12 @@ import time, logging.config, requests, datetime, gettext, math, os.path, schedul
import src.misc
from collections import defaultdict, Counter
import src.api.client
from typing import Optional
import src.api.client
from src.api.context import Context
from src.configloader import settings
from src.misc import add_to_dict, datafile, \
WIKI_API_PATH, LinkParser
WIKI_API_PATH
from src.api.util import create_article_path, default_message
from src.discord.queue import send_to_discord
from src.discord.message import DiscordMessage, DiscordMessageMetadata
@ -60,6 +60,7 @@ def load_extensions():
except ImportError:
logger.critical("No extensions module found. What's going on?")
sys.exit(1)
storage = datafile
# Remove previous data holding file if exists and limitfetch allows
@ -229,11 +230,12 @@ def day_overview():
def rc_processor(change, changed_categories):
"""Prepares essential information for both embed and compact message format."""
from src.misc import LinkParser
LinkParser = LinkParser()
metadata = DiscordMessageMetadata("POST", rev_id=change.get("revid", None), log_id=change.get("logid", None),
page_id=change.get("pageid", None))
logger.debug(change)
context = Context(settings["appearance"]["mode"], settings["webhookURL"], src.api.client.client)
context = Context(settings["appearance"]["mode"], settings["webhookURL"], client)
if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression
context.event = "suppressed"
discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change)
@ -269,6 +271,7 @@ def abuselog_processing(entry, recent_changes):
# Log in and download wiki information
wiki = Wiki(rc_processor, abuselog_processing)
client = src.api.client.Client(formatter_hooks, wiki)
try:
if settings["wiki_bot_login"] and settings["wiki_bot_password"]:
wiki.log_in()