From 901b3ca9601b9ff56796329f2db776433ea4e846 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Fri, 16 Oct 2020 15:36:34 +0200 Subject: [PATCH] Made database file configurable --- .gitignore | 3 ++- torch-sub.conf | 9 +++++++++ torchsub/torch_sub.py | 15 +++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 torch-sub.conf diff --git a/.gitignore b/.gitignore index b83b5d2..483c03b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ venv .idea /test/clients.json *.pyc -*.egg-info \ No newline at end of file +*.egg-info +*.json \ No newline at end of file diff --git a/torch-sub.conf b/torch-sub.conf new file mode 100644 index 0000000..e00ed0f --- /dev/null +++ b/torch-sub.conf @@ -0,0 +1,9 @@ +############################################################################## +# Sample torch-sub.conf file +############################################################################## + +[mqtt] +Topic=torch/+/wake #Required + +[database] +Filename=torch_clients.json #Optional; Default: clients.json \ No newline at end of file diff --git a/torchsub/torch_sub.py b/torchsub/torch_sub.py index 56c6de3..7235dac 100644 --- a/torchsub/torch_sub.py +++ b/torchsub/torch_sub.py @@ -15,10 +15,6 @@ database_lock = threading.Lock() def update_client_record(client, userdata, message): database_lock.acquire() - if not os.path.exists(database_filename): - with open(database_filename, 'w') as database_blank: - json.dump({}, database_blank) - with open(database_filename, 'r') as infile: database = json.load(infile) @@ -47,6 +43,7 @@ class Config: self.broker_port = None self.topic = None self.tls = None + self.database_file = database_filename parser = self.do_cli_argument_parsing() (config_path, config_filename) = self.get_config_path(parser.parse_args()) print("Using torch configuration path: " + config_path) @@ -98,10 +95,20 @@ class Config: 'certfile': cert_file, 'keyfile': key_file } + if config.has_section('database'): + self.database_file = config['database'].get('Filename', fallback=database_filename) def main(): config = Config() + + global database_filename + database_filename = config.database_file + + if not os.path.exists(database_filename): + with open(database_filename, 'w') as database_blank: + json.dump({}, database_blank) + subscribe(config.broker_hostname, config.broker_port, config.topic,