Creating installer wizards with RKD

Wizard is a component designed to create comfortable installers, where user has to answer a few questions to get the task done.

Concept

  • User answers questions invoked by ask() method calls
  • At the end the finish() is called, which acts as a commit, saves answers into .rkd/tmp-wizard.json by default and into the .env file (depends on if to_env=true was specified)
  • Next RKD task executed can read .rkd/tmp-wizard.json looking for answers, the answers placed in .env are already loaded automatically as part of standard mechanism of environment variables support

Example Wizard

from rkd.api.inputoutput import Wizard

# self is the TaskInterface instance, in Makefile.yaml it would be "this", in Python code it is "self"
Wizard(self)\
    .ask('Service name', attribute='service_name', regexp='([A-Za-z0-9_]+)', default='redis')\
    .finish()
Service name [([A-Za-z0-9_]+)] [default: redis]:
    -> redis

Example of application that is using Wizard to ask interactive questions

https://github.com/riotkit-org/rkd-coop/raw/master/docs/demo.gif

Using Wizard results internally

Wizard is designed to keep the data on the disk, so you can access it in any other task executed, but this is not mandatory. You can skip committing changes to disk by not using finish() which is flushing data to json and to .env files.

Use wizard.answers to see all answers that would be put into json file, and wizard.to_env to browse all environment variables that would be set in .env if finish() would be used.

Example of loading stored values by other task

Wizard stores values into file and into .env file, so it can read it from file after it was stored there. This allows you to separate Wizard questions into one RKD task, and the rest of logic/steps into other RKD tasks.

from rkd.api.inputoutput import Wizard

# ... assuming that previously the Wizard was completed by user and the finish() method was called ...

wizard = Wizard(self)
wizard.load_previously_stored_values()

print(wizard.answers, wizard.to_env)

API

class rkd.api.inputoutput.Wizard(task: TaskInterface, filename: str = 'tmp-wizard.json')
ask(title: str, attribute: str, regexp: str = '', to_env: bool = False, default: str = None, choices: list = [], secret: bool = False) → rkd.api.inputoutput.Wizard

Asks user a question

Usage:

wizard = Wizard(self) wizard.ask(‘In which year the Spanish social revolution has begun?’,

attribute=’year’, choices=[‘1936’, ‘1910’])

wizard.finish()

finish() → rkd.api.inputoutput.Wizard

Commit all pending changes into json and .env files

input(secret: bool = False)

Extracted for unit testing to be possible easier

load_previously_stored_values()

Load previously saved values