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)
25 log.setLevel(DEBUG if "-d" in opts else INFO)
27 hdl = StreamHandler(stderr)
29 Formatter("%(asctime)s - %(levelname)s - %(message)s")
32 log.debug("%s starting with options: %s", version, opts)
34 hdl = SysLogHandler(address="/dev/log")
36 Formatter("%(name)s[%(process)d]: %(levelname)s - %(message)s")
39 log.info("%s starting with options: %s", version, opts)
43 def readconfig(fname):
44 config = ConfigParser()
45 config["collector"] = {
51 config["termconfig"] = {}
56 def normconf(section):
58 for key, val in section.items():
59 vals = val.split("\n")
60 if len(vals) > 1 and vals[0] == "":
67 if el[0] == '"' and el[-1] == '"':
68 el = el.strip('"').rstrip('"')
76 if __name__ == "__main__":
79 def _print_config(conf):
80 for section in conf.sections():
81 print("section", section)
82 for option in conf.options(section):
83 print(" ", option, conf[section][option])
85 conf = readconfig(argv[1])
87 print(normconf(conf["termconfig"]))