X-Git-Url: http://www.average.org/gitweb/?a=blobdiff_plain;f=loctrkd%2Ftermconfig.py;h=14df8cf5fb267c065b84f37a351e3c8c29b77a1c;hb=HEAD;hp=b1ea80ae691bb1b93174c50b5d58d8150d9065fd;hpb=84861997657f7a8daab41aa13790981bd77749f8;p=loctrkd.git diff --git a/loctrkd/termconfig.py b/loctrkd/termconfig.py index b1ea80a..14df8cf 100644 --- a/loctrkd/termconfig.py +++ b/loctrkd/termconfig.py @@ -1,9 +1,10 @@ """ 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 @@ -14,15 +15,43 @@ 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 @@ -44,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 = {}