Skip to content

Commit 7b49d21

Browse files
author
Renato Almeida
committed
feat: add environment and logger
1 parent f63868e commit 7b49d21

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ endif
1818
venv3: ### Creates a virtual environment for this project
1919
test -d $(VENV) || python3.8 -m venv $(VENV)
2020
$(PIP) install --upgrade pip wheel setuptools twine
21+
$(PIP) install -r requirements.txt
2122
$(PIP) install -r requirements-dev.txt
2223

2324
clean: clean-build clean-pyc ### Cleans artifacts

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-dotenv==1.0.0
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
from argparse import ArgumentParser
3+
from enum import Enum
4+
5+
from dotenv import load_dotenv
6+
7+
8+
class Environment(Enum):
9+
DEV = "development"
10+
PROD = "production"
11+
12+
@classmethod
13+
def detect(cls, parser: ArgumentParser = ArgumentParser(description='parse environment')):
14+
parser.add_argument('--env', type=str, required=False)
15+
16+
args, _ = parser.parse_known_args()
17+
env_string = args.env or 'prod'
18+
19+
if env_string in ('dev', 'development'):
20+
env_string = 'development'
21+
elif env_string in ('prod', 'production'):
22+
env_string = 'production'
23+
else:
24+
raise ValueError('missing environment')
25+
26+
return cls(env_string)
27+
28+
def load(self):
29+
# load the file specific to the environment
30+
load_dotenv(f'.env.{str(self.name).lower()}')
31+
load_dotenv(f'.env.{self.value}')
32+
33+
# Load the .env file if exists with default values
34+
load_dotenv()
35+
36+
@staticmethod
37+
def get(key: str, default=None):
38+
return os.getenv(key, default=default)
39+
40+
@staticmethod
41+
def set(key: str, value: str):
42+
os.environ[key] = value

src/core/ydata/core/common/logger.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import logging
2+
from typing import TextIO
3+
import sys
4+
5+
6+
def create_logger(name, stream: TextIO = sys.stdout, level=logging.INFO):
7+
handler = logging.StreamHandler(stream)
8+
handler.setFormatter(
9+
logging.Formatter(
10+
"%(asctime)s | %(levelname)s | %(module)s:%(lineno)d | %(message)s"
11+
)
12+
)
13+
14+
logger = logging.getLogger(name)
15+
logger.setLevel(level)
16+
logger.addHandler(handler)
17+
logger.propagate = False
18+
19+
return logger

0 commit comments

Comments
 (0)