From a30d6719604f51219d838fcfa3f9562b65d59d7f Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sun, 31 Dec 2023 20:34:12 +0300 Subject: [PATCH] more code cleanup --- app/Config.py | 13 +++++++++++-- app/Download.py | 3 +-- app/DownloadQueue.py | 3 +-- app/Notifier.py | 26 -------------------------- app/Utils.py | 29 +++++++++++++++++++++++++++++ app/main.py | 9 ++------- 6 files changed, 44 insertions(+), 39 deletions(-) delete mode 100644 app/Notifier.py diff --git a/app/Config.py b/app/Config.py index 8a115360..fb721409 100644 --- a/app/Config.py +++ b/app/Config.py @@ -4,6 +4,7 @@ import os import re import sys import coloredlogs +from version import APP_VERSION class Config: @@ -33,7 +34,10 @@ class Config: logging_level: str = 'info' - _boolean_vars: tuple = ('keep_archive', 'ytdl_debug') + version: str = APP_VERSION + + _boolean_vars: tuple = ('keep_archive', 'ytdl_debug',) + _immutable: tuple = ('version',) def __init__(self): baseDefualtPath: str = os.path.dirname(os.path.dirname(__file__)) @@ -46,6 +50,11 @@ class Config: if k.startswith('_'): continue + # If the variable is not updateable, set it to the default value. + if k in self._immutable: + setattr(self, k, v) + continue + lookUpKey: str = f'YTP_{k}'.upper() setattr( self, k, @@ -53,7 +62,7 @@ class Config: ) for k, v in self.__dict__.items(): - if k.startswith('_'): + if k.startswith('_') or k in self._immutable: continue if isinstance(v, str) and '{' in v and '}' in v: diff --git a/app/Download.py b/app/Download.py index 429bb130..261b37d9 100644 --- a/app/Download.py +++ b/app/Download.py @@ -7,9 +7,8 @@ import os import re import shutil import yt_dlp -from Utils import get_format, get_opts, jsonCookie, mergeConfig +from Utils import Notifier, get_format, get_opts, jsonCookie, mergeConfig from ItemDTO import ItemDTO -from Notifier import Notifier log = logging.getLogger('download') diff --git a/app/DownloadQueue.py b/app/DownloadQueue.py index 187f6b30..3f68d3f3 100644 --- a/app/DownloadQueue.py +++ b/app/DownloadQueue.py @@ -6,11 +6,10 @@ import time import yt_dlp from sqlite3 import Connection from Config import Config -from Notifier import Notifier from Download import Download from ItemDTO import ItemDTO from DataStore import DataStore -from Utils import ObjectSerializer, calcDownloadPath, ExtractInfo, mergeConfig +from Utils import Notifier, ObjectSerializer, calcDownloadPath, ExtractInfo, mergeConfig from datetime import datetime, timezone log = logging.getLogger('DownloadQueue') diff --git a/app/Notifier.py b/app/Notifier.py deleted file mode 100644 index 2af377b4..00000000 --- a/app/Notifier.py +++ /dev/null @@ -1,26 +0,0 @@ -from socketio import AsyncServer -from Utils import ObjectSerializer - - -class Notifier: - def __init__(self, sio: AsyncServer, serializer: ObjectSerializer): - self.sio = sio - self.serializer = serializer - - async def added(self, dl): - await self.emit('added', dl) - - async def updated(self, dl): - await self.emit('updated', dl) - - async def completed(self, dl): - await self.emit('completed', dl) - - async def canceled(self, id): - await self.emit('canceled', id) - - async def cleared(self, id): - await self.emit('cleared', id) - - async def emit(self, event: str, data): - await self.sio.emit(event, self.serializer.encode(data)) diff --git a/app/Utils.py b/app/Utils.py index fdb969e7..7584079b 100644 --- a/app/Utils.py +++ b/app/Utils.py @@ -5,6 +5,7 @@ import logging import os from typing import Any import yt_dlp +from socketio import AsyncServer log = logging.getLogger('Utils') @@ -274,3 +275,31 @@ class ObjectSerializer(json.JSONEncoder): def default(self, obj): return obj.__dict__ if isinstance(obj, object) else json.JSONEncoder.default(self, obj) + + +class Notifier: + ''' + This class is used to send events to the frontend. + ''' + + def __init__(self, sio: AsyncServer, serializer: ObjectSerializer): + self.sio = sio + self.serializer = serializer + + async def added(self, dl): + await self.emit('added', dl) + + async def updated(self, dl): + await self.emit('updated', dl) + + async def completed(self, dl): + await self.emit('completed', dl) + + async def canceled(self, id): + await self.emit('canceled', id) + + async def cleared(self, id): + await self.emit('cleared', id) + + async def emit(self, event: str, data): + await self.sio.emit(event, self.serializer.encode(data)) diff --git a/app/main.py b/app/main.py index 3fe4d74e..62f8941c 100644 --- a/app/main.py +++ b/app/main.py @@ -6,10 +6,8 @@ import json import os import random from Config import Config -from version import APP_VERSION -from Notifier import Notifier from DownloadQueue import DownloadQueue -from Utils import ObjectSerializer +from Utils import ObjectSerializer, Notifier from aiohttp import web from aiohttp.web import Request, Response from player.M3u8 import M3u8 @@ -20,9 +18,6 @@ import caribou import sqlite3 from aiocron import crontab -log: logging.Logger = None - - class Main: config: Config = None serializer: ObjectSerializer = None @@ -36,7 +31,6 @@ class Main: def __init__(self): self.config = Config() - self.config.version = APP_VERSION self.logger = logging.getLogger('main') try: @@ -113,6 +107,7 @@ class Main: ) async def connect(self, sid, _): + self.logger.info(f'Config [{self.config.__dict__}].') data: dict = { **self.dqueue.get(), "config": self.config,