X-Git-Url: http://www.average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Ftermconfig.py;h=14df8cf5fb267c065b84f37a351e3c8c29b77a1c;hb=042f003133249fb38d87d92faa9c730833d14904;hp=f8f4b77e56538d0b15e5eb6969e8f2b9472658f0;hpb=dbdf9d63af31770ad57302e16b17a2fdc526773f;p=loctrkd.git diff --git a/loctrkd/termconfig.py b/loctrkd/termconfig.py index f8f4b77..14df8cf 100644 --- a/loctrkd/termconfig.py +++ b/loctrkd/termconfig.py @@ -1,27 +1,57 @@ """ For when responding to the terminal is not trivial """ -from configparser import ConfigParser +from configparser import ConfigParser, SectionProxy from datetime import datetime, timezone from logging import getLogger from struct import pack +from typing import Any, Dict, List, Union import zmq from . import common from .zx303proto import * +from .zx303proto import STATUS, SETUP, POSITION_UPLOAD_INTERVAL from .zmsg import Bcast, Resp, topic log = getLogger("loctrkd/termconfig") +def normconf(section: SectionProxy) -> Dict[str, Any]: + result: Dict[str, Any] = {} + for key, val in section.items(): + vals = val.split("\n") + if len(vals) > 1 and vals[0] == "": + vals = vals[1:] + lst: List[Union[str, int]] = [] + for el in vals: + try: + lst.append(int(el, 0)) + except ValueError: + if el[0] == '"' and el[-1] == '"': + el = el.strip('"').rstrip('"') + lst.append(el) + if not ( + all([isinstance(x, int) for x in lst]) + or all([isinstance(x, str) for x in lst]) + ): + raise ValueError( + "Values of %s - %s are of different type", key, vals + ) + if len(lst) == 1: + result[key] = lst[0] + else: + result[key] = lst + return result + + def runserver(conf: ConfigParser) -> None: # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?! zctx = zmq.Context() # type: ignore zsub = zctx.socket(zmq.SUB) # type: ignore zsub.connect(conf.get("collector", "publishurl")) for proto in ( - proto_name(STATUS), - proto_name(SETUP), - proto_name(POSITION_UPLOAD_INTERVAL), + STATUS.proto_name(), + SETUP.proto_name(), + POSITION_UPLOAD_INTERVAL.proto_name(), ): zsub.setsockopt(zmq.SUBSCRIBE, topic(proto)) zpush = zctx.socket(zmq.PUSH) # type: ignore @@ -43,9 +73,9 @@ def runserver(conf: ConfigParser) -> None: "%s does not expect externally provided response", msg ) if zmsg.imei is not None and conf.has_section(zmsg.imei): - termconfig = common.normconf(conf[zmsg.imei]) + termconfig = normconf(conf[zmsg.imei]) elif conf.has_section("termconfig"): - termconfig = common.normconf(conf["termconfig"]) + termconfig = normconf(conf["termconfig"]) else: termconfig = {} kwargs = {}