Skip to content

Commit

Permalink
Use dedicated logger
Browse files Browse the repository at this point in the history
  • Loading branch information
principis committed May 20, 2024
1 parent bf9b495 commit 09e326c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 3 additions & 1 deletion bibtexparser/middlewares/fieldkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from .middleware import BlockMiddleware

logger = logging.getLogger(__name__)


class NormalizeFieldKeys(BlockMiddleware):
"""Normalize field keys to lowercase.
Expand Down Expand Up @@ -37,7 +39,7 @@ def transform_entry(self, entry: Entry, library: "Library") -> Entry:
# if performance is a concern, we could emit a warning with only {entry.key}
# to remove "seen_normalized_keys" and this if statement
if normalized_key in seen_normalized_keys:
logging.warning(
logger.warning(
f"NormalizeFieldKeys: in entry '{entry.key}': "
+ f"duplicate normalized key '{normalized_key}' "
+ f"(original '{field.key}'); overriding previous value"
Expand Down
6 changes: 4 additions & 2 deletions bibtexparser/middlewares/latex_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from .middleware import BlockMiddleware
from .names import NameParts

logger = logging.getLogger(__name__)


class _PyStringTransformerMiddleware(BlockMiddleware, abc.ABC):
"""Abstract utility class allowing to modify python-strings"""
Expand Down Expand Up @@ -59,7 +61,7 @@ def transform_entry(self, entry: Entry, library: Library) -> Block:
field.value.von = self._transform_all_strings(field.value.von, errors)
field.value.jr = self._transform_all_strings(field.value.jr, errors)
else:
logging.info(
logger.info(
f" [{self.metadata_key()}] Cannot python-str transform field {field.key}"
f" with value type {type(field.value)}"
)
Expand All @@ -76,7 +78,7 @@ def transform_string(self, string: String, library: "Library") -> Block:
if isinstance(string.value, str):
string.value = self._transform_python_value_string(string.value)
else:
logging.info(
logger.info(
f" [{self.metadata_key()}] Cannot python-str transform string {string.key}"
f" with value type {type(string.value)}"
)
Expand Down
4 changes: 3 additions & 1 deletion bibtexparser/middlewares/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from bibtexparser.model import Preamble
from bibtexparser.model import String

logger = logging.getLogger(__name__)


class Middleware(abc.ABC):
"""Implements a function to transform a block or library.
Expand Down Expand Up @@ -128,7 +130,7 @@ def transform_block(
elif isinstance(block, ImplicitComment):
return self.transform_implicit_comment(block, library)

logging.warning(f"Unknown block type {type(block)}")
logger.warning(f"Unknown block type {type(block)}")
return block

def transform_entry(
Expand Down
14 changes: 8 additions & 6 deletions bibtexparser/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .model import Preamble
from .model import String

logger = logging.getLogger(__name__)


class Splitter:
"""Object responsible for splitting a BibTeX string into blocks.
Expand Down Expand Up @@ -254,7 +256,7 @@ def split(self, library: Optional[Library] = None) -> Library:
if library is None:
library = Library()
else:
logging.info("Adding blocks to existing library.")
logger.info("Adding blocks to existing library.")

while True:
m = self._next_mark(accept_eof=True)
Expand Down Expand Up @@ -283,12 +285,12 @@ def split(self, library: Optional[Library] = None) -> Library:
library.add(self._handle_entry(m, m_val))

except BlockAbortedException as e:
logging.warning(
logger.warning(
f"Parsing of `{m_val}` block (line {start_line}) "
f"aborted on line {self._current_line} "
f"due to syntactical error in bibtex:\n {e.abort_reason}"
)
logging.info(
logger.info(
"We will try to continue parsing, but this might lead to unexpected results."
"The failed block will be stored in the `failed_blocks`of the library."
)
Expand All @@ -302,14 +304,14 @@ def split(self, library: Optional[Library] = None) -> Library:

except ParserStateException as e:
# This is a bug in the parser, not in the bibtex. We should not continue.
logging.error(
logger.error(
"python-bibtexparser detected an invalid state. Please report this bug."
)
logging.error(e.message)
logger.error(e.message)
raise e
except Exception as e:
# For unknown exeptions, we want to fail hard and get the info in our issue tracker.
logging.error(
logger.error(
f"Unexpected exception while parsing `{m_val}` block (line {start_line})"
"Please report this bug."
)
Expand Down
2 changes: 1 addition & 1 deletion tests/middleware_tests/test_fieldkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_normalize_fieldkeys_force_last(caplog):
lib.add(Entry(entry_type=entry_type, key=f"entry{i}", fields=f))

lib = NormalizeFieldKeys().transform(lib)
assert re.match(r"(WARNING\s*)(\w*\:\w*\.py\:[0-9]*\s*)(NormalizeFieldKeys)(.*)", caplog.text)
assert re.match(r"(WARNING\s*)([\w\.]*\:\w*\.py\:[0-9]*\s*)(NormalizeFieldKeys)(.*)", caplog.text)

for key in lib.entries_dict:
assert lib.entries_dict[key] == ref.entries_dict[key]

0 comments on commit 09e326c

Please sign in to comment.