From: Muhammad Rizki <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
Ammar Faizi <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH 09/28] enum: Use the created Platform class enumeration
Date: Tue, 20 Dec 2022 06:52:07 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
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 <[email protected]>
Link: https://lore.gnuweeb.org/gwml/[email protected]
Cc: Alviro Iskandar Setiawan <[email protected]>
Cc: Ammar Faizi <[email protected]>
Cc: GNU/Weeb Mailing List <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
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 <[email protected]>
#
+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<code>{'-'*72}</code>"
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(
--
2.34.1.windows.1
prev parent reply other threads:[~2022-12-19 23:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-19 23:51 [PATCH 01/28] discord: Add send_text_mail_interaction() Muhammad Rizki
2022-12-19 23:52 ` [PATCH 02/28] discord: Add send_patch_mail_interaction() Muhammad Rizki
2022-12-19 23:52 ` [PATCH 03/28] discord: Add get lore mail slash command Muhammad Rizki
2022-12-19 23:52 ` [PATCH 04/28] atom: Improve remove_patch() Muhammad Rizki
2022-12-19 23:52 ` [PATCH 05/28] atom: add get_decoded_payload() Muhammad Rizki
2022-12-19 23:52 ` [PATCH 06/28] telegram: Fix get lore command Muhammad Rizki
2022-12-19 23:52 ` [PATCH 07/28] atom: Improve extract_body() Muhammad Rizki
2022-12-19 23:52 ` [PATCH 08/28] enum: Add Platform enumeration Muhammad Rizki
2022-12-19 23:52 ` Muhammad Rizki [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox