clickyaml package
clickyaml.clickyaml module
Main module.
- clickyaml.clickyaml.construct_arguments(loader: Loader, node: MappingNode)[source]
Converts nodes with !arg tag to object of
click.Argument
.Passes the value associated with the node as arguments to the click.Argument constructor.
- Parameters:
loader (yaml.Loader) – The yaml loader
node (yaml.Node) – The current node in the yaml
- Returns:
an instance of click Argument
- Return type:
click.Argument
- Example:
!arg param_decls: [category]
This will be converted to
click.Argument(param_decls = ["category"])
- clickyaml.clickyaml.construct_env_vars(loader: Loader, node: ScalarNode)[source]
Extracts the environment variable from the node’s value.
- Parameters:
loader (yaml.Loader) – The yaml loader
node (yaml.ScalarNode) – The current node in the yaml
- Returns:
A Scalar Node with the pattern replaced by environment variables
- Return type:
yaml.ScalarNode
For a node
host: !ENV ${HOST}
replaces the${ ... }
with the value stored in the environment variable HOST
- clickyaml.clickyaml.construct_objects(loader: Loader, node: MappingNode)[source]
Converts nodes with !obj tag to object of specified class.
The yaml node needs to specified with the tag !obj. The first node should have key as class and the value should have the class object you want to create. Rest of the nodes are passed as parameters to the object at runtime.
- Parameters:
loader (yaml.Loader) – The yaml loader
node (yaml.Node) – The current node in the yaml
- Returns:
returns an object of type defined in the yaml node
- Return type:
Any
- Example:
type: !obj class: click.Choice choices: ["1","2","3","ALL"] case_sensitive: False
This will be converted to
click.Choice(choices = ["1","2","3","ALL"], case_sensitive = False)
- clickyaml.clickyaml.construct_options(loader: Loader, node: MappingNode)[source]
Converts nodes with !opt tag to object of
click.Option
.- Parameters:
loader (yaml.Loader) – The yaml loader
node (yaml.Node) – The current node in the yaml
- Returns:
an instance of click Option
- Return type:
click.Option
- Example:
!opt param_decls: ["--type", "-t"]
This will be converted to
click.Option(param_decls = ["--type","-t"])
- clickyaml.clickyaml.get_command(name: str, parsed_yaml: dict, callback=None) Command [source]
Returns the desired command from the yaml file
It has the ability to assign custom callbacks to the command. If a callback is not passed a default callback is assigned. The default callback runs the script associated with the command.
See also
The callback property of Commander class.
- Parameters:
name (str) – Name of the command.
parsed_yaml (str) – Dictionary from the parsed yaml of the command. Contains the parameters to be passed to the click constructor
callback (Callable | None, optional) – The callback to invoke on running the command, defaults to None
- Returns:
The click command with desired parameters.
- Return type:
click.Command
- clickyaml.clickyaml.get_commanders(yaml: str) dict [source]
Returns all the
Commander
objects from the yaml data in a python dictionary- Parameters:
yaml (str) – The yaml data, this can be path to a file or a string
- Returns:
A dictionary of Commander objects
- Return type:
dict[str, Commander]
- clickyaml.clickyaml.parse_yaml(path=None, data=None) dict [source]
Parses a yaml files and loads it into a python dictionary
- It can deal with 4 types of tags:
!arg: converts the yaml node to
click.Argument
object!opt: converts the yaml node to
click.Option
object!obj: converts the yaml node to the specified class object
!env: replaces the environment variables with the associated values
- Parameters:
path (str | pathlib.Path | None, optional) – Defines the path to the yaml file that needs to be parsed, defaults to None
data (str | None, optional) – The yaml data itself as a stream , defaults to None
- Raises:
ValueError – Raises the error if neither a path or data is defined as input
- Returns:
The parsed yaml file
- Return type:
dict[str, dict]
clickyaml.commander module
- class clickyaml.commander.Commander(name: str, parsed_yaml: dict)[source]
Bases:
object
Commander class takes in a parsed yaml and creates commands out of it. The parsed_yaml needs to be a dictionary of commands, and the name parameter is used to fetch the required information for the command.
- property callback
Callback to be linked to the click Command.
If no callback is passed, the command is passed a default callback. The default callback runs the script associated with the command and passes the arguments in the order they are defined in the yaml file.
- Returns:
The callback linked to the click command
- Return type:
Callable
- property command
The click Command created out of the yaml. Uses the default callback or the callback assigned to the commander object.
- Returns:
Click command created from the yaml file.
- Return type:
class: click.Command
- name: str
Name of the command to create. It’s value is used to fetch the parameters out of the parsed yaml and is also used to create a command of the same name
- parsed_yaml: dict
Dictionary of commands, can include one or more commands.
- script: str = ''
Script associated with the command.
Module contents
Top-level package for clickYaml.