Skip to content

Commit

Permalink
Merge pull request #2 from daverolo/envars
Browse files Browse the repository at this point in the history
REFACTOR: support environment variables
  • Loading branch information
montajebi authored Nov 25, 2022
2 parents 001a56a + 759fbec commit 5b8f99d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand All @@ -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();
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions config/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 5b8f99d

Please sign in to comment.