From 18ca465f9489f135a3f646f3484af6e569007012 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Fri, 22 Apr 2022 22:59:24 +0200 Subject: [PATCH] introduce `class_by_prefix()` --- gps303/common.py | 5 +++-- gps303/gps303proto.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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) -- 2.43.0