|
1 | | -from collections.abc import Iterable |
2 | | -from typing import Any |
| 1 | +from typing import Self, Any |
| 2 | + |
3 | 3 |
|
4 | 4 | MISSING_VALUES = { |
5 | 5 | "", |
|
11 | 11 | } |
12 | 12 |
|
13 | 13 |
|
14 | | -class ContextField(Iterable): |
15 | | - def __init__(self, original: Any, transformed: Any = None): |
16 | | - self.original = original |
17 | | - self.transformed = transformed or original |
18 | | - self.normalized = self.normalize(self.transformed) |
| 14 | +class ContextField: |
| 15 | + def __init__(self, value: str | list[str] | tuple[str]): |
| 16 | + self.value = value |
19 | 17 |
|
20 | | - def normalize(self, value: Any) -> tuple[str, ...]: |
| 18 | + def normalize(self, obj: Any | None = None, mapping: dict | None = None) -> Self: |
| 19 | + value = obj or self.value |
21 | 20 | if isinstance(value, (tuple, list)): |
22 | 21 | intermediate = list(value) |
23 | 22 | elif isinstance(value, str) and "/" in value: |
24 | 23 | intermediate = list(value.split("/")) |
25 | 24 | elif isinstance(value, str): |
26 | 25 | intermediate = [value] |
27 | 26 | else: |
28 | | - raise ValueError(f"Can't understand input context {value}") |
| 27 | + raise ValueError(f"Can't understand input context {self.value}") |
29 | 28 |
|
30 | 29 | intermediate = [elem.lower().strip() for elem in intermediate] |
31 | 30 |
|
32 | | - if intermediate[-1] in MISSING_VALUES: |
| 31 | + while intermediate[-1] in MISSING_VALUES: |
33 | 32 | intermediate = intermediate[:-1] |
34 | 33 |
|
35 | | - return tuple(intermediate) |
| 34 | + # TODO: Apply mapping |
36 | 35 |
|
37 | | - def export_as_string(self): |
38 | | - if isinstance(self.original, str): |
39 | | - return self.original |
40 | | - elif isinstance(self.original, (list, tuple)): |
41 | | - return "✂️".join(self.original) |
42 | | - else: |
43 | | - # Only reachable by manually changing `self.original` |
44 | | - raise ValueError("Invalid context data") |
| 36 | + return type(self)(value=tuple(intermediate)) |
| 37 | + |
| 38 | + def export_as_string(self, join_character: str = "✂️"): |
| 39 | + if isinstance(self.value, (list, tuple)): |
| 40 | + return join_character.join(self.value) |
| 41 | + return self.value |
45 | 42 |
|
46 | 43 | def __iter__(self): |
47 | | - return iter(self.normalized) |
| 44 | + return iter(self.value) |
48 | 45 |
|
49 | | - def __eq__(self, other): |
| 46 | + def __eq__(self, other: Any) -> bool: |
50 | 47 | if self and other and isinstance(other, ContextField): |
51 | | - return self.original and self.normalized == other.normalized |
| 48 | + return self.value == other.value |
52 | 49 | else: |
53 | 50 | try: |
54 | | - normalized_other = self.normalize(other) |
55 | | - return (self.normalized == normalized_other) or ( |
56 | | - self.original == normalized_other |
57 | | - ) |
| 51 | + return self.value == self.normalize(other).value |
58 | 52 | except ValueError: |
59 | 53 | return False |
60 | 54 |
|
61 | 55 | def __repr__(self): |
62 | | - return f"ContextField: '{self.original}' -> '{self.normalized}'" |
| 56 | + return f"ContextField: {self.value}" |
63 | 57 |
|
64 | 58 | def __bool__(self): |
65 | | - return bool(self.normalized) |
| 59 | + return bool(self.value) |
66 | 60 |
|
67 | 61 | def __hash__(self): |
68 | | - return hash(self.normalized) |
| 62 | + return hash(self.value) |
69 | 63 |
|
70 | | - def __contains__(self, other): |
71 | | - """This context is more generic than the `other` context. |
| 64 | + def __contains__(self, other: Any) -> bool: |
| 65 | + """`self` context is more generic than the `other` context. |
72 | 66 |
|
73 | 67 | ```python |
74 | | - Context("a/b/c") in Context("a/b") |
| 68 | + Context("a/b") in Context("a/b/c") |
75 | 69 | >>> True |
76 | 70 | ``` |
77 | 71 |
|
78 | 72 | """ |
79 | 73 | if not isinstance(other, ContextField): |
80 | 74 | return False |
81 | | - return self.normalized == other.normalized[: len(self.normalized)] |
| 75 | + return self.value == other.value[: len(self.value)] |
0 commit comments