|
| 1 | +import re |
| 2 | + |
| 3 | + |
| 4 | +class InvalidFormat(Exception): |
| 5 | + """key is invalid formated""" |
| 6 | + |
| 7 | + def __init__(self, key): |
| 8 | + super().__init__(f"invaid key format: {key}") |
| 9 | + |
| 10 | + |
| 11 | +class NestedParserOptionsType(type): |
| 12 | + def __new__(cls, cls_name, ns, childs): |
| 13 | + if cls_name != "NestedParserOptionsAbstract" and cls_name: |
| 14 | + if "sanitize" not in childs: |
| 15 | + raise ValueError("you need to define sanitize methods") |
| 16 | + return super().__new__(cls, cls_name, ns, childs) |
| 17 | + |
| 18 | + |
| 19 | +TOKEN_PARSER = ("[", "]", ".") |
| 20 | + |
| 21 | + |
| 22 | +class NestedParserOptionsAbstract(metaclass=NestedParserOptionsType): |
| 23 | + def check(self, key, keys): |
| 24 | + if len(keys) == 0: |
| 25 | + raise InvalidFormat(key) |
| 26 | + |
| 27 | + first = keys[0] |
| 28 | + for token in TOKEN_PARSER: |
| 29 | + if token in first: |
| 30 | + raise InvalidFormat(key) |
| 31 | + |
| 32 | + for key in keys: |
| 33 | + if not isinstance(key, str): |
| 34 | + continue |
| 35 | + for c in key: |
| 36 | + if c.isspace(): |
| 37 | + raise InvalidFormat(key) |
| 38 | + |
| 39 | + def split(self, key): |
| 40 | + contents = list(filter(None, self._reg_spliter.split(key))) |
| 41 | + if not contents: |
| 42 | + raise ValueError(f"invalid form key: {key}") |
| 43 | + |
| 44 | + lst = [contents[0]] |
| 45 | + if len(contents) >= 2: |
| 46 | + lst.extend(self._reg_options.split(contents[1])) |
| 47 | + if len(contents) == 3: |
| 48 | + lst.append(contents[2]) |
| 49 | + |
| 50 | + return list(filter(None, lst)) |
| 51 | + |
| 52 | + |
| 53 | +REGEX_SEPARATOR = { |
| 54 | + "dot": r"(\.[^\.]+)", |
| 55 | + "bracket": r"([^\[\]]+)", |
| 56 | + "mixed": r"(\[\d+\])|([^\[\]]+)", |
| 57 | + "mixed-dot": r"(\[\d+\])|(\.[^\[\]\.]+)", |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +class NestedParserOptionsDot(NestedParserOptionsAbstract): |
| 62 | + def __init__(self): |
| 63 | + self._reg_spliter = re.compile(r"^([^\.]+)(.*?)(\.)?$") |
| 64 | + self._reg_options = re.compile(r"(\.[^\.]+)") |
| 65 | + |
| 66 | + def sanitize(self, key, value): |
| 67 | + contents = self.split(key) |
| 68 | + lst = contents[1:] |
| 69 | + keys = [contents[0]] |
| 70 | + for idx, k in enumerate(lst): |
| 71 | + if k.startswith("."): |
| 72 | + k = k[1:] |
| 73 | + if not k: |
| 74 | + if len(lst) != idx + 1: |
| 75 | + raise InvalidFormat(key) |
| 76 | + value = {} |
| 77 | + break |
| 78 | + try: |
| 79 | + k = int(k) |
| 80 | + except Exception: |
| 81 | + pass |
| 82 | + else: |
| 83 | + raise InvalidFormat(key) |
| 84 | + keys.append(k) |
| 85 | + |
| 86 | + return keys, value |
| 87 | + |
| 88 | + |
| 89 | +class NestedParserOptionsBracket(NestedParserOptionsAbstract): |
| 90 | + def __init__(self): |
| 91 | + self._reg_spliter = re.compile(r"^([^\[\]]+)(.*?)(\[\])?$") |
| 92 | + self._reg_options = re.compile(r"(\[[^\[\]]+\])") |
| 93 | + |
| 94 | + def sanitize(self, key, value): |
| 95 | + first, *lst = self.split(key) |
| 96 | + keys = [first] |
| 97 | + |
| 98 | + for idx, k in enumerate(lst): |
| 99 | + if k.startswith("[") or k.endswith("]"): |
| 100 | + if not k.startswith("[") or not k.endswith("]"): |
| 101 | + raise InvalidFormat(key) |
| 102 | + k = k[1:-1] |
| 103 | + if not k: |
| 104 | + if len(lst) != idx + 1: |
| 105 | + raise InvalidFormat(key) |
| 106 | + value = [] |
| 107 | + break |
| 108 | + try: |
| 109 | + k = int(k) |
| 110 | + except Exception: |
| 111 | + pass |
| 112 | + else: |
| 113 | + raise InvalidFormat(key) |
| 114 | + keys.append(k) |
| 115 | + return keys, value |
| 116 | + |
| 117 | + |
| 118 | +class NestedParserOptionsMixedDot(NestedParserOptionsAbstract): |
| 119 | + def __init__(self): |
| 120 | + self._reg_spliter = re.compile(r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$") |
| 121 | + self._reg_options = re.compile(r"(\[\d+\])|(\.[^\[\]\.]+)") |
| 122 | + |
| 123 | + def sanitize(self, key, value): |
| 124 | + first, *lst = self.split(key) |
| 125 | + keys = [first] |
| 126 | + |
| 127 | + for idx, k in enumerate(lst): |
| 128 | + if k.startswith("."): |
| 129 | + k = k[1:] |
| 130 | + # empty dict |
| 131 | + if not k: |
| 132 | + if len(lst) != idx + 1: |
| 133 | + raise InvalidFormat(key) |
| 134 | + value = {} |
| 135 | + break |
| 136 | + elif k.startswith("[") or k.endswith("]"): |
| 137 | + if not k.startswith("[") or not k.endswith("]"): |
| 138 | + raise InvalidFormat(key) |
| 139 | + k = k[1:-1] |
| 140 | + if not k: |
| 141 | + if len(lst) != idx + 1: |
| 142 | + raise InvalidFormat(key) |
| 143 | + value = [] |
| 144 | + break |
| 145 | + k = int(k) |
| 146 | + else: |
| 147 | + raise InvalidFormat(key) |
| 148 | + keys.append(k) |
| 149 | + |
| 150 | + return keys, value |
| 151 | + |
| 152 | + |
| 153 | +class NestedParserOptionsMixed(NestedParserOptionsMixedDot): |
| 154 | + def __init__(self): |
| 155 | + self._reg_spliter = re.compile(r"^([^\[\]\.]+)(.*?)((?:\.)|(?:\[\]))?$") |
| 156 | + self._reg_options = re.compile(r"(\[\d+\])|(\.?[^\[\]\.]+)") |
| 157 | + |
| 158 | + def sanitize(self, key, value): |
| 159 | + first, *lst = self.split(key) |
| 160 | + keys = [first] |
| 161 | + |
| 162 | + for idx, k in enumerate(lst): |
| 163 | + if k.startswith("."): |
| 164 | + k = k[1:] |
| 165 | + # empty dict |
| 166 | + if not k: |
| 167 | + if len(lst) != idx + 1: |
| 168 | + raise InvalidFormat(key) |
| 169 | + value = {} |
| 170 | + break |
| 171 | + elif k.startswith("[") or k.endswith("]"): |
| 172 | + if not k.startswith("[") or not k.endswith("]"): |
| 173 | + raise InvalidFormat(key) |
| 174 | + k = k[1:-1] |
| 175 | + if not k: |
| 176 | + if len(lst) != idx + 1: |
| 177 | + raise InvalidFormat(key) |
| 178 | + value = [] |
| 179 | + break |
| 180 | + k = int(k) |
| 181 | + keys.append(k) |
| 182 | + |
| 183 | + return keys, value |
0 commit comments