From 02af11a59584fc9af4c3adcd44326d45e9b03946 Mon Sep 17 00:00:00 2001 From: AllMeatball <181806857+AllMeatball@users.noreply.github.com> Date: Tue, 5 May 2026 22:36:16 -0500 Subject: [PATCH] Add support for `__contains__` in the `ResourceFork` class --- rsrcdump/resfork.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/rsrcdump/resfork.py b/rsrcdump/resfork.py index 0143e4b..bb390c6 100644 --- a/rsrcdump/resfork.py +++ b/rsrcdump/resfork.py @@ -72,6 +72,16 @@ def ordered_flat_list(self): flat.append(self.tree[res_type][res_id]) return sorted(flat, key=lambda r: r.order) + def lint_resource_key(self, key: str | bytes): + if type(key) is str: + return parse_type_name(key) + if type(key) is not bytes: + raise TypeError("restype must convert to a bytes object") + if len(key) != 4: + raise ValueError("restype isn't 4 bytes") + + return key + def __repr__(self) -> str: s = "" prefix = "ResourceFork(" @@ -82,13 +92,12 @@ def __repr__(self) -> str: s += ")" return s + def __contains__(self, key: str | bytes): + key = self.lint_resource_key(key) + return key in self.tree + def __getitem__(self, key: str | bytes) -> dict[int, Resource]: - if type(key) is str: - key = parse_type_name(key) - if type(key) is not bytes: - raise TypeError("restype must convert to a bytes object") - if len(key) != 4: - raise ValueError("restype isn't 4 bytes") + key = self.lint_resource_key(key) return self.tree[key] @staticmethod