X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fcommon.py;fp=gps303%2Fcommon.py;h=0e7aac12e11d29f31f96b786e6386406f98d9d0c;hb=4c173a5448990cd4da398be1bf3479bef17b1048;hp=0000000000000000000000000000000000000000;hpb=2c60838ac44f02802480eeca626040231014caaa;p=loctrkd.git diff --git a/gps303/common.py b/gps303/common.py new file mode 100644 index 0000000..0e7aac1 --- /dev/null +++ b/gps303/common.py @@ -0,0 +1,50 @@ +""" Common housekeeping for all daemons """ + +from configparser import ConfigParser +from getopt import getopt +from logging import getLogger, StreamHandler, DEBUG, INFO +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") + opts = dict(opts) + conf = readconfig(opts["-c"] if "-c" in opts else CONF) + if stdout.isatty(): + log.addHandler(StreamHandler(stderr)) + else: + log.addHandler(SysLogHandler(address="/dev/log")) + log.setLevel(DEBUG if "-d" in opts else INFO) + log.info("starting with options: %s", opts) + return conf + +def readconfig(fname): + config = ConfigParser() + config["collector"] = { + "port": PORT, + } + config["storage"] = { + "dbfn": DBFN, + } + config["device"] = {} + #_print_config(config) + #print("now reading", fname) + config.read(fname) + #_print_config(config) + return config + +if __name__ == "__main__": + from sys import argv + + def _print_config(conf): + for section in conf.sections(): + print("section", section) + for option in conf.options(section): + print(" ", option, conf[section][option]) + + conf = readconfig(argv[1]) + _print_config(conf) + print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))