From: Eugene Crosser Date: Fri, 22 Apr 2022 20:59:24 +0000 (+0200) Subject: introduce `class_by_prefix()` X-Git-Tag: 0.01~42 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=18ca465f9489f135a3f646f3484af6e569007012 introduce `class_by_prefix()` --- diff --git a/gps303/common.py b/gps303/common.py index 44c301d..6e4a2f3 100644 --- a/gps303/common.py +++ b/gps303/common.py @@ -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(): diff --git a/gps303/gps303proto.py b/gps303/gps303proto.py index acfaea6..cc4ab3f 100755 --- a/gps303/gps303proto.py +++ b/gps303/gps303proto.py @@ -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)