Skip to content

Instantly share code, notes, and snippets.

@kevinknights29
Created July 6, 2023 03:22
Show Gist options
  • Save kevinknights29/e5de07a0bea33dc72bf941a7095af13b to your computer and use it in GitHub Desktop.
Save kevinknights29/e5de07a0bea33dc72bf941a7095af13b to your computer and use it in GitHub Desktop.
Config loader
import logging
import sys
import yaml
from yaml.loader import SafeLoader
from project.utils.constants import LOGGING_FORMAT
from project.utils.constants import CONFIG_FILE
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter(LOGGING_FORMAT)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
conf: dict = {}
def config() -> dict:
"""
Loads the config.yaml file and returns a dict
stored in config
Returns:
dict: configuration dictionary
"""
global conf
if not conf:
with open(CONFIG_FILE, encoding="utf-8") as cfg:
conf = yaml.load(cfg, Loader=SafeLoader)
logger.log(logging.INFO, "Config loaded")
return conf
@kevinknights29
Copy link
Author

import os


# Directories
UTILS_FOLDER = os.path.dirname(os.path.abspath(__file__))
PROJECT_PACKAGE = os.path.dirname(UTILS_FOLDER)
ROOT = os.path.dirname(PROJECT_PACKAGE)
MISC_FOLDER = os.path.join(ROOT, "misc")
DATA_FOLDER = os.path.join(ROOT, "data")

# Files
CONFIG_FILE = os.path.join(ROOT, "config.yaml")

# Logging
LOGGING_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment