1 """ Common housekeeping for all daemons """
3 from configparser import ConfigParser
4 from getopt import getopt
5 from logging import getLogger, StreamHandler, DEBUG, INFO
6 from sys import argv, stderr, stdout
8 CONF = "/etc/gps303.conf"
10 DBFN = "/var/lib/gps303/gps303.sqlite"
13 def init(log, opts=None):
15 opts, _ = getopt(argv[1:], "c:d")
17 conf = readconfig(opts["-c"] if "-c" in opts else CONF)
19 log.addHandler(StreamHandler(stderr))
21 log.addHandler(SysLogHandler(address="/dev/log"))
22 log.setLevel(DEBUG if "-d" in opts else INFO)
23 log.info("starting with options: %s", opts)
27 def readconfig(fname):
28 config = ConfigParser()
29 config["collector"] = {
35 config["termconfig"] = {}
40 def normconf(section):
42 for key, val in section.items():
43 vals = val.split("\n")
44 if len(vals) > 1 and vals[0] == "":
51 if el[0] == '"' and el[-1] == '"':
52 el = el.strip('"').rstrip('"')
60 if __name__ == "__main__":
63 def _print_config(conf):
64 for section in conf.sections():
65 print("section", section)
66 for option in conf.options(section):
67 print(" ", option, conf[section][option])
69 conf = readconfig(argv[1])
71 print(normconf(conf["termconfig"]))