changeset 3635:7bc443253b7c

merge branche "@"
author Goffi <goffi@goffi.org>
date Sat, 28 Aug 2021 15:32:58 +0200
parents 597a535ee187 (current diff) 3c7a64d6f49f (diff)
children 51983c55c5b6
files doc/libervia-cli/pubsub.rst sat/core/sat_main.py
diffstat 7 files changed, 48 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/doc/libervia-cli/pubsub.rst	Fri Aug 27 15:27:14 2021 +0200
+++ b/doc/libervia-cli/pubsub.rst	Sat Aug 28 15:32:58 2021 +0200
@@ -346,7 +346,7 @@
    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::
--- a/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat/bridge/bridge_constructor/constructors/pb/pb_core_template.py	Sat Aug 28 15:32:58 2021 +0200
@@ -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 @@
         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 @@
                 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:
--- a/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py	Sat Aug 28 15:32:58 2021 +0200
@@ -140,29 +140,15 @@
     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}")
--- a/sat/bridge/pb.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat/bridge/pb.py	Sat Aug 28 15:32:58 2021 +0200
@@ -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 @@
         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 @@
                 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:
--- a/sat/core/sat_main.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat/core/sat_main.py	Sat Aug 28 15:32:58 2021 +0200
@@ -86,7 +86,10 @@
             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:
--- a/sat/tools/config.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat/tools/config.py	Sat Aug 28 15:32:58 2021 +0200
@@ -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 @@
         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_<prefix>_` will be prefixed (and name
+        will be set to uppercase).
+        For config file, `<prefix>_` 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)
--- a/sat_frontends/bridge/pb.py	Fri Aug 27 15:27:14 2021 +0200
+++ b/sat_frontends/bridge/pb.py	Sat Aug 28 15:32:58 2021 +0200
@@ -140,29 +140,15 @@
     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}")