]> www.average.org Git - loctrkd.git/blobdiff - loctrkd/termconfig.py
Update man pages to use correct name
[loctrkd.git] / loctrkd / termconfig.py
index f8f4b77e56538d0b15e5eb6969e8f2b9472658f0..14df8cf5fb267c065b84f37a351e3c8c29b77a1c 100644 (file)
@@ -1,27 +1,57 @@
 """ For when responding to the terminal is not trivial """
 
 """ 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 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 *
 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")
 
 
 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 (
 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
     ):
         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):
                     "%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"):
             elif conf.has_section("termconfig"):
-                termconfig = common.normconf(conf["termconfig"])
+                termconfig = normconf(conf["termconfig"])
             else:
                 termconfig = {}
             kwargs = {}
             else:
                 termconfig = {}
             kwargs = {}