From 94eeded67df0c6d1606b812a4c5ede932d9aafe7 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 24 Feb 2021 09:51:27 +0100 Subject: [PATCH] python: autodetect API URL --- python/villas/node/node.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/python/villas/node/node.py b/python/villas/node/node.py index 71be7bba1..bf4c1ce2c 100644 --- a/python/villas/node/node.py +++ b/python/villas/node/node.py @@ -1,4 +1,5 @@ import json +import os import tempfile import subprocess import logging @@ -20,19 +21,31 @@ class Node(object): self.api_url = api_url self.log_filename = log_filename - self.config = config - self.config_filename = config_filename self.executable = executable + if config_filename and config: + raise RuntimeError('Can\'t provide config_filename and ' + 'config at the same time!') + + if config_filename: + with open(self.config_filename) as f: + self.config = json.load(f) + else: + self.config = config + + # Try to deduct api_url from config + if self.api_url is None: + port = config.get('http', {}).get('port') + if port is None: + port = 80 if os.getuid() == 0 else 8080 + + self.api_url = f'http://localhost:{port}' + def start(self): self.config_file = tempfile.NamedTemporaryFile(mode='w+', suffix='.json') - if self.config_filename: - with open(self.config_filename) as f: - self.config_file.write(f.read()) - else: - json.dump(self.config, self.config_file) + json.dump(self.config, self.config_file) self.config_file.flush()