From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.123]) by gnuweeb.org (Postfix) with ESMTPSA id D93088060C; Sat, 22 Oct 2022 06:52:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666421548; bh=YwXEXiNMruF967mlb2TEloGjZRpriEw+6A0PQIDt3wI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z0o+7Brp05XJJ33Ngy84uybsWdxp0D6ZGpMJaiBtoKUrI6TvRADDfTz00XGkQqXXN gwLtmB1ajmRsxZyu+V7Kyg+laugsD/P75O2evppdyUtaIDdjfVhfjHVE5i0ziYkDhe 5I9gb/UPF22xtSzmoj+67PE6VAGe5d8/pMh2huotgUN9ncEz1bgKy5YUHxJhhAs1jp XBKDXfYQWUXr8nU0rGh1Gy862I+LVG0SyxhXI5rw0ZNC+BoGnvyrKC1f1EF9vcGi0S sHWwLXhhcItTm8Ae6rHtQNe88VXbz6UEAJLo38hvb4vyGZ+r91FBeZ7j9ggtT/oyeN ktNFtJCk5Rojg== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v4 9/9] enum: Use the created Platform class enumeration Date: Sat, 22 Oct 2022 13:51:49 +0700 Message-Id: <20221022065149.865-10-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221022065149.865-1-kiizuha@gnuweeb.org> References: <20221022065149.865-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Use the Platform class enumeration to make it more practice, avoid getting typo, type-hinted suggestion to easily manageable-maintainable. Signed-off-by: Muhammad Rizki --- daemon/atom/utils.py | 28 ++++++++----------- daemon/dscord/gnuweeb/client.py | 5 ++-- .../plugins/slash_commands/get_lore_mail.py | 3 +- daemon/dscord/mailer/listener.py | 3 +- daemon/telegram/mailer/listener.py | 4 +-- daemon/telegram/packages/client.py | 3 +- .../packages/plugins/commands/scrape.py | 4 +-- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/daemon/atom/utils.py b/daemon/atom/utils.py index 4383b03..03453b4 100644 --- a/daemon/atom/utils.py +++ b/daemon/atom/utils.py @@ -4,6 +4,8 @@ # Copyright (C) 2022 Ammar Faizi # +from enums import Platform + from pyrogram.types import Chat, InlineKeyboardMarkup, InlineKeyboardButton from email.message import Message from typing import Dict, Union @@ -115,19 +117,13 @@ def consruct_to_n_cc(to: list, cc: list): return ret -def gen_temp(name: str, platform: str): - platform = platform.lower() - plt_ls = ["telegram", "discord"] - - if platform not in plt_ls: - t = f"Platform {platform} is not found, " - t += f"only {', '.join(plt_ls)} is available" - raise ValueError(f"Platform {platform} is not found") - +def gen_temp(name: str, platform: Platform): + platform: str = platform.value md5 = hashlib.md5(name.encode()).hexdigest() store_dir = os.getenv("STORAGE_DIR", "storage") platform = platform.replace("discord", "dscord") path = f"{platform}/{store_dir}/{md5}" + try: os.mkdir(path) except FileExistsError: @@ -136,11 +132,11 @@ def gen_temp(name: str, platform: str): return path -def extract_body(thread: Message, platform: str): +def extract_body(thread: Message, platform: Platform): if not thread.is_multipart(): p = get_decoded_payload(thread) - if platform == "discord": + if platform is Platform.DISCORD: p = quote_reply(p) return f"{p}\n".lstrip(), [] @@ -183,12 +179,12 @@ def __is_patch(subject, content): return True -def create_template(thread: Message, platform: str, to=None, cc=None): +def create_template(thread: Message, platform: Platform, to=None, cc=None): if not to: to = extract_list("to", thread) if not cc: cc = extract_list("cc", thread) - if platform == "telegram": + if platform is Platform.TELEGRAM: substr = 4000 border = f"\n{'-'*72}" else: @@ -211,13 +207,13 @@ def create_template(thread: Message, platform: str, to=None, cc=None): if len(ret) >= substr: ret = ret[:substr] + "..." - ret = fix_utf8_char(ret, platform == "telegram") + ret = fix_utf8_char(ret, platform is Platform.TELEGRAM) ret += border return ret, files, is_patch -def prepare_patch(mail, text, url, platform: str): +def prepare_patch(mail, text, url, platform: Platform): tmp = gen_temp(url, platform) fnm = str(mail.get("subject")) sch = re.search(PATCH_PATTERN, fnm, re.IGNORECASE) @@ -237,7 +233,7 @@ def prepare_patch(mail, text, url, platform: str): f.write(bytes(text, encoding="utf8")) caption = "#patch #ml" - if platform == "telegram": + if platform is Platform.TELEGRAM: caption += fix_utf8_char("\n" + cap, True) return tmp, file, caption, url diff --git a/daemon/dscord/gnuweeb/client.py b/daemon/dscord/gnuweeb/client.py index b921f7d..82858c5 100644 --- a/daemon/dscord/gnuweeb/client.py +++ b/daemon/dscord/gnuweeb/client.py @@ -13,6 +13,7 @@ from typing import Union from . import filters from . import models from atom import utils +from enums import Platform from dscord.database import DB @@ -59,7 +60,7 @@ class GWClient(commands.Bot): reply_to: Union[int, None] = None, url: str = None): print("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "discord" + mail, text, url, Platform.DISCORD ) channel = self.get_channel(chat_id) @@ -90,7 +91,7 @@ class GWClient(commands.Bot): async def send_patch_mail_interaction(self, mail, i: "Interaction", text: str, url: str = None): tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "discord" + mail, text, url, Platform.DISCORD ) m = await i.response.send_message( content=caption, diff --git a/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py b/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py index 2d55d16..0c67b8c 100644 --- a/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py +++ b/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py @@ -11,6 +11,7 @@ from discord import app_commands from atom import utils from atom import Scraper +from enums import Platform class GetLoreSC(commands.Cog): @@ -26,7 +27,7 @@ class GetLoreSC(commands.Cog): async def get_lore(self, i: "Interaction", url: str): s = Scraper() mail = await s.get_email_from_url(url) - text, _, is_patch = utils.create_template(mail, "discord") + text, _, is_patch = utils.create_template(mail, Platform.DISCORD) if is_patch: m = await self.bot.send_patch_mail_interaction( diff --git a/daemon/dscord/mailer/listener.py b/daemon/dscord/mailer/listener.py index cc0a9f7..d986fbd 100644 --- a/daemon/dscord/mailer/listener.py +++ b/daemon/dscord/mailer/listener.py @@ -14,6 +14,7 @@ from discord import Message from dscord.gnuweeb import GWClient from atom.scraper import Scraper from atom import utils +from enums import Platform class Mutexes: @@ -107,7 +108,7 @@ class Listener: # return False - text, files, is_patch = utils.create_template(mail, "discord") + text, files, is_patch = utils.create_template(mail, Platform.DISCORD) reply_to = self.get_discord_reply(mail, dc_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 08feddd..1c92f23 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -9,8 +9,8 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from telegram.packages import DaemonClient from atom import Scraper from atom import utils +from enums import Platform import asyncio -import shutil import re import traceback @@ -99,7 +99,7 @@ class Bot(): # return False - text, files, is_patch = utils.create_template(mail, "telegram") + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) reply_to = self.get_reply(mail, tg_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py index 686e5ef..c971ea1 100644 --- a/daemon/telegram/packages/client.py +++ b/daemon/telegram/packages/client.py @@ -9,6 +9,7 @@ from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton from typing import Union from email.message import Message from atom import utils +from enums import Platform from telegram.database import DB from .decorator import handle_flood @@ -57,7 +58,7 @@ class DaemonClient(Client): ) -> Message: print("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "telegram" + mail, text, url, Platform.TELEGRAM ) m = await self.send_document( chat_id=chat_id, diff --git a/daemon/telegram/packages/plugins/commands/scrape.py b/daemon/telegram/packages/plugins/commands/scrape.py index 860e993..29cc8ad 100644 --- a/daemon/telegram/packages/plugins/commands/scrape.py +++ b/daemon/telegram/packages/plugins/commands/scrape.py @@ -9,8 +9,8 @@ from pyrogram import filters from telegram.packages import DaemonClient from atom import Scraper from atom import utils +from enums import Platform from telegram import config -import shutil import re import asyncio @@ -37,7 +37,7 @@ async def scrap_email(c: DaemonClient, m: Message): s = Scraper() mail = await s.get_email_from_url(url) - text, files, is_patch = utils.create_template(mail, "telegram") + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) if is_patch: m = await c.send_patch_email( -- Muhammad Rizki