pathlib support

This commit is contained in:
Profitroll 2023-06-23 11:17:02 +02:00
parent 88d8a38444
commit 23467a88ef
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
4 changed files with 11 additions and 11 deletions

View File

@ -66,12 +66,12 @@ async def compress_image(image_path: str):
return return
task.start() task.start()
logWrite(f"Compressing '{path.split(image_path)[-1]}'...") logWrite(f"Compressing '{Path(image_path).name}'...")
task.join() task.join()
size_after = path.getsize(image_path) / 1024 size_after = path.getsize(image_path) / 1024
logWrite( 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"
) )

View File

@ -1,12 +1,11 @@
import re import re
from datetime import datetime, timezone 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 secrets import token_urlsafe
from shutil import move from shutil import move
from typing import Union from typing import Union
from pathlib import Path
import aiofiles import aiofiles
from bson.errors import InvalidId from bson.errors import InvalidId
from bson.objectid import ObjectId from bson.objectid import ObjectId

View File

@ -37,7 +37,7 @@ def dynamic_import(module_name, py_path):
def dynamic_import_from_src(src, star_import=False): def dynamic_import_from_src(src, star_import=False):
my_py_files = get_py_files(src) my_py_files = get_py_files(src)
for py_file in my_py_files: 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) print(f"Importing {module_name} extension...", flush=True)
imported_module = dynamic_import(module_name, py_file) imported_module = dynamic_import(module_name, py_file)
if imported_module != None: if imported_module != None:

View File

@ -1,3 +1,4 @@
from pathlib import Path
from traceback import print_exc from traceback import print_exc
from typing import Any, Union from typing import Any, Union
@ -11,11 +12,11 @@ def logWrite(message: str, debug: bool = False) -> None:
print(f"{message}", flush=True) print(f"{message}", flush=True)
def jsonLoad(filepath: str) -> Any: def jsonLoad(filepath: Union[str, Path]) -> Any:
"""Load json file """Load json file
### Args: ### Args:
* filepath (`str`): Path to input file * filepath (`Union[str, Path]`): Path to input file
### Returns: ### Returns:
* `Any`: Some json deserializable * `Any`: Some json deserializable
@ -37,12 +38,12 @@ def jsonLoad(filepath: str) -> Any:
return output 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 """Save contents into json file
### Args: ### Args:
* contents (`Union[list, dict]`): Some json serializable * contents (`Union[list, dict]`): Some json serializable
* filepath (`str`): Path to output file * filepath (`Union[str, Path]`): Path to output file
""" """
try: try:
with open(filepath, "w", encoding="utf8") as file: with open(filepath, "w", encoding="utf8") as file:
@ -63,7 +64,7 @@ def configGet(key: str, *args: str) -> Any:
### Returns: ### Returns:
* `Any`: Value of provided key * `Any`: Value of provided key
""" """
this_dict = jsonLoad("config.json") this_dict = jsonLoad(Path("config.json"))
this_key = this_dict this_key = this_dict
for dict_key in args: for dict_key in args:
this_key = this_key[dict_key] this_key = this_key[dict_key]