From 5f4d5b298ada88fbbc1b6f6a0a55d67e1966d53b Mon Sep 17 00:00:00 2001 From: daverolo <107847185+daverolo@users.noreply.github.com> Date: Fri, 25 Nov 2022 14:09:06 +0100 Subject: [PATCH 1/3] FIX: set list as default if argparse action is append --- config/omegaconf.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/omegaconf.py b/config/omegaconf.py index 275b908..9f0a2b7 100644 --- a/config/omegaconf.py +++ b/config/omegaconf.py @@ -106,6 +106,8 @@ def from_argparse( v['xkwargs']["default"] = '' if v['xkwargs']["type"] == int: v['xkwargs']["default"] = "0" + if "action" in v['xkwargs'] and v['xkwargs']["action"].lower().strip() == "append": + v['xkwargs']["default"] = [] parser.add_argument(*v["xargs"],**v["xkwargs"]) parsed_args = vars(parser.parse_args()) parsed_args = {k: v for k, v in parsed_args.items() if v} From dc802f1887a2e6721dbab1b29c5a3dcab7e13552 Mon Sep 17 00:00:00 2001 From: daverolo <107847185+daverolo@users.noreply.github.com> Date: Fri, 25 Nov 2022 14:09:49 +0100 Subject: [PATCH 2/3] REFACTOR: support environment variables --- config/config.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/config.py b/config/config.py index f945bf4..3b1a275 100644 --- a/config/config.py +++ b/config/config.py @@ -5,6 +5,9 @@ from os.path import exists as file_exists from .omegaconf import OmegaConfArgparse as OmegaConf, DictConfig from .definition import Schema, cli_args +from os import environ +from sys import argv +import re class Config(): @@ -26,6 +29,7 @@ def init( cls._schema = schema cls._cli_args = cli_args cls._strict = strict + cls._add_environment_variables() cls._setup_schema() cls._setup_user_config(); #cls._setup_cli_config_omega(); @@ -37,6 +41,22 @@ def init( def _is_private(cls): if not cls._initialized: raise RuntimeError("invalid call on private method") + @classmethod + def _add_environment_variables(cls): + cls._is_private() + for k, v in environ.items(): + k = k.upper() + #if not k.startswith("SYLI_") or k == "SYLI_CONFIG": # all "SYLI_*" except config file + if not k.startswith("SYLI_"): # all "SYLI_*" + continue + # support lists as environment variables, e.g.: SYLI_NODE_1, SYLI_NODE_2 -> --node val1 --node val2 + m = re.search(r'(.*)(_\d+)$', k) + if m is not None: + k=m.group(1) + argv.append(f"--{k[5:].lower()}") + if v != "": + argv.append(v) + @classmethod def _setup_schema(cls,lala="",lolo=""): cls._is_private() From 759fbec518242a329d09345709bdeb40447342b1 Mon Sep 17 00:00:00 2001 From: daverolo <107847185+daverolo@users.noreply.github.com> Date: Fri, 25 Nov 2022 14:11:33 +0100 Subject: [PATCH 3/3] DOCS: update readme for env-vars support --- README.md | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 20d2b34..00a8a30 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ -# SyncLink Client PoC +# WARNING + +**CURRENTLY IN DEVELOPMENT - DO NOT USE IN PRODUCTION!** + +You can start the SyncLink Client as described in this document however during development we continue implementing changes that could (and very likely will) make some or all of the described operations and tasks below obsolete. + +# SyncLink Client A very basic implementation of the [SyncLink Client](https://github.com/stereum-dev/synclink-spec/wiki/SyncLink-Client). @@ -58,6 +64,40 @@ pip install -r requirements.txt Optional you can also specify the path to your config file or add/overwrite this arguments on the command line. Run `python main.py -h` for details. +### Environment variables + +You can also configure your app by environment variables. These are identical to the command line arguments but must be prefixed with "SYLI_" and always UPPERCASE. +For example, to specify `addr` as environment variable: + +``` +export SYLI_ADDR="127.0.0.1" +python main.py +``` +will be converted to `python main.py --addr "127.0.0.1"`. + +For lists, the environment variables need additionally suffixed with a number, e.g.: +``` +export SYLI_NODE_1="10.0.0.1" +export SYLI_NODE_2="10.0.0.2" +python main.py +``` +will be converted to `python main.py --node "10.0.0.1" --node "10.0.0.2"`. + +If you have accidently specified an environment variable that is not suported on the command line, you need to unset the variable to avoid an error. For example: + +``` +export SYLI_BLA="12345" +python main.py +``` +will be converted to `python main.py --bla "12345"`. + +and of course results in the error `main.py: error: unrecognized arguments: --bla 12345`. + +Therefore, you'd have to remove the environment variable: + +``` +unset SYLI_BLA +``` ## Run the App Activate the virtual environment and run the app.