Load Settings From Configuration Files¶
It is often useful to load settings from configuration files in different parts of the workflow.
The loadns() function (short for “load namespace”)
simplifies this process:
- It supports loading from JSON, YAML, TOML and Python files.
- It assigns all loaded variables to a namespace, which is easier to use than a dictionary.
- It can load from multiple files and merge them into a single namespace.
- The step calling
loadns()is automatically amended with the loaded files as inputs, unless explicitly disabled.
Example¶
Example source files: docs/getting_started/load_namespace/
This is just a simple example with a single configuration file and a single script that uses it. In a real-world scenario, you would typically have multiple configuration files and scripts.
Create a file plan.py with the following contents:
#!/usr/bin/env python3
from stepup.core.api import run, static
static("config.toml", "print_sentence.py")
run("./print_sentence.py")
It calls a script print_sentence.py, which loads a configuration file config.toml and
prints a sentence to the standard output.
Create the script print_sentence.py with the following contents:
#!/usr/bin/env python3
from stepup.core.api import loadns
config = loadns("config.toml")
print(f"I love {config.fruit} an {config.vegetable} salad.")
Finally, create the configuration file config.toml with the following contents:
Make the appropriate files executable and run the plan:
You should get the following screen output:
DIRECTOR │ Listening on /tmp/stepup-########/director (StepUp Core 3.2.3.post54)
STARTUP │ (Re)initialized boot script
PHASE │ build
START │ ./plan.py
SUCCESS │ ./plan.py
START │ ./print_sentence.py
SUCCESS │ ./print_sentence.py
─────────────────────────────── Standard output ────────────────────────────────
I love apple an carrot salad.
────────────────────────────────────────────────────────────────────────────────
DIRECTOR │ Trying to delete 0 outdated output(s)
DIRECTOR │ See you!
As you can see, the sentence contains elements from the configuration file.
Try the Following¶
-
Run the plan again with
sb -j 1. The steps are not executed again, as expected. -
Change some fields in the configuration file and run the plan again. This time, the step
print_sentence.pyis executed again, now with the new values from the configuration file.