From 23467a88ef211242e87770730b701cbb1713d97b Mon Sep 17 00:00:00 2001 From: profitroll Date: Fri, 23 Jun 2023 11:17:02 +0200 Subject: [PATCH] pathlib support --- extensions/photos.py | 4 ++-- extensions/videos.py | 5 ++--- modules/extensions_loader.py | 2 +- modules/utils.py | 11 ++++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/extensions/photos.py b/extensions/photos.py index 1a0b338..b7c9da5 100644 --- a/extensions/photos.py +++ b/extensions/photos.py @@ -66,12 +66,12 @@ async def compress_image(image_path: str): return task.start() - logWrite(f"Compressing '{path.split(image_path)[-1]}'...") + logWrite(f"Compressing '{Path(image_path).name}'...") task.join() size_after = path.getsize(image_path) / 1024 logWrite( - f"Compressed '{path.split(image_path)[-1]}' from {size_before} Kb to {size_after} Kb" + f"Compressed '{Path(image_path).name}' from {size_before} Kb to {size_after} Kb" ) diff --git a/extensions/videos.py b/extensions/videos.py index 415d0d4..88aa50c 100644 --- a/extensions/videos.py +++ b/extensions/videos.py @@ -1,12 +1,11 @@ import re from datetime import datetime, timezone -from os import makedirs, path, remove +from os import makedirs, remove +from pathlib import Path from secrets import token_urlsafe from shutil import move from typing import Union -from pathlib import Path - import aiofiles from bson.errors import InvalidId from bson.objectid import ObjectId diff --git a/modules/extensions_loader.py b/modules/extensions_loader.py index 30043e0..32d72bb 100644 --- a/modules/extensions_loader.py +++ b/modules/extensions_loader.py @@ -37,7 +37,7 @@ def dynamic_import(module_name, py_path): def dynamic_import_from_src(src, star_import=False): my_py_files = get_py_files(src) for py_file in my_py_files: - module_name = path.split(py_file)[-1][:-3] + module_name = Path(py_file).stem print(f"Importing {module_name} extension...", flush=True) imported_module = dynamic_import(module_name, py_file) if imported_module != None: diff --git a/modules/utils.py b/modules/utils.py index 1cf907e..3f4a9cd 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -1,3 +1,4 @@ +from pathlib import Path from traceback import print_exc from typing import Any, Union @@ -11,11 +12,11 @@ def logWrite(message: str, debug: bool = False) -> None: print(f"{message}", flush=True) -def jsonLoad(filepath: str) -> Any: +def jsonLoad(filepath: Union[str, Path]) -> Any: """Load json file ### Args: - * filepath (`str`): Path to input file + * filepath (`Union[str, Path]`): Path to input file ### Returns: * `Any`: Some json deserializable @@ -37,12 +38,12 @@ def jsonLoad(filepath: str) -> Any: return output -def jsonSave(contents: Union[list, dict], filepath: str) -> None: +def jsonSave(contents: Union[list, dict], filepath: Union[str, Path]) -> None: """Save contents into json file ### Args: * contents (`Union[list, dict]`): Some json serializable - * filepath (`str`): Path to output file + * filepath (`Union[str, Path]`): Path to output file """ try: with open(filepath, "w", encoding="utf8") as file: @@ -63,7 +64,7 @@ def configGet(key: str, *args: str) -> Any: ### Returns: * `Any`: Value of provided key """ - this_dict = jsonLoad("config.json") + this_dict = jsonLoad(Path("config.json")) this_key = this_dict for dict_key in args: this_key = this_key[dict_key]