|
1
|
|
|
""" |
|
2
|
|
|
Tool to filter annotations. |
|
3
|
|
|
""" |
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
import re |
|
7
|
|
|
from dataclasses import dataclass |
|
8
|
|
|
import enum |
|
9
|
|
|
from datetime import datetime |
|
10
|
|
|
from pathlib import Path |
|
11
|
|
|
from typing import Union, Sequence, Mapping, Set, Optional |
|
12
|
|
|
|
|
13
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
from mandos.model.hits import AbstractHit, HitFrame |
|
16
|
|
|
|
|
17
|
|
|
_Type = Union[str, int, float, datetime] |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class Operator(enum.Enum): |
|
|
|
|
|
|
21
|
|
|
"""""" |
|
22
|
|
|
|
|
23
|
|
|
eq = enum.auto() |
|
24
|
|
|
ne = enum.auto() |
|
25
|
|
|
lt = enum.auto() |
|
26
|
|
|
gt = enum.auto() |
|
27
|
|
|
le = enum.auto() |
|
28
|
|
|
ge = enum.auto() |
|
29
|
|
|
like = enum.auto() |
|
30
|
|
|
is_in = enum.auto() |
|
31
|
|
|
not_in = enum.auto() |
|
32
|
|
|
|
|
33
|
|
|
@classmethod |
|
34
|
|
|
def parse(cls, s: str): |
|
|
|
|
|
|
35
|
|
|
if isinstance(s, Operator): |
|
36
|
|
|
return s |
|
37
|
|
|
if s in cls: |
|
38
|
|
|
return cls[s] |
|
39
|
|
|
return cls.rev_symbols().get(s) |
|
40
|
|
|
|
|
41
|
|
|
@property |
|
42
|
|
|
def symbol(self) -> str: |
|
|
|
|
|
|
43
|
|
|
return dict( |
|
44
|
|
|
eq="=", ne="!=", lt="<", gt=">", le="<=", ge=">=", like="$", is_in="<<", not_in="!<<" |
|
45
|
|
|
)[self.name] |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
def rev_symbols(cls) -> Mapping[str, Operator]: |
|
|
|
|
|
|
49
|
|
|
return {e.symbol: e for e in cls} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@dataclass(frozen=True, repr=True) |
|
|
|
|
|
|
53
|
|
|
class Expression: |
|
54
|
|
|
"""""" |
|
55
|
|
|
|
|
56
|
|
|
op: Operator |
|
|
|
|
|
|
57
|
|
|
val: _Type |
|
58
|
|
|
extra: Optional[Set[_Type]] |
|
59
|
|
|
|
|
60
|
|
|
def matches(self, value: _Type) -> bool: |
|
|
|
|
|
|
61
|
|
|
if type(value) != type(self.val): |
|
|
|
|
|
|
62
|
|
|
raise TypeError(f"{type(value)} != {type(self.val)}") |
|
63
|
|
|
if self.op is Operator.like: |
|
64
|
|
|
return re.compile(self.val).fullmatch(str(value)) is not None |
|
65
|
|
|
if self.op is Operator.is_in: |
|
66
|
|
|
return value in self.extra |
|
67
|
|
|
if self.op is Operator.not_in: |
|
68
|
|
|
return value not in self.extra |
|
69
|
|
|
call = f"__{self.op}__" |
|
70
|
|
|
return getattr(value, call) != getattr(self.val, call) |
|
71
|
|
|
|
|
72
|
|
|
@classmethod |
|
73
|
|
|
def parse(cls, s: str, context: NestedDotDict) -> Expression: |
|
|
|
|
|
|
74
|
|
|
op = Operator.parse(s) |
|
|
|
|
|
|
75
|
|
|
if op is None: |
|
76
|
|
|
op = Operator.eq |
|
|
|
|
|
|
77
|
|
|
val = s[len(op.symbol) :] |
|
78
|
|
|
return Expression(op, val, set(context.get(val, []))) |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
class Filtration: |
|
|
|
|
|
|
82
|
|
|
def __init__(self, key_to_statements: Mapping[str, Mapping[str, Sequence[Expression]]]): |
|
83
|
|
|
self._x = key_to_statements |
|
84
|
|
|
|
|
85
|
|
|
@classmethod |
|
86
|
|
|
def from_file(cls, path: Path) -> Filtration: |
|
|
|
|
|
|
87
|
|
|
dot = NestedDotDict.read_toml(path) |
|
88
|
|
|
return cls.from_toml(dot) |
|
89
|
|
|
|
|
90
|
|
|
@classmethod |
|
91
|
|
|
def from_toml(cls, dot: NestedDotDict) -> Filtration: |
|
|
|
|
|
|
92
|
|
|
data = dot.get("mandos.filter", []) |
|
93
|
|
|
return Filtration({d["key"]: {k: v for k, v in d.items() if k != "key"} for d in data}) |
|
94
|
|
|
|
|
95
|
|
|
def apply(self, df: HitFrame) -> HitFrame: |
|
|
|
|
|
|
96
|
|
|
hits = [h for h in df.to_hits() if self.keep(h)] |
|
97
|
|
|
return HitFrame.from_hits(hits) |
|
98
|
|
|
|
|
99
|
|
|
def keep(self, hit: AbstractHit) -> bool: |
|
|
|
|
|
|
100
|
|
|
if hit.search_key not in self._x: |
|
101
|
|
|
return True |
|
102
|
|
|
for field, values in self._x[hit.search_key].items(): |
|
|
|
|
|
|
103
|
|
|
if not hasattr(hit, field): |
|
104
|
|
|
raise ValueError(f"No field {field} in {hit.__class__.__name__}") |
|
105
|
|
|
if not self._matches(getattr(hit, field), field): |
|
106
|
|
|
return False |
|
107
|
|
|
return True |
|
108
|
|
|
|
|
109
|
|
|
def _matches(self, actual: _Type, allowed: Sequence[Expression]) -> bool: |
|
|
|
|
|
|
110
|
|
|
for e in allowed: |
|
|
|
|
|
|
111
|
|
|
if e.matches(actual): |
|
112
|
|
|
return True |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
__all__ = ["Filtration"] |
|
116
|
|
|
|