12. pypsi.wizard - Prompt Wizards¶
Command line input wizards.
-
class
pypsi.wizard.PromptWizard(name, description, steps=None, features=None)[source]¶ Bases:
objectA user input prompt wizards.
PromptWizards will walk the user through a series of questions (
WizardStep) and accept input. The user may at any time enter the?key to get help regarding the current step.Each step can have validators that determine if a value is valid before proceeding to the next step. Also, each step can have a default value that is saved if the user hits
Returnwith no input.Once complete, the wizard will return a
Namespaceobject that contains all the user’s answers. Each step contains anidattribute that determines what variable is set in the returned namespace.For example, a step may have an id of
"ip_addr". When the user enters"192.168.0.1"for this step, the input can be retrieved through the namespace’sip_addrattribute.Parameters: - name (str) – the prompt wizard name to display to the user
- description (str) – a short description of what the wizard does
- steps (list) – a list of
WizardStepobjects
-
run(shell, print_header=True)[source]¶ Execute the wizard, prompting the user for input.
Parameters: shell (pypsi.shell.Shell) – the active shell Returns: a Namespaceobject containing all the answers on success,Noneif the user exited the wizard
-
class
pypsi.wizard.WizardStep(id, name, help, default=None, completer=None, validators=None)[source]¶ Bases:
objectA single input step in a prompt wizard.
Parameters: - id (str) – the step io, used for referencing the step’s value
- name (str) – the name to display for input to the user
- help (str) – the help message to display to the user
- default (str) – the default value if the user immediately hits “Return”
- completer – a completion function
- validators – a single or a list of validators
-
complete(wizard, args, prefix)[source]¶ Get the list of possible completions for input. This will call the local completer function (
self.completerwith the arguments: (wizard,args,prefix).- wizard (
PromptWizard) - the active wizard - args (
list) - the list of input arguments - prefix (
str) - the input prefix
This function mirrors the command
complete()function.- wizard (
-
validate(ns, value)[source]¶ Validate the input value. This will call the local validators (
self.validators) sequentially with the following arguments:- ns (
Namespace) - the current input values - value - the value to validate
Validators may change the value in place (if it is a mutable object) or may return the validated value that will be passed to the remaining validators. If a validation error occurs, raise a ValueError exception.
Parameters: - ns (pypsi.namespace.Namespace) – current input values
- value – current input value
Returns: validated value
- ns (
-
pypsi.wizard.boolean_validator(ns, value)[source]¶ Boolean validator. Raises ValueError if input isn’t a boolean string.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns bool: validated value
-
pypsi.wizard.choice_validator(choices)[source]¶ String choice validator. Raises ValueError if input isn’t a valid choice.
Parameters: choices (list) – valid choices Returns: validator function
-
pypsi.wizard.directory_validator(ns, value)[source]¶ Directory path validator. Raises ValueError on validation error.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns: validated value
-
pypsi.wizard.file_validator(ns, value)[source]¶ File path validator. Raises ValueError on validation error.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns: validated value
-
pypsi.wizard.hostname_or_ip_validator(ns, value)[source]¶ Network hostname or IPv4 address validator. Raises ValueError on validation error.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns: validated value
-
pypsi.wizard.int_validator(min=None, max=None)[source]¶ Integer value wizard validator creator.
Parameters: Returns: validator function
-
pypsi.wizard.lowercase_validator(ns, value)[source]¶ Converts input string to lowercase.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns: validated value (in lowercase)
-
pypsi.wizard.module_name_validator(type_str)[source]¶ Python module name validator. Raises ValueError on validation error.
Parameters: type_str (str) – the input type to reference when raising validation errors. Returns: validator function
-
pypsi.wizard.package_name_validator(type_str)[source]¶ Python package name validator. Raises ValueError on validation error.
Parameters: type_str (str) – the input type to reference when raising validation errors. Returns: validator function
-
pypsi.wizard.required_validator(ns, value)[source]¶ Required value wizard validator. Raises ValueError on validation error.
Parameters: - ns (pypsi.namespace.Namespace) – active namespace
- value – input value
Returns: validated value