adding data path handling on session level
This commit is contained in:
parent
72ede57a82
commit
dbe6e3b340
@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "delta-barth"
|
||||
version = "0.4.0dev0"
|
||||
version = "0.4.1"
|
||||
description = "workflows and pipelines for the Python-based Plugin of Delta Barth's ERP system"
|
||||
authors = [
|
||||
{name = "Florian Förster", email = "f.foerster@d-opt.com"},
|
||||
@ -73,7 +73,7 @@ directory = "reports/coverage"
|
||||
|
||||
|
||||
[tool.bumpversion]
|
||||
current_version = "0.4.0dev0"
|
||||
current_version = "0.4.1"
|
||||
parse = """(?x)
|
||||
(?P<major>0|[1-9]\\d*)\\.
|
||||
(?P<minor>0|[1-9]\\d*)\\.
|
||||
|
||||
@ -8,10 +8,12 @@ from dopt_basics.io import combine_route
|
||||
from pydantic import BaseModel
|
||||
from requests import Response
|
||||
|
||||
import delta_barth.logging
|
||||
from delta_barth.errors import (
|
||||
STATUS_HANDLER,
|
||||
UnspecifiedRequestType,
|
||||
)
|
||||
from delta_barth.logging import logger_session as logger
|
||||
from delta_barth.types import (
|
||||
ApiCredentials,
|
||||
DelBarApiError,
|
||||
@ -26,19 +28,40 @@ class Session:
|
||||
def __init__(
|
||||
self,
|
||||
base_headers: HttpContentHeaders,
|
||||
logging_folder: str = "logs",
|
||||
) -> None:
|
||||
self._data_path: Path | None = None
|
||||
self._logging_dir: Path | None = None
|
||||
self._logging_folder = logging_folder
|
||||
self._creds: ApiCredentials | None = None
|
||||
self._base_url: str | None = None
|
||||
self._headers = base_headers
|
||||
self._session_token: str | None = None
|
||||
self._logged_in: bool = False
|
||||
|
||||
def setup(self) -> None:
|
||||
self.setup_logging()
|
||||
|
||||
@property
|
||||
def data_path(self) -> Path:
|
||||
assert self._data_path is not None, "accessed data path not set"
|
||||
return self._data_path
|
||||
|
||||
@property
|
||||
def logging_dir(self) -> Path:
|
||||
if self._logging_dir is not None:
|
||||
return self._logging_dir
|
||||
|
||||
logging_dir = self.data_path / self._logging_folder
|
||||
if not logging_dir.exists():
|
||||
logging_dir.mkdir(parents=False)
|
||||
self._logging_dir = logging_dir
|
||||
return self._logging_dir
|
||||
|
||||
def setup_logging(self) -> None:
|
||||
delta_barth.logging.setup_logging(self.logging_dir)
|
||||
logger.info("[SESSION] Successfully setup logging")
|
||||
|
||||
@property
|
||||
def creds(self) -> ApiCredentials:
|
||||
assert self._creds is not None, "accessed credentials not set"
|
||||
|
||||
@ -18,6 +18,7 @@ DUMMY_DATA_PATH: Final[Path] = dummy_data_pth
|
||||
ENABLE_LOGGING: Final[bool] = False
|
||||
LOGGING_TO_FILE: Final[bool] = True
|
||||
LOGGING_TO_STDERR: Final[bool] = True
|
||||
LOG_FILENAME: Final[str] = "dopt-delbar.log"
|
||||
|
||||
|
||||
# ** error handling
|
||||
|
||||
@ -6,24 +6,46 @@ from pathlib import Path
|
||||
from time import gmtime
|
||||
from typing import Final
|
||||
|
||||
from delta_barth.constants import ENABLE_LOGGING, LIB_PATH, LOGGING_TO_FILE, LOGGING_TO_STDERR
|
||||
from delta_barth.constants import (
|
||||
ENABLE_LOGGING,
|
||||
LOG_FILENAME,
|
||||
LOGGING_TO_FILE,
|
||||
LOGGING_TO_STDERR,
|
||||
)
|
||||
|
||||
# ** config
|
||||
logging.Formatter.converter = gmtime
|
||||
LOG_FMT: Final[str] = "%(asctime)s | lang_main:%(module)s:%(levelname)s | %(message)s"
|
||||
LOG_DATE_FMT: Final[str] = "%Y-%m-%d %H:%M:%S +0000"
|
||||
LOG_FILE_FOLDER: Final[Path] = LIB_PATH / "logs"
|
||||
if not LOG_FILE_FOLDER.exists():
|
||||
LOG_FILE_FOLDER.mkdir(parents=True)
|
||||
# LOG_FILE_FOLDER: Final[Path] = LIB_PATH / "logs" # !! configured in SESSION
|
||||
# if not LOG_FILE_FOLDER.exists():
|
||||
# LOG_FILE_FOLDER.mkdir(parents=True)
|
||||
|
||||
|
||||
LOG_FILE_PATH: Final[Path] = LOG_FILE_FOLDER / "lang-main.log"
|
||||
LOGGING_LEVEL_STDERR: Final[int] = logging.INFO
|
||||
LOGGING_LEVEL_FILE: Final[int] = logging.DEBUG
|
||||
|
||||
|
||||
# ** loggers and configuration
|
||||
logger_all = logging.getLogger("delta_barth")
|
||||
# logger_all.addHandler(logger_all_handler_stderr)
|
||||
# logger_all.addHandler(logger_all_handler_file)
|
||||
logger_session = logging.getLogger("delta_barth.session")
|
||||
logger_session.setLevel(logging.DEBUG)
|
||||
logger_wrapped_results = logging.getLogger("delta_barth.wrapped_results")
|
||||
logger_wrapped_results.setLevel(logging.DEBUG)
|
||||
logger_pipelines = logging.getLogger("delta_barth.logger_pipelines")
|
||||
logger_pipelines.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def setup_logging(
|
||||
logging_dir: Path,
|
||||
) -> None:
|
||||
# ** formatters
|
||||
logger_all_formater = logging.Formatter(fmt=LOG_FMT, datefmt=LOG_DATE_FMT)
|
||||
|
||||
# ** handlers
|
||||
LOG_FILE_PATH: Final[Path] = logging_dir / LOG_FILENAME
|
||||
null_handler = logging.NullHandler()
|
||||
if ENABLE_LOGGING and LOGGING_TO_STDERR:
|
||||
logger_all_handler_stderr = logging.StreamHandler()
|
||||
@ -38,18 +60,12 @@ if ENABLE_LOGGING and LOGGING_TO_FILE:
|
||||
encoding="utf-8",
|
||||
maxBytes=5_242_880,
|
||||
backupCount=1,
|
||||
delay=True,
|
||||
)
|
||||
logger_all_handler_file.setLevel(LOGGING_LEVEL_FILE)
|
||||
logger_all_handler_file.setFormatter(logger_all_formater)
|
||||
else: # pragma: no cover
|
||||
logger_all_handler_file = null_handler
|
||||
|
||||
# ** loggers and configuration
|
||||
logger_all = logging.getLogger("delta_barth")
|
||||
logger_all.addHandler(logger_all_handler_stderr)
|
||||
logger_all.addHandler(logger_all_handler_file)
|
||||
|
||||
logger_wrapped_results = logging.getLogger("delta_barth.wrapped_results")
|
||||
logger_wrapped_results.setLevel(logging.DEBUG)
|
||||
logger_pipelines = logging.getLogger("delta_barth.logger_pipelines")
|
||||
logger_pipelines.setLevel(logging.DEBUG)
|
||||
|
||||
@ -13,7 +13,7 @@ SESSION: Final[Session] = Session(HTTP_BASE_CONTENT_HEADERS)
|
||||
|
||||
def set_data_path(
|
||||
path: str,
|
||||
) -> None:
|
||||
) -> None: # pragma: no cover
|
||||
SESSION.set_data_path(path)
|
||||
|
||||
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from delta_barth.api import common
|
||||
from delta_barth.constants import DEFAULT_API_ERR_CODE, HTTP_BASE_CONTENT_HEADERS
|
||||
from delta_barth.constants import (
|
||||
DEFAULT_API_ERR_CODE,
|
||||
HTTP_BASE_CONTENT_HEADERS,
|
||||
LOG_FILENAME,
|
||||
)
|
||||
from delta_barth.errors import (
|
||||
UnspecifiedRequestType,
|
||||
)
|
||||
@ -43,6 +48,25 @@ def test_session_set_DataPath(tmp_path):
|
||||
assert isinstance(session.data_path, Path)
|
||||
|
||||
|
||||
@patch("delta_barth.logging.ENABLE_LOGGING", True)
|
||||
@patch("delta_barth.logging.LOGGING_TO_FILE", True)
|
||||
def test_session_setup_logging(tmp_path):
|
||||
str_path = str(tmp_path)
|
||||
foldername: str = "logging_test"
|
||||
target_log_dir = tmp_path / foldername
|
||||
|
||||
session = common.Session(HTTP_BASE_CONTENT_HEADERS, logging_folder=foldername)
|
||||
session.set_data_path(str_path)
|
||||
log_dir = session.logging_dir
|
||||
assert log_dir.exists()
|
||||
assert log_dir == target_log_dir
|
||||
# write file
|
||||
target_file = target_log_dir / LOG_FILENAME
|
||||
assert not target_file.exists()
|
||||
session.setup() # calls setup code for logging
|
||||
assert target_file.exists()
|
||||
|
||||
|
||||
def test_validate_creds(credentials):
|
||||
creds = common.validate_credentials(
|
||||
username=credentials["user"],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user