GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v3 9/9] enum: Use the created Platform class enumeration
Date: Fri, 21 Oct 2022 20:45:20 +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]>
---
 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 6a9e5c8..b7a7aaf 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 = manage_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(
-- 
Muhammad Rizki


      parent reply	other threads:[~2022-10-21 13:45 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21 13:45 [PATCH v3 0/9] Fix some bugs and add some features Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 1/9] discord: Add send_text_mail_interaction() Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 2/9] discord: Add send_patch_mail_interaction() Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 3/9] discord: Add get lore mail slash command Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 4/9] atom: Improve remove_patch() Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 5/9] atom: add manage_payload() Muhammad Rizki
2022-10-21 14:01   ` Ammar Faizi
2022-10-21 14:02     ` Muhammad Rizki
2022-10-21 14:06       ` Ammar Faizi
2022-10-21 14:07         ` Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 6/9] telegram: Fix get lore command Muhammad Rizki
2022-10-21 13:45 ` [PATCH v3 7/9] atom: Improve extract_body() Muhammad Rizki
2022-10-21 14:34   ` Alviro Iskandar Setiawan
2022-10-21 13:45 ` [PATCH v3 8/9] enum: Add Platform enumeration Muhammad Rizki
2022-10-21 14:02   ` Ammar Faizi
2022-10-21 14:04     ` Muhammad Rizki
2022-10-21 14:21       ` Ammar Faizi
2022-10-21 14:34         ` Muhammad Rizki
2022-10-21 14:45           ` Ammar Faizi
2022-10-21 14:54             ` Muhammad Rizki
2022-10-21 15:07               ` Ammar Faizi
2022-10-21 15:10                 ` Muhammad Rizki
2022-10-21 15:08               ` Ammar Faizi
2022-10-21 15:12                 ` Muhammad Rizki
2022-10-21 15:21                   ` Ammar Faizi
2022-10-21 15:35                     ` Muhammad Rizki
2022-10-21 15:37                       ` Ammar Faizi
2022-10-21 15:54                         ` Muhammad Rizki
2022-10-21 15:59                           ` Ammar Faizi
2022-10-21 15:39                       ` Ammar Faizi
2022-10-21 15:45                         ` Muhammad Rizki
2022-10-21 15:48                           ` Ammar Faizi
2022-10-21 18:29                             ` Muhammad Rizki
2022-10-21 18:53                               ` Ammar Faizi
2022-10-21 19:05                                 ` Alviro Iskandar Setiawan
2022-10-21 19:15                                   ` Ammar Faizi
2022-10-21 19:24                                     ` Alviro Iskandar Setiawan
2022-10-21 19:07                                 ` Muhammad Rizki
2022-10-21 17:03                     ` Alviro Iskandar Setiawan
2022-10-21 13:45 ` 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