]> www.average.org Git - loctrkd.git/blobdiff - gps303/common.py
Do not write startup message for command-line cmds
[loctrkd.git] / gps303 / common.py
index 0e7aac12e11d29f31f96b786e6386406f98d9d0c..8afd65edfd65bd6e5efcb4b8df8234d79bccdfd4 100644 (file)
@@ -2,25 +2,44 @@
 
 from configparser import ConfigParser
 from getopt import getopt
-from logging import getLogger, StreamHandler, DEBUG, INFO
+from logging import Formatter, getLogger, StreamHandler, DEBUG, INFO
+from logging.handlers import SysLogHandler
+from pkg_resources import get_distribution, DistributionNotFound
 from sys import argv, stderr, stdout
 
 CONF = "/etc/gps303.conf"
 PORT = 4303
 DBFN = "/var/lib/gps303/gps303.sqlite"
 
-def init(log):
-    opts, _ = getopt(argv[1:], "c:d")
+try:
+    version = get_distribution("gps303").version
+except DistributionNotFound:
+    version = "<local>"
+
+
+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)
+    log.setLevel(DEBUG if "-d" in opts else INFO)
     if stdout.isatty():
-        log.addHandler(StreamHandler(stderr))
+        hdl = StreamHandler(stderr)
+        hdl.setFormatter(
+            Formatter("%(asctime)s - %(levelname)s - %(message)s")
+        )
+        log.addHandler(hdl)
+        log.debug("%s starting with options: %s", version, opts)
     else:
-        log.addHandler(SysLogHandler(address="/dev/log"))
-    log.setLevel(DEBUG if "-d" in opts else INFO)
-    log.info("starting with options: %s", opts)
+        hdl = SysLogHandler(address="/dev/log")
+        hdl.setFormatter(
+            Formatter("%(name)s[%(process)d]: %(levelname)s - %(message)s")
+        )
+        log.addHandler(hdl)
+        log.info("%s starting with options: %s", version, opts)
     return conf
 
+
 def readconfig(fname):
     config = ConfigParser()
     config["collector"] = {
@@ -29,13 +48,31 @@ def readconfig(fname):
     config["storage"] = {
         "dbfn": DBFN,
     }
-    config["device"] = {}
-    #_print_config(config)
-    #print("now reading", fname)
+    config["termconfig"] = {}
     config.read(fname)
-    #_print_config(config)
     return config
 
+
+def normconf(section):
+    result = {}
+    for key, val in section.items():
+        vals = val.split("\n")
+        if len(vals) > 1 and vals[0] == "":
+            vals = vals[1:]
+        lst = []
+        for el in vals:
+            try:
+                el = int(el, 0)
+            except ValueError:
+                if el[0] == '"' and el[-1] == '"':
+                    el = el.strip('"').rstrip('"')
+            lst.append(el)
+        if len(lst) == 1:
+            [lst] = lst
+        result[key] = lst
+    return result
+
+
 if __name__ == "__main__":
     from sys import argv
 
@@ -47,4 +84,4 @@ if __name__ == "__main__":
 
     conf = readconfig(argv[1])
     _print_config(conf)
-    print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))
+    print(normconf(conf["termconfig"]))