This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Split setup dotenv & logger in the logical way
- Loading branch information
1 parent
f4ce202
commit b91535b
Showing
6 changed files
with
95 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters