Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
🚧 Logger, road for NTGNguyeen
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 18, 2024
1 parent 695e704 commit f4ce202
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 8 deletions.
18 changes: 14 additions & 4 deletions config-sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
# IT WON'T ASK YOU FOR CONFIGING AGAIN WHILE RUNNING THE SCRIPT / APP
# THE VALUE MUST BE IN QUOTATION MARKS 'STRING' AND 'True' OR 'False'

LINK = '' # Link of the first picture
OVERWRITE_BOOK = 'True' # If the folder of images / PDF file of the book is already exist, then overwrite it
CREATE_PDF = 'True' # Merge images to a PDF
KEEP_IMGS = 'False' # Keep downloaded images, not delete them
# THE LINK CAN BE LIKE THIS
# LINK = [
# LINK1,
# LINK2
# ]

LINK = [] # A list of links of one image of the book (any page)
OVERWRITE_BOOK = '' # [True, False] If the folder of images / PDF file of the book is already exist, then overwrite it
CREATE_PDF = '' # [True, False] Merge images to a PDF
KEEP_IMGS = '' # [True, False] Keep downloaded images, not delete them

# ADVANCED CONFIG
LOG = '' # [True, False] Log the process to the log folder
LOG_LEVEL = '' # [DEBUG, INFO, WARNING, ERROR, CRITICAL] Log level
5 changes: 5 additions & 0 deletions src/modules/createPDF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Merge all downloaded images into a single PDF file"""

from PIL import Image

...
5 changes: 5 additions & 0 deletions src/modules/downloadImages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Download all images of the book from a given URL of one page"""

from requests import get

...
48 changes: 44 additions & 4 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
from os import get_terminal_size
from os import get_terminal_size, getenv
from typing import Literal
from logging import basicConfig
from datetime import datetime
from dotenv import load_dotenv
from utils import createDirectory
from utils.printColor import printWarning, printInfo


LOGGER_MODE = Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
DOTENV_TRUE_VALUES: list[str] = ['True', 'true', '1']
DOTENV_FALSE_VALUES: list[str] = ['False', 'false', '0', '', ' ']
TERMINAL_SIZE_COLUMNS: int = get_terminal_size().columns


def pause() -> None:
"""Pause the system
"""Pause the terminal until user hits Enter
Params:
- None
Returns:
- None
"""
_: str = input("Press Enter to continue . . .")
_: str = input('Press Enter to continue . . .')


TERMINAL_SIZE_COLUMNS: int = get_terminal_size().columns
def setupLogger(level: str | None = 'INFO') -> None:
"""Setup the logger
Params:
- level (str | None): The level to log at. Only accept DEBUG, INFO, WARNING, ERROR, CRITICAL
Returns:
- None
"""
createDirectory('logs')
file_name: str = datetime.now().strftime(format='%Y-%m-%d-%H-%M-%S') + '.log'
basicConfig(
filename=f'log/{file_name}',
format="%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d",
level=level
)


# SETUP LOGGER
load_dotenv(dotenv_path='config.env')
if getenv(key='LOG') in DOTENV_TRUE_VALUES:
LOG_LEVEL: str | None = getenv(key='LOG_LEVEL')
if LOG_LEVEL is None:
printWarning(
message='LOG_LEVEL is not set in config.env. Defaulting to INFO')
setupLogger()
else:
printInfo(message=f'LOG_LEVEL is set to {LOG_LEVEL}')
setupLogger(level=LOG_LEVEL)
12 changes: 12 additions & 0 deletions src/utils/printColor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ def printRetry(message: str) -> None:
- None
"""
printColor(message, tag='Retry', color='blue')


def printInfo(message: str) -> None:
"""Print out info message with yan color
Args:
- Message (str): Message of Info
Returns:
- None
"""
printColor(message, tag='Info', color='yan')
33 changes: 33 additions & 0 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Contains utility functions for the project"""


from os import makedirs, path, removedirs


def createDirectory(*directories: str) -> None:
"""Create directories if they do not exist
Params:
- *directories (str): The directories to create
Returns:
- None
"""
for directory in directories:
if not path.exists(path=directory):
makedirs(name=directory)


def reCreateDirectory(*directories: str) -> None:
"""Delete directories and recreate them
Params:
- *directories (str): The directories to recreate
Returns:
- None
"""
for directory in directories:
if path.exists(path=directory):
removedirs(name=directory)
makedirs(name=directory)

0 comments on commit f4ce202

Please sign in to comment.