From: Eugene Crosser Date: Wed, 16 Mar 2022 13:36:13 +0000 (+0100) Subject: Use command line args and config more X-Git-Tag: 0.01~66 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=da4a83724f675e9b46639742eb095dc5b17a357f Use command line args and config more --- diff --git a/gps303/GT06mod.py b/gps303/GT06mod.py index 31be5d3..3256d6c 100755 --- a/gps303/GT06mod.py +++ b/gps303/GT06mod.py @@ -15,6 +15,7 @@ log = getLogger("gps303") class _GT06pkt: PROTO: int + CONFIG = None def __init__(self, *args, **kwargs): assert len(args) == 0 @@ -263,3 +264,6 @@ def handle_packet(packet, addr, when): def make_response(msg): return msg.response() + +def set_config(config): # Note that we are setting _class_ attribute + _GT06pkt.CONFIG = config diff --git a/gps303/__main__.py b/gps303/__main__.py index c8c5a28..ef03c61 100755 --- a/gps303/__main__.py +++ b/gps303/__main__.py @@ -7,7 +7,7 @@ import sys from time import time from .config import readconfig -from .GT06mod import handle_packet, make_response, LOGIN +from .GT06mod import handle_packet, make_response, LOGIN, set_config from .evstore import initdb, stow CONF = "/etc/gps303.conf" @@ -15,18 +15,19 @@ CONF = "/etc/gps303.conf" log = getLogger("gps303") if __name__.endswith("__main__"): - opts, _ = getopt(sys.argv[1:], "c:p:") + opts, _ = getopt(sys.argv[1:], "c:d") opts = dict(opts) - conf = readconfig(opts["c"] if "c" in opts else CONF) + conf = readconfig(opts["-c"] if "-c" in opts else CONF) if sys.stdout.isatty(): log.addHandler(StreamHandler(sys.stderr)) - log.setLevel(DEBUG) else: log.addHandler(SysLogHandler(address="/dev/log")) - log.setLevel(INFO) + log.setLevel(DEBUG if "-d" in opts else INFO) + log.info("starting with options: %s", opts) initdb(conf.get("daemon", "dbfn")) + set_config(conf) ctlsock = socket(AF_INET, SOCK_STREAM) ctlsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) diff --git a/gps303/config.py b/gps303/config.py index 6102e4a..fe4f696 100644 --- a/gps303/config.py +++ b/gps303/config.py @@ -5,20 +5,25 @@ DBFN = "/var/lib/gps303/gps303.sqlite" def readconfig(fname): config = ConfigParser() + config["daemon"] = { + "port": PORT, + "dbfn": DBFN, + } + config["device"] = {} + #_print_config(config) + #print("now reading", fname) config.read(fname) - if not config.has_section("daemon"): - config.add_section("daemon") - if not config.has_option("daemon", "port"): - config["daemon"]["port"] = str(PORT) - if not config.has_option("daemon", "dbfn"): - config["daemon"]["dbfn"] = DBFN + #_print_config(config) return config -if __name__ == "__main__": - from sys import argv - conf = readconfig(argv[1]) +def _print_config(conf): for section in conf.sections(): print("section", section) for option in conf.options(section): print(" ", option, conf[section][option]) + +if __name__ == "__main__": + from sys import argv + conf = readconfig(argv[1]) + _print_config(conf) print("binaryswitch", int(conf.get("device", "binaryswitch"), 0)) diff --git a/gps303/evstore.py b/gps303/evstore.py index 1b1ae2d..360ab35 100644 --- a/gps303/evstore.py +++ b/gps303/evstore.py @@ -18,6 +18,7 @@ SCHEMA = """create table if not exists events ( def initdb(dbname): global DB + log.info("Using Sqlite3 database \"%s\"", dbname) DB = connect(dbname) DB.execute(SCHEMA) @@ -30,7 +31,6 @@ def stow(clntaddr, timestamp, imei, proto, payload): (str(clntaddr), timestamp, imei, proto, payload), ) ) - log.debug("inserting %s", parms) DB.execute( """insert or ignore into events (timestamp, imei, clntaddr, proto, payload)