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 logging.handlers import SysLogHandler
7 from sys import argv, stderr, stdout
9 CONF = "/etc/gps303.conf"
11 DBFN = "/var/lib/gps303/gps303.sqlite"
14 def init(log, opts=None):
16 opts, _ = getopt(argv[1:], "c:d")
18 conf = readconfig(opts["-c"] if "-c" in opts else CONF)
20 log.addHandler(StreamHandler(stderr))
22 log.addHandler(SysLogHandler(address="/dev/log"))
23 log.setLevel(DEBUG if "-d" in opts else INFO)
24 log.info("starting with options: %s", opts)
28 def readconfig(fname):
29 config = ConfigParser()
30 config["collector"] = {
36 config["termconfig"] = {}
41 def normconf(section):
43 for key, val in section.items():
44 vals = val.split("\n")
45 if len(vals) > 1 and vals[0] == "":
52 if el[0] == '"' and el[-1] == '"':
53 el = el.strip('"').rstrip('"')
61 if __name__ == "__main__":
64 def _print_config(conf):
65 for section in conf.sections():
66 print("section", section)
67 for option in conf.options(section):
68 print(" ", option, conf[section][option])
70 conf = readconfig(argv[1])
72 print(normconf(conf["termconfig"]))