Skip to content

Commit

Permalink
fix: provide _warning code
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Oct 4, 2023
1 parent caa2248 commit 44d1813
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions raillabel_providerkit/_util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0
37 changes: 37 additions & 0 deletions raillabel_providerkit/_util/_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

import logging
import typing as t
from io import StringIO


class _WarningsLogger:

warnings: t.List[str] = []

def __enter__(self) -> "_WarningsLogger":
logger = logging.getLogger("loader_warnings")
warnings_stream = StringIO()
handler = logging.StreamHandler(warnings_stream)
handler.setLevel(logging.WARNING)
logger.addHandler(handler)

return self

def __exit__(self, exc_type, exc_value, traceback):
logger = logging.getLogger("loader_warnings")
stream = logger.handlers[-1].stream
stream.seek(0)

warnings_list = stream.getvalue().split("\n")

if len(warnings_list) > 0:
warnings_list = warnings_list[:-1]

self.warnings = warnings_list


def _warning(message: str) -> logging.Logger:
"""Create a loader warning."""
logging.getLogger("loader_warnings").warning(message)

0 comments on commit 44d1813

Please sign in to comment.