-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.py
78 lines (63 loc) · 2.5 KB
/
startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*-coding:utf-8 -*-
"""
@File : startup.py
@Time : 2022/08/14 10:29:51
@Author : Aditya Divakaran
@Version : 1.0
@Contact : [email protected]
@License : MIT License, Aditya Divakaran
@Desc : Startup file that gets env vars, logging settings etc. for the whole project
"""
import os
import sys
import logging
# Valid but pylint acts up
from pydantic import BaseModel, ValidationError # pylint: disable=no-name-in-module
# Load .env setup
# Note: Remove this setup in case of docker environment
from dotenv import load_dotenv
load_dotenv(".env") # Load .env file once
# Using pydantic for robust casting (1, True, on, true / 0, False, off, false)
class EnvLoader(BaseModel): # pylint: disable=too-few-public-methods
"""Env Loading Class - Meant to act as a single blueprint for loading env
variables and having validation checks for each variable loaded through the
pydantic class
Args:
BaseModel (pydantic.BaseModel): Base Class for pydantic
"""
debug_mode: bool
def __init__():
"""Wrapper for commands to be run during app startup"""
# Get the initial env to understand if we should run in debug mode
try:
debug_mode: bool = EnvLoader(debug_mode=os.environ["DEBUG_MODE"]).debug_mode
except ValidationError as valerr:
sys.stderr.write("Env vars values are not valid. Please check Env Variables!")
sys.stderr.write(str(valerr))
sys.exit(1)
except KeyError as keyerr:
sys.stderr.write("Env vars could not be set. Please check Env Variables!")
sys.stderr.write(str(keyerr))
sys.exit(1)
# Set up logging according to mode set
if debug_mode:
logging_debug_level = logging.DEBUG
logging_format = "[%(asctime)s] %(levelname)s \
[%(name)s - %(filename)s.%(funcName)s:%(lineno)d] %(message)s"
else:
logging_debug_level = logging.INFO
logging_format = "[%(asctime)s] %(levelname)s [%(name)s] %(message)s"
log_file_path: str = "data/app.log"
# Note: Path to file handler is relative to root script that's run! Can be set as absolute path
logging.basicConfig(
format=logging_format,
datefmt="%a, %d %b %Y %H:%M:%S",
level=logging_debug_level,
handlers=[
logging.FileHandler(log_file_path, mode="w"),
logging.StreamHandler(sys.stderr),
],
)
log = logging.getLogger(__name__)
_ = log.info("DEBUG Mode") if debug_mode else log.info("DEBUG mode disabled")