Skip to content

feat: add environment and logger #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ endif
venv3: ### Creates a virtual environment for this project
test -d $(VENV) || python3.8 -m venv $(VENV)
$(PIP) install --upgrade pip wheel setuptools twine
$(PIP) install -r requirements.txt
$(PIP) install -r requirements-dev.txt

clean: clean-build clean-pyc ### Cleans artifacts
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-dotenv==1.0.0
42 changes: 42 additions & 0 deletions src/core/ydata/core/common/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
from argparse import ArgumentParser
from enum import Enum

from dotenv import load_dotenv


class Environment(Enum):
DEV = "development"
PROD = "production"

@classmethod
def detect(cls, parser: ArgumentParser = ArgumentParser(description='parse environment')):
parser.add_argument('--env', type=str, required=False)

args, _ = parser.parse_known_args()
env_string = args.env or 'prod'

if env_string in ('dev', 'development'):
env_string = 'development'
elif env_string in ('prod', 'production'):
env_string = 'production'
else:
raise ValueError('missing environment')

return cls(env_string)

def load(self):
# load the file specific to the environment
load_dotenv(f'.env.{str(self.name).lower()}')
load_dotenv(f'.env.{self.value}')

# Load the .env file if exists with default values
load_dotenv()

@staticmethod
def get(key: str, default=None):
return os.getenv(key, default=default)

@staticmethod
def set(key: str, value: str):
os.environ[key] = value
15 changes: 15 additions & 0 deletions src/core/ydata/core/common/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
from typing import TextIO
import sys


def create_logger(name, stream: TextIO = sys.stdout, level=logging.INFO):
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter("%(asctime)s | %(levelname)s | %(module)s:%(lineno)d | %(message)s"))

logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = False

return logger