Skip to content

Load Settings From Configuration Files

It is often useful to load settings from configuration files in different parts of the workflow. The loadns() (short for “load namespace”) makes this easy:

  • 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 this is 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 runsh, static

static("config.toml")
runsh("./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:

fruit = "apple"
vegetable = "carrot"

Make the appropriate files executable and run the plan:

chmod +x plan.py print_sentence.py
stepup boot -n 1

You should get the following screen output:

  DIRECTOR │ Listening on /tmp/stepup-########/director (StepUp 3.0.0)
   STARTUP │ (Re)initialized boot script
  DIRECTOR │ Launched worker 0
     PHASE │ run
     START │ runpy ./plan.py
   SUCCESS │ runpy ./plan.py
     START │ runsh ./print_sentence.py
   SUCCESS │ runsh ./print_sentence.py
─────────────────────────────── Standard output ────────────────────────────────
I love apple an carrot salad.
────────────────────────────────────────────────────────────────────────────────
  DIRECTOR │ Trying to delete 0 outdated output(s)
  DIRECTOR │ Stopping workers
  DIRECTOR │ See you!

As you can see, the sentence contains elements from the configuration file.

Try the Following

  • Run the plan again with stepup boot -n 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.py is executed again, now with the new values from the configuration file.