]> www.average.org Git - loctrkd.git/blobdiff - gps303/gps303proto.py
introduce `class_by_prefix()`
[loctrkd.git] / gps303 / gps303proto.py
index acfaea63c272aa74bc676dcac90765d027c8ddd2..cc4ab3f629279ab932292489e122a41ad2efcf33 100755 (executable)
@@ -21,6 +21,7 @@ from logging import getLogger
 from struct import pack, unpack
 
 __all__ = (
+    "class_by_prefix",
     "handle_packet",
     "inline_response",
     "make_object",
@@ -536,6 +537,15 @@ if True:  # just to indent the code, sorry!
             PROTOS[cls.__name__] = cls.PROTO
 
 
+def class_by_prefix(prefix):
+    lst = [(name, proto) for name, proto in PROTOS.items()
+            if name.upper().startswith(prefix.upper())]
+    if len(lst) != 1:
+        return lst
+    _, proto = lst[0]
+    return CLASSES[proto]
+
+
 def proto_by_name(name):
     return PROTOS.get(name, -1)