Apply patches to objects. Args: ---- objects: list of objects to apply patches to patches: list of patches to apply
(objects: dict[str, Entry], patches: list[Patch])
| 16 | |
| 17 | |
| 18 | def apply_roomcode_patch(objects: dict[str, Entry], patches: list[Patch]) -> None: |
| 19 | """ |
| 20 | Apply patches to objects. |
| 21 | |
| 22 | Args: |
| 23 | ---- |
| 24 | objects: list of objects to apply patches to |
| 25 | patches: list of patches to apply |
| 26 | |
| 27 | """ |
| 28 | compiled_patches: list[tuple[re.Pattern[str], dict[str, object]]] = [ |
| 29 | ( |
| 30 | re.compile(p["if_room_code"]), |
| 31 | {k: v for k, v in p.items() if k != "if_room_code"}, |
| 32 | ) |
| 33 | for p in patches |
| 34 | ] |
| 35 | |
| 36 | to_delete = [] |
| 37 | applied_patches: set[re.Pattern[str]] = set() |
| 38 | for room_code, room in objects.items(): |
| 39 | for patch_check, patch in compiled_patches: |
| 40 | if patch_check.match(room_code) is not None: |
| 41 | applied_patches.add(patch_check) |
| 42 | if patch.get("__delete"): |
| 43 | to_delete.append(room_code) |
| 44 | continue |
| 45 | for patch_key, patched_value in patch.items(): |
| 46 | room[patch_key] = patched_value |
| 47 | room["patched"] = True |
| 48 | |
| 49 | for room_code in to_delete: |
| 50 | objects.pop(room_code) |
| 51 | |
| 52 | for patch_check, _ in compiled_patches: |
| 53 | if patch_check not in applied_patches: |
| 54 | _logger.warning( |
| 55 | f"The patch for roomcode: r'{patch_check.pattern}' was never applied. Make sure it is still required.", |
| 56 | ) |
no test coverage detected