1 """ Common housekeeping for all daemons """
3 from configparser import ConfigParser
4 from getopt import getopt
5 from logging import Formatter, getLogger, StreamHandler, DEBUG, INFO
6 from logging.handlers import SysLogHandler
7 from pkg_resources import get_distribution, DistributionNotFound
8 from sys import argv, stderr, stdout
10 CONF = "/etc/gps303.conf"
12 DBFN = "/var/lib/gps303/gps303.sqlite"
15 version = get_distribution("gps303").version
16 except DistributionNotFound:
20 def init(log, opts=None):
22 opts, _ = getopt(argv[1:], "c:d")
24 conf = readconfig(opts["-c"] if "-c" in opts else CONF)
26 hdl = StreamHandler(stderr)
28 Formatter("%(asctime)s - %(levelname)s - %(message)s")
32 hdl = SysLogHandler(address="/dev/log")
34 Formatter("%(name)s[%(process)d]: %(levelname)s - %(message)s")
37 log.setLevel(DEBUG if "-d" in opts else INFO)
38 log.info("%s starting with options: %s", version, opts)
42 def readconfig(fname):
43 config = ConfigParser()
44 config["collector"] = {
50 config["termconfig"] = {}
55 def normconf(section):
57 for key, val in section.items():
58 vals = val.split("\n")
59 if len(vals) > 1 and vals[0] == "":
66 if el[0] == '"' and el[-1] == '"':
67 el = el.strip('"').rstrip('"')
75 if __name__ == "__main__":
78 def _print_config(conf):
79 for section in conf.sections():
80 print("section", section)
81 for option in conf.options(section):
82 print(" ", option, conf[section][option])
84 conf = readconfig(argv[1])
86 print(normconf(conf["termconfig"]))