-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor record matching to use internal
Matcher
class (#126)
This will make adding new features such as #25, #26 and #80 easier. It also opens the door to #31 and #124, if desired in future.
- Loading branch information
Showing
10 changed files
with
203 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from logot._capture import Captured | ||
from logot._match import Matcher | ||
from logot._typing import Level | ||
|
||
|
||
class _LevelNameMatcher(Matcher): | ||
__slots__ = ("_levelname",) | ||
|
||
def __init__(self, levelname: str) -> None: | ||
self._levelname = levelname | ||
|
||
def match(self, captured: Captured) -> bool: | ||
return captured.levelname == self._levelname | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return isinstance(other, _LevelNameMatcher) and other._levelname == self._levelname | ||
|
||
def __repr__(self) -> str: | ||
return repr(self._levelname) | ||
|
||
def __str__(self) -> str: | ||
return f"[{self._levelname}]" | ||
|
||
|
||
DEBUG_MATCHER: Matcher = _LevelNameMatcher("DEBUG") | ||
INFO_MATCHER: Matcher = _LevelNameMatcher("INFO") | ||
WARNING_MATCHER: Matcher = _LevelNameMatcher("WARNING") | ||
ERROR_MATCHER: Matcher = _LevelNameMatcher("ERROR") | ||
CRITICAL_MATCHER: Matcher = _LevelNameMatcher("CRITICAL") | ||
|
||
|
||
class _LevelNoMatcher(Matcher): | ||
__slots__ = ("_levelno",) | ||
|
||
def __init__(self, levelno: int) -> None: | ||
self._levelno = levelno | ||
|
||
def match(self, captured: Captured) -> bool: | ||
return captured.levelno == self._levelno | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return isinstance(other, _LevelNoMatcher) and other._levelno == self._levelno | ||
|
||
def __repr__(self) -> str: | ||
return repr(self._levelno) | ||
|
||
def __str__(self) -> str: | ||
return f"[Level {self._levelno}]" | ||
|
||
|
||
def level_matcher(level: Level) -> Matcher: | ||
# Handle `str` level. | ||
if isinstance(level, str): | ||
return _LevelNameMatcher(level) | ||
# Handle `int` level. | ||
if isinstance(level, int): | ||
return _LevelNoMatcher(level) | ||
# Handle invalid level. | ||
raise TypeError(f"Invalid level: {level!r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
from logot._capture import Captured | ||
|
||
|
||
class Matcher(ABC): | ||
__slots__ = () | ||
|
||
@abstractmethod | ||
def match(self, captured: Captured) -> bool: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def __eq__(self, other: object) -> bool: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def __repr__(self) -> str: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def __str__(self) -> str: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import cast | ||
|
||
import pytest | ||
|
||
from logot._level import level_matcher | ||
from logot._typing import Level | ||
|
||
|
||
def test_eq_pass() -> None: | ||
assert level_matcher("INFO") == level_matcher("INFO") | ||
assert level_matcher(20) == level_matcher(20) | ||
|
||
|
||
def test_eq_fail() -> None: | ||
assert level_matcher("INFO") != level_matcher("WARNING") | ||
assert level_matcher(20) != level_matcher(30) | ||
assert level_matcher("INFO") != level_matcher(20) | ||
|
||
|
||
def test_repr() -> None: | ||
assert repr(level_matcher("INFO")) == "'INFO'" | ||
assert repr(level_matcher(20)) == "20" | ||
|
||
|
||
def test_str() -> None: | ||
assert str(level_matcher("INFO")) == "[INFO]" | ||
assert str(level_matcher(20)) == "[Level 20]" | ||
|
||
|
||
def test_invalid() -> None: | ||
with pytest.raises(TypeError) as ex: | ||
level_matcher(cast(Level, 1.5)) | ||
assert str(ex.value) == "Invalid level: 1.5" |
Oops, something went wrong.