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

Commit

Permalink
🎨 Split setup dotenv & logger in the logical way
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 18, 2024
1 parent f4ce202 commit b91535b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 50 deletions.
2 changes: 1 addition & 1 deletion VNULIB-Downloader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""VNULIB Downloader"""

from src.utils import pause
from src.utils.utils import pause
from src.utils.printIntro import printIntro


Expand Down
3 changes: 3 additions & 0 deletions src/CONSTANTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
██████╔╝╚██████╔╝╚███╔███╔╝██║ ╚████║███████╗╚██████╔╝██║ ██║██████╔╝███████╗██║ ██║
╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═╝
"""
LOGGER_MODE: list[str] = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
DOTENV_TRUE_VALUES: list[str] = ['True', 'true', '1']
DOTENV_FALSE_VALUES: list[str] = ['False', 'false', '0', '', ' ']
55 changes: 6 additions & 49 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
from os import get_terminal_size, getenv
from typing import Literal
from logging import basicConfig
from datetime import datetime
from os import get_terminal_size
from dotenv import load_dotenv
from utils import createDirectory
from utils.printColor import printWarning, printInfo
from setupDotEnv import setupConfigEnv
from setupLogger import setupLogger


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 terminal until user hits Enter
Params:
- None
Returns:
- None
"""
_: str = input('Press Enter to continue . . .')


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
setupConfigEnv()
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)

setupLogger()
26 changes: 26 additions & 0 deletions src/utils/setupDotEnv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Setup .env file for the project."""


from os import path
from requests import get
from printColor import printInfo


def setupConfigEnv(file_name: str = 'config.env') -> None:
"""Setup the config.env file
Params:
- file_name (str): The name of the config file (Default: config.env)
Returns:
- None
"""
if not path.exists(path=file_name):
printInfo(
message=f'{file_name} does not exist. Fetch the config from repository')
with open(file=file_name, mode='w', encoding='utf-8') as file:
content = get(
url='https://raw.githubusercontent.com/KevinNitroG/VNULIB-Downloader/main/config-sample.env',
allow_redirects=True
).content.decode(encoding='utf-8')
file.write(content)
47 changes: 47 additions & 0 deletions src/utils/setupLogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Setup the logger"""


from os import getenv
from logging import basicConfig
from datetime import datetime
from utils import createDirectory
from utils.printColor import printWarning, printInfo
from ..CONSTANTS import DOTENV_TRUE_VALUES


def setupLoggerNow(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
)


def setupLogger() -> None:
"""Check to decide whether to setup the logger or not
Params:
- None
Returns:
- None
"""
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')
setupLoggerNow()
else:
printInfo(message=f'LOG_LEVEL is set to {LOG_LEVEL}')
setupLoggerNow(level=LOG_LEVEL)
12 changes: 12 additions & 0 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
from os import makedirs, path, removedirs


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


def createDirectory(*directories: str) -> None:
"""Create directories if they do not exist
Expand Down

0 comments on commit b91535b

Please sign in to comment.