Flags that map to parsed keys/namespaces.
| 206 | |
| 207 | |
| 208 | class Flags: |
| 209 | """Flags that map to parsed keys/namespaces.""" |
| 210 | |
| 211 | # Marks an immutable namespace (inline array or inline table). |
| 212 | FROZEN: Final = 0 |
| 213 | # Marks a nest that has been explicitly created and can no longer |
| 214 | # be opened using the "[table]" syntax. |
| 215 | EXPLICIT_NEST: Final = 1 |
| 216 | |
| 217 | def __init__(self) -> None: |
| 218 | self._flags: dict[str, dict[Any, Any]] = {} |
| 219 | self._pending_flags: set[tuple[Key, int]] = set() |
| 220 | |
| 221 | def add_pending(self, key: Key, flag: int) -> None: |
| 222 | self._pending_flags.add((key, flag)) |
| 223 | |
| 224 | def finalize_pending(self) -> None: |
| 225 | for key, flag in self._pending_flags: |
| 226 | self.set(key, flag, recursive=False) |
| 227 | self._pending_flags.clear() |
| 228 | |
| 229 | def unset_all(self, key: Key) -> None: |
| 230 | cont = self._flags |
| 231 | for k in key[:-1]: |
| 232 | if k not in cont: |
| 233 | return |
| 234 | cont = cont[k]["nested"] |
| 235 | cont.pop(key[-1], None) |
| 236 | |
| 237 | def set(self, key: Key, flag: int, *, recursive: bool) -> None: # noqa: A003 |
| 238 | cont = self._flags |
| 239 | key_parent, key_stem = key[:-1], key[-1] |
| 240 | for k in key_parent: |
| 241 | if k not in cont: |
| 242 | cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}} |
| 243 | cont = cont[k]["nested"] |
| 244 | if key_stem not in cont: |
| 245 | cont[key_stem] = {"flags": set(), "recursive_flags": set(), "nested": {}} |
| 246 | cont[key_stem]["recursive_flags" if recursive else "flags"].add(flag) |
| 247 | |
| 248 | def is_(self, key: Key, flag: int) -> bool: |
| 249 | if not key: |
| 250 | return False # document root has no flags |
| 251 | cont = self._flags |
| 252 | for k in key[:-1]: |
| 253 | if k not in cont: |
| 254 | return False |
| 255 | inner_cont = cont[k] |
| 256 | if flag in inner_cont["recursive_flags"]: |
| 257 | return True |
| 258 | cont = inner_cont["nested"] |
| 259 | key_stem = key[-1] |
| 260 | if key_stem in cont: |
| 261 | inner_cont = cont[key_stem] |
| 262 | return flag in inner_cont["flags"] or flag in inner_cont["recursive_flags"] |
| 263 | return False |
| 264 | |
| 265 |
no outgoing calls
no test coverage detected
searching dependent graphs…