Flags that map to parsed keys/namespaces.
| 133 | |
| 134 | |
| 135 | class Flags: |
| 136 | """Flags that map to parsed keys/namespaces.""" |
| 137 | |
| 138 | # Marks an immutable namespace (inline array or inline table). |
| 139 | FROZEN = 0 |
| 140 | # Marks a nest that has been explicitly created and can no longer |
| 141 | # be opened using the "[table]" syntax. |
| 142 | EXPLICIT_NEST = 1 |
| 143 | |
| 144 | def __init__(self) -> None: |
| 145 | self._flags: dict[str, dict] = {} |
| 146 | self._pending_flags: set[tuple[Key, int]] = set() |
| 147 | |
| 148 | def add_pending(self, key: Key, flag: int) -> None: |
| 149 | self._pending_flags.add((key, flag)) |
| 150 | |
| 151 | def finalize_pending(self) -> None: |
| 152 | for key, flag in self._pending_flags: |
| 153 | self.set(key, flag, recursive=False) |
| 154 | self._pending_flags.clear() |
| 155 | |
| 156 | def unset_all(self, key: Key) -> None: |
| 157 | cont = self._flags |
| 158 | for k in key[:-1]: |
| 159 | if k not in cont: |
| 160 | return |
| 161 | cont = cont[k]["nested"] |
| 162 | cont.pop(key[-1], None) |
| 163 | |
| 164 | def set(self, key: Key, flag: int, *, recursive: bool) -> None: # noqa: A003 |
| 165 | cont = self._flags |
| 166 | key_parent, key_stem = key[:-1], key[-1] |
| 167 | for k in key_parent: |
| 168 | if k not in cont: |
| 169 | cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}} |
| 170 | cont = cont[k]["nested"] |
| 171 | if key_stem not in cont: |
| 172 | cont[key_stem] = {"flags": set(), "recursive_flags": set(), "nested": {}} |
| 173 | cont[key_stem]["recursive_flags" if recursive else "flags"].add(flag) |
| 174 | |
| 175 | def is_(self, key: Key, flag: int) -> bool: |
| 176 | if not key: |
| 177 | return False # document root has no flags |
| 178 | cont = self._flags |
| 179 | for k in key[:-1]: |
| 180 | if k not in cont: |
| 181 | return False |
| 182 | inner_cont = cont[k] |
| 183 | if flag in inner_cont["recursive_flags"]: |
| 184 | return True |
| 185 | cont = inner_cont["nested"] |
| 186 | key_stem = key[-1] |
| 187 | if key_stem in cont: |
| 188 | cont = cont[key_stem] |
| 189 | return flag in cont["flags"] or flag in cont["recursive_flags"] |
| 190 | return False |
| 191 | |
| 192 |
no outgoing calls
no test coverage detected