1#!/usr/bin/env python3
2"""
3
4"""
5
6# Imports:
7from __future__ import annotations
8
9# ##-- stdlib imports
10import re
11import logging as logmod
12
13# ##-- end stdlib imports
14
15# ##-- types
16# isort: off
17# General
18import abc
19import collections.abc
20import typing
21import types
22from typing import cast, assert_type, assert_never
23from typing import Generic, NewType, Never
24from typing import no_type_check, final, override, overload
25# Protocols and Interfaces:
26from typing import Protocol, runtime_checkable
27# isort: on
28# ##-- end types
29
30# ##-- type checking
31# isort: off
32if typing.TYPE_CHECKING:
33 from typing import Final, ClassVar, Any, Self
34 from typing import Literal, LiteralString
35 from typing import TypeGuard
36 from collections.abc import Iterable, Iterator, Callable, Generator
37 from collections.abc import Sequence, Mapping, MutableMapping, Hashable
38
39 from logging import LogRecord
40 from jgdv import Maybe, RxStr
41## isort: on
42# ##-- end type checking
43
44##-- logging
45logging = logmod.getLogger(__name__)
46##-- end logging
47
48# True to process, False to reject
49
[docs]
50class SimpleFilter:
51 """
52 A Simple filter to reject based on:
53 1) a whitelist of regexs,
54 2) a simple list of rejection names
55
56 """
57
58 def __init__(self, allow:Maybe[list[RxStr]]=None, reject:Maybe[list[str]]=None) -> None:
59 self.allowed = allow or []
60 self.rejections = reject or []
61 self.allowed_re = re.compile("^({})".format("|".join(self.allowed)))
62 if bool(self.allowed):
63 msg = "Logging Allows are not implemented yet"
64 raise NotImplementedError(msg)
65
66 def __call__(self, record:LogRecord) -> bool:
67 if record.name in ["root", "__main__"]:
68 return True
69 if not (bool(self.allowed) or bool(self.rejections)):
70 return True
71
72 rejected = False
73 rejected |= any(x in record.name for x in self.rejections)
74 return not rejected