]> www.average.org Git - loctrkd.git/commitdiff
introduce `class_by_prefix()`
authorEugene Crosser <crosser@average.org>
Fri, 22 Apr 2022 20:59:24 +0000 (22:59 +0200)
committerEugene Crosser <crosser@average.org>
Fri, 22 Apr 2022 20:59:24 +0000 (22:59 +0200)
gps303/common.py
gps303/gps303proto.py

index 44c301d41c591bbd785ef5b077d09a9ac8f338e3..6e4a2f36209f05a37198a0735c4a8cd0a323c3cd 100644 (file)
@@ -10,8 +10,9 @@ PORT = 4303
 DBFN = "/var/lib/gps303/gps303.sqlite"
 
 
-def init(log):
-    opts, _ = getopt(argv[1:], "c:d")
+def init(log, opts=None):
+    if opts is None:
+        opts, _ = getopt(argv[1:], "c:d")
     opts = dict(opts)
     conf = readconfig(opts["-c"] if "-c" in opts else CONF)
     if stdout.isatty():
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)