]> www.average.org Git - loctrkd.git/blob - gps303/common.py
initial storage service
[loctrkd.git] / gps303 / common.py
1 """ Common housekeeping for all daemons """
2
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
7
8 CONF = "/etc/gps303.conf"
9 PORT = 4303
10 DBFN = "/var/lib/gps303/gps303.sqlite"
11
12 def init(log):
13     opts, _ = getopt(argv[1:], "c:d")
14     opts = dict(opts)
15     conf = readconfig(opts["-c"] if "-c" in opts else CONF)
16     if stdout.isatty():
17         log.addHandler(StreamHandler(stderr))
18     else:
19         log.addHandler(SysLogHandler(address="/dev/log"))
20     log.setLevel(DEBUG if "-d" in opts else INFO)
21     log.info("starting with options: %s", opts)
22     return conf
23
24 def readconfig(fname):
25     config = ConfigParser()
26     config["collector"] = {
27         "port": PORT,
28     }
29     config["storage"] = {
30         "dbfn": DBFN,
31     }
32     config["device"] = {}
33     #_print_config(config)
34     #print("now reading", fname)
35     config.read(fname)
36     #_print_config(config)
37     return config
38
39 if __name__ == "__main__":
40     from sys import argv
41
42     def _print_config(conf):
43         for section in conf.sections():
44             print("section", section)
45             for option in conf.options(section):
46                 print("    ", option, conf[section][option])
47
48     conf = readconfig(argv[1])
49     _print_config(conf)
50     print("binaryswitch", int(conf.get("device", "binaryswitch"), 0))