|
| 1 | +import re |
| 2 | +from functools import partial, singledispatch |
| 3 | +from operator import is_, not_ |
| 4 | +from typing import Any, Callable, Iterable, Mapping, Sequence, Tuple, TypeVar |
| 5 | + |
| 6 | +from foundation import non_empty_pairs |
| 7 | +from foundation.core import compose, flip, fst |
| 8 | + |
| 9 | +T1 = TypeVar("T1") |
| 10 | +T2 = TypeVar("T2") |
| 11 | + |
| 12 | +# def extract_context(context: Context[T1], pair: Tuple[T1, T2]) -> Tuple[Context[T1], T2]: |
| 13 | +# return (context + [pair[0]], pair[1]) |
| 14 | + |
| 15 | +Context = str |
| 16 | + |
| 17 | + |
| 18 | +def extract_context(context: Context, pair: Tuple[str, T1]) -> Tuple[Context, T1]: |
| 19 | + new_context = context + ("." if context and not context.endswith(".") else "") + pair[0] |
| 20 | + return (new_context, pair[1]) |
| 21 | + |
| 22 | + |
| 23 | +Rule = Tuple[re.Pattern[str], Callable[[str], str | None]] |
| 24 | + |
| 25 | + |
| 26 | +def first(input: Iterable[T1]) -> T1 | StopIteration: |
| 27 | + for item in input: |
| 28 | + return item |
| 29 | + return StopIteration() |
| 30 | + |
| 31 | + |
| 32 | +def first_or_default(input: Iterable[T1], default: T2) -> T1 | T2: |
| 33 | + for item in input: |
| 34 | + return item |
| 35 | + return default |
| 36 | + |
| 37 | + |
| 38 | +first_or_none: Callable[[Iterable[T1]], T1 | None] = partial(flip(first_or_default), None) |
| 39 | + |
| 40 | +is_none = partial(is_, None) |
| 41 | + |
| 42 | +is_not_none = compose(not_, is_none) |
| 43 | + |
| 44 | + |
| 45 | +def first_matching_rule(context: Context, rules: Sequence[Rule]) -> Rule | None: |
| 46 | + match_with_context = partial(flip(re.search), context) |
| 47 | + match_on_key_of_pair = compose(match_with_context, fst) |
| 48 | + is_matching_pair = compose(is_not_none, match_on_key_of_pair) |
| 49 | + filter_pairs: Callable[[Iterable[Rule]], Iterable[Rule]] = partial(filter, is_matching_pair) |
| 50 | + first_matching = compose(first_or_none, filter_pairs) |
| 51 | + first_found = first_matching(rules) |
| 52 | + return first_found |
| 53 | + |
| 54 | + |
| 55 | +@singledispatch |
| 56 | +def _apply_rules_internal(input: Any, context: Context, rules: Sequence[Rule]) -> Any: |
| 57 | + # print(f"ANY {context} {input}") |
| 58 | + return input |
| 59 | + |
| 60 | + |
| 61 | +@_apply_rules_internal.register(str) |
| 62 | +def _(input: str, context: Context, rules: Sequence[Rule]) -> str | None: |
| 63 | + # print(f"STR {context} {input}") |
| 64 | + first_found_rule = first_matching_rule(context, rules) |
| 65 | + if first_found_rule is None: |
| 66 | + # no pattern matched - return unchanged |
| 67 | + return input |
| 68 | + else: |
| 69 | + return first_found_rule[1](input) |
| 70 | + |
| 71 | + |
| 72 | +@_apply_rules_internal.register(tuple) |
| 73 | +def _(input: Tuple[str, Any], context: Context, rules: Sequence[Rule]) -> Any: |
| 74 | + # print(f"TUPLE {context} {input}") |
| 75 | + first_found_rule = first_matching_rule(context, rules) |
| 76 | + if first_found_rule is None: |
| 77 | + # no pattern matched - continue recursive |
| 78 | + new_context, new_value = extract_context(context, input) |
| 79 | + return (input[0], _apply_rules_internal(new_value, new_context, rules)) |
| 80 | + else: |
| 81 | + # this is to allow overriding the whole object with string |
| 82 | + return (input[0], first_found_rule[1]("tuple")) |
| 83 | + |
| 84 | + |
| 85 | +filter_not_none: Callable[[Iterable[T1 | None]], Iterable[T1]] = partial(filter, is_not_none) |
| 86 | + |
| 87 | + |
| 88 | +def apply_rules(context: Context, rules: Sequence[Rule], input: Any) -> Any: |
| 89 | + return _apply_rules_internal(input, context, rules) |
| 90 | + |
| 91 | + |
| 92 | +@_apply_rules_internal.register(list) |
| 93 | +def _(input: Sequence[Any], context: Context, rules: Sequence[Rule]) -> Any: |
| 94 | + # print(f"LIST {context} {input}") |
| 95 | + first_found_rule = first_matching_rule(context, rules) |
| 96 | + if first_found_rule is None: |
| 97 | + # no pattern matched - continue recursive |
| 98 | + apply_context_rules = partial(apply_rules, context + ".", rules) |
| 99 | + apply_rule_iter = partial(map, apply_context_rules) |
| 100 | + apply_and_filter: Callable[[Iterable[Any]], Iterable[Any]] = compose( |
| 101 | + filter_not_none, apply_rule_iter |
| 102 | + ) |
| 103 | + materialized_apply_and_filter: Callable[[Iterable[Any]], Iterable[Any]] = compose( |
| 104 | + list, apply_and_filter |
| 105 | + ) |
| 106 | + return materialized_apply_and_filter(input) |
| 107 | + else: |
| 108 | + # this is to allow overriding the whole list with string |
| 109 | + return first_found_rule[1]("list") |
| 110 | + |
| 111 | + |
| 112 | +@_apply_rules_internal.register(dict) |
| 113 | +def _(input: Mapping[str, Any], context: Context, rules: Sequence[Rule]) -> Any: |
| 114 | + # print(f"DICT {context} {input}") |
| 115 | + first_found_rule = first_matching_rule(context, rules) |
| 116 | + if first_found_rule is None: |
| 117 | + # no pattern matched - continue recursive |
| 118 | + apply_context_rules = partial(apply_rules, context, rules) |
| 119 | + apply_rule_iter = partial(map, apply_context_rules) |
| 120 | + apply_and_filter: Callable[[Iterable[Any]], Iterable[Any]] = compose( |
| 121 | + non_empty_pairs, apply_rule_iter |
| 122 | + ) |
| 123 | + materialized_apply_and_filter: Callable[[Iterable[Any]], Iterable[Any]] = compose( |
| 124 | + dict, apply_and_filter |
| 125 | + ) |
| 126 | + return materialized_apply_and_filter(input.items()) |
| 127 | + else: |
| 128 | + # this is to allow overriding the whole list with string |
| 129 | + return first_found_rule[1]("dict") |
| 130 | + |
| 131 | + |
| 132 | +def re_compile_flag(flags: re.RegexFlag, input: str) -> re.Pattern[str]: |
| 133 | + return re.compile(input, flags) |
| 134 | + |
| 135 | + |
| 136 | +re_compile_ignorecase: Callable[[str], re.Pattern[str]] = partial( |
| 137 | + re_compile_flag, re.RegexFlag.IGNORECASE |
| 138 | +) |
| 139 | + |
| 140 | +compile_patterns: Callable[[Iterable[str]], Iterable[re.Pattern[str]]] = partial( |
| 141 | + map, re_compile_ignorecase |
| 142 | +) |
0 commit comments