Skip to content

Creating Commands

Simon Lambin edited this page Sep 25, 2021 · 1 revision

A command is a standalone script that can be called from an action query. It# is meant to be reused in different actions, like a function.

Structure

You can create your commands in silex_client/commands folder, or in a subfolder of it. Be careful if you change a command of place, you need to change the path in every actions that use the command you moved.

📦silex_client
 ┗ 📂commands
   ┣ 📂command_category
   ┃ ┗ 📜your_command.py
   ┗ 📜your_command.py

Implementation

A command is a class that must inherit from CommandBase.

from silex_client.action.command_base import CommandBase

Member variables

You can override two member variables:

  • parameters: a dict that represent the parameters the command takes in. The dict must correspond to the following schema:
"parameter_name": {
    "label": A pretty version of the name with spaces. If you don't provide this entry or set it to None, it will be guessed from the name,
    "value": The default value of the parameter. If you don't provide this entry or set it to None, the parameter will be set to mendatory for the user. Make sure the value is json serializable.
    "type": The type the value should have. You can use types from the typing module like Any or Union. This entry is mendatory, an error will be returned at the resolution of every actions that uses commands with invalid parameters
}
  • required_metadata: a list of context metadata that are required. This is to make sure your command is only executed in certain contexts. For example :
["user", "shot"]

Means that your command can only be executed in a context where the user is connected and currently working on a shot.

Member methods

You must override the method async call to implement the actual code that will run when an action is executed. The async call method takes 3 parameters:

  • upstream: Any: The returned value of the previous command that were executed
  • parameters: dict: A dict of "parameter_name": <parameter_value> for this command
  • action_query: ActionQuery: The action that called this command. You can access the websocket connection, the async event loop, and the action buffer from it.

Don't forget that the command is executed in an asyncio event loop. You can use await to stop the execution of the command and make it restart at any point.

📚 Doc

🔧 Introduction:

🚀 Development:

Clone this wiki locally