summaryrefslogtreecommitdiff
path: root/python/stringrecognizer.py
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2025-11-20 09:58:28 -0500
committerPeter LaFosse <peter@vector35.com>2025-11-21 10:02:50 -0500
commit5880769cbac1362aad46a34faec4c553fa9fcac6 (patch)
tree02db205b7aa915900cbfc10384940b3750eb7d69 /python/stringrecognizer.py
parentf52693d50de6a5c97afa0274277edca18cfa00ea (diff)
Add get and __contains__ methods to all *MetaClass
Fixes: https://github.com/Vector35/binaryninja-api/issues/7588
Diffstat (limited to 'python/stringrecognizer.py')
-rw-r--r--python/stringrecognizer.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/python/stringrecognizer.py b/python/stringrecognizer.py
index 004a58c4..3e6167a0 100644
--- a/python/stringrecognizer.py
+++ b/python/stringrecognizer.py
@@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-from typing import Optional, Union
+from typing import Optional, Union, Any
from dataclasses import dataclass
import ctypes
@@ -49,6 +49,23 @@ class _CustomStringTypeMetaClass(type):
raise KeyError("'%s' is not a valid type" % str(value))
return CustomStringType(handle=string_type)
+ def __contains__(cls: '_CustomStringTypeMetaClass', name: object) -> bool:
+ if not isinstance(name, str):
+ return False
+ try:
+ cls[name]
+ return True
+ except KeyError:
+ return False
+
+ def get(cls: '_CustomStringTypeMetaClass', name: str, default: Any = None) -> Optional['CustomStringType']:
+ try:
+ return cls[name]
+ except KeyError:
+ if default is not None:
+ return default
+ return None
+
class CustomStringType(metaclass=_CustomStringTypeMetaClass):
"""
@@ -125,6 +142,23 @@ class _StringRecognizerMetaClass(type):
raise KeyError("'%s' is not a valid recognizer" % str(value))
return CoreStringRecognizer(handle=recognizer)
+ def __contains__(cls: '_StringRecognizerMetaClass', name: object) -> bool:
+ if not isinstance(name, str):
+ return False
+ try:
+ cls[name]
+ return True
+ except KeyError:
+ return False
+
+ def get(cls: '_StringRecognizerMetaClass', name: str, default: Any = None) -> Optional['StringRecognizer']:
+ try:
+ return cls[name]
+ except KeyError:
+ if default is not None:
+ return default
+ return None
+
class StringRecognizer(metaclass=_StringRecognizerMetaClass):
"""