From 404a2820cfd24c282c8d6dc3a1ba0a54553d78b0 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 27 Aug 2021 14:59:47 +0200 Subject: [PATCH 1/2] doc: minor typo --- doc/libervia-cli/pubsub.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/libervia-cli/pubsub.rst b/doc/libervia-cli/pubsub.rst index 5b4e3940..05757476 100644 --- a/doc/libervia-cli/pubsub.rst +++ b/doc/libervia-cli/pubsub.rst @@ -343,7 +343,7 @@ Imagine that you want to replace all occurrences of "sàt" by "Libervia" in your else: print(item_raw.replace("sàt", "Libervia")) -And save it a some location, e.g. ``~/expand_sat.py`` (don't forget to make is executable +And save it a some location, e.g. ``~/expand_sat.py`` (don't forget to make it executable with ``chmod +x ~/expand_sat.py``). To be sure it's safe, you can first do a dry-run and check the result:: From 547eb9febaecc9b2d23608906a69ec32c06e2156 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 28 Aug 2021 15:26:02 +0200 Subject: [PATCH 2/2] bridge: bridge can now be set using environment variable: The `LIBERVIA_BRIDGE_NAME` environment variable can now be used to specify the bridge to use. If set and different from empty string, the environment has precedence over config file value. For `pb` bridge, the following environment variable can also be used: - LIBERVIA_BRIDGE_PB_CONNECTION_TYPE - LIBERVIA_BRIDGE_PB_HOST - LIBERVIA_BRIDGE_PB_PORT --- .../constructors/pb/pb_core_template.py | 16 +++-------- .../constructors/pb/pb_frontend_template.py | 22 +++------------ sat/bridge/pb.py | 16 +++-------- sat/core/sat_main.py | 5 +++- sat/tools/config.py | 27 +++++++++++++++++++ sat_frontends/bridge/pb.py | 22 +++------------ 6 files changed, 47 insertions(+), 61 deletions(-) diff --git a/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py b/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py index 71e1075b..e8a8610c 100644 --- a/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py +++ b/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py @@ -19,6 +19,7 @@ import dataclasses +from functools import partial from pathlib import Path from twisted.spread import jelly, pb from twisted.internet import reactor @@ -106,12 +107,8 @@ class Bridge(object): log.info("Init Perspective Broker...") self.root = PBRoot() conf = config.parseMainConf() - conn_type = config.getConfig( - conf, - "", - "bridge_pb_connection_type", - "unix_socket" - ) + getConf = partial(config.getConf, conf, "bridge_pb", "") + conn_type = getConf("connection_type", "unix_socket") if conn_type == "unix_socket": local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() socket_path = local_dir / "bridge_pb" @@ -120,12 +117,7 @@ class Bridge(object): str(socket_path), pb.PBServerFactory(self.root), mode=0o600 ) elif conn_type == "socket": - port = int(config.getConfig( - conf, - "", - "bridge_pb_port", - 8789 - )) + port = int(getConf("port", 8789)) log.info(f"using TCP Socket at port {port}") reactor.listenTCP(port, pb.PBServerFactory(self.root)) else: diff --git a/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py b/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py index 9f8a77d8..8f9457ea 100644 --- a/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py +++ b/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py @@ -140,29 +140,15 @@ class Bridge(object): def bridgeConnect(self, callback, errback): factory = pb.PBClientFactory() conf = config.parseMainConf() - conn_type = config.getConfig( - conf, - "", - "bridge_pb_connection_type", - "unix_socket" - ) + getConf = partial(config.getConf, conf, "bridge_pb", "") + conn_type = getConf("connection_type", "unix_socket") if conn_type == "unix_socket": local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() socket_path = local_dir / "bridge_pb" reactor.connectUNIX(str(socket_path), factory) elif conn_type == "socket": - host = int(config.getConfig( - conf, - "", - "bridge_pb_host", - "localhost" - )) - port = int(config.getConfig( - conf, - "", - "bridge_pb_port", - 8789 - )) + host = getConf("host", "localhost") + port = int(getConf("port", 8789)) reactor.connectTCP(host, port, factory) else: raise ValueError(f"Unknown pb connection type: {conn_type!r}") diff --git a/sat/bridge/pb.py b/sat/bridge/pb.py index 04132800..a9929d3a 100644 --- a/sat/bridge/pb.py +++ b/sat/bridge/pb.py @@ -19,6 +19,7 @@ import dataclasses +from functools import partial from pathlib import Path from twisted.spread import jelly, pb from twisted.internet import reactor @@ -106,12 +107,8 @@ class Bridge(object): log.info("Init Perspective Broker...") self.root = PBRoot() conf = config.parseMainConf() - conn_type = config.getConfig( - conf, - "", - "bridge_pb_connection_type", - "unix_socket" - ) + getConf = partial(config.getConf, conf, "bridge_pb", "") + conn_type = getConf("connection_type", "unix_socket") if conn_type == "unix_socket": local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() socket_path = local_dir / "bridge_pb" @@ -120,12 +117,7 @@ class Bridge(object): str(socket_path), pb.PBServerFactory(self.root), mode=0o600 ) elif conn_type == "socket": - port = int(config.getConfig( - conf, - "", - "bridge_pb_port", - 8789 - )) + port = int(getConf("port", 8789)) log.info(f"using TCP Socket at port {port}") reactor.listenTCP(port, pb.PBServerFactory(self.root)) else: diff --git a/sat/core/sat_main.py b/sat/core/sat_main.py index 571f0bbf..923700d3 100644 --- a/sat/core/sat_main.py +++ b/sat/core/sat_main.py @@ -85,7 +85,10 @@ class SAT(service.Service): trigger.TriggerManager() ) - bridge_name = self.memory.getConfig("", "bridge", "dbus") + bridge_name = ( + os.getenv("LIBERVIA_BRIDGE_NAME") + or self.memory.getConfig("", "bridge", "dbus") + ) bridge_module = dynamic_import.bridge(bridge_name) if bridge_module is None: diff --git a/sat/tools/config.py b/sat/tools/config.py index 17867343..3867a4fc 100644 --- a/sat/tools/config.py +++ b/sat/tools/config.py @@ -23,6 +23,7 @@ import os import csv import json +from typing import Any from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError from xdg import BaseDirectory from sat.core.log import getLogger @@ -142,3 +143,29 @@ def getConfig(config, section, name, default=None): except ValueError as e: raise exceptions.ParsingError("Error while parsing data: {}".format(e)) return value + + +def getConf( + conf: ConfigParser, + prefix: str, + section: str, + name: str, + default: Any +) -> Any: + """Get configuration value from environment or config file + + @param str: prefix to use for the varilable name (see `name` below) + @param section: config section to use + @param name: unsuffixed name. + For environment variable, `LIBERVIA__` will be prefixed (and name + will be set to uppercase). + For config file, `_` will be prefixed (and DEFAULT section will be + used). + Environment variable has priority over config values. If Environment variable + is set but empty string, config value will be used. + @param default: default value to use if varilable is set neither in environment, + nor in config + """ + # XXX: This is a temporary method until parameters are refactored + value = os.getenv(f"LIBERVIA_{prefix}_{name}".upper()) + return value or getConfig(conf, section, f"{prefix}_{name}", default) diff --git a/sat_frontends/bridge/pb.py b/sat_frontends/bridge/pb.py index 3b747078..4c7f5167 100644 --- a/sat_frontends/bridge/pb.py +++ b/sat_frontends/bridge/pb.py @@ -140,29 +140,15 @@ class Bridge(object): def bridgeConnect(self, callback, errback): factory = pb.PBClientFactory() conf = config.parseMainConf() - conn_type = config.getConfig( - conf, - "", - "bridge_pb_connection_type", - "unix_socket" - ) + getConf = partial(config.getConf, conf, "bridge_pb", "") + conn_type = getConf("connection_type", "unix_socket") if conn_type == "unix_socket": local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() socket_path = local_dir / "bridge_pb" reactor.connectUNIX(str(socket_path), factory) elif conn_type == "socket": - host = int(config.getConfig( - conf, - "", - "bridge_pb_host", - "localhost" - )) - port = int(config.getConfig( - conf, - "", - "bridge_pb_port", - 8789 - )) + host = getConf("host", "localhost") + port = int(getConf("port", 8789)) reactor.connectTCP(host, port, factory) else: raise ValueError(f"Unknown pb connection type: {conn_type!r}")