app_model
#
Generic application schema implemented in python.
Modules:
-
backends–Adapters for using the app_model with various backends.
-
expressions–Abstraction on expressions, and contexts in which to evaluate them.
-
registries–App-model registries, such as menus, keybindings, commands.
-
types–App-model types.
Classes:
-
Action–An Action is a callable object with menu placement, keybindings, and metadata.
-
Application–Full application model.
Functions:
-
register_action–Register an action.
Action
#
Bases: CommandRule, Generic[P, R]
An Action is a callable object with menu placement, keybindings, and metadata.
This is the "complete" representation of a command. Including a pointer to the
actual callable object, as well as any additional menu and keybinding rules.
Most commands and menu items will be represented by Actions, and registered using
register_action.
Parameters:
-
callback(Callable[ParamSpec, R] | str) –A function to call when the associated command id is executed. If a string is provided, it must be a fully qualified name to a callable python object. This usually takes the form of
{obj.__module__}:{obj.__qualname__}(e.g.my_package.a_module:some_function) -
menus(list[MenuRule] | None, default:None) –(Optional) Menus to which this action should be added. Note that menu items in the sequence may be supplied as a plain string, which will be converted to a
MenuRulewith the string as theidfield. -
keybindings(list[KeyBindingRule] | None, default:None) –(Optional) Default keybinding(s) that will trigger this command.
-
palette(bool, default:True) –Whether to add this command to the global Command Palette during registration.
Application
#
Application(
name: str,
*,
raise_synchronous_exceptions: bool = False,
commands_reg_class: type[
CommandsRegistry
] = CommandsRegistry,
menus_reg_class: type[MenusRegistry] = MenusRegistry,
keybindings_reg_class: type[
KeyBindingsRegistry
] = KeyBindingsRegistry,
injection_store_class: type[Store] = ino.Store,
context: Context | MutableMapping | None = None,
)
Full application model.
This is the top level object that comprises all of the registries, and other app-namespace specific objects.
Parameters:
-
name(str) –A name for this application.
-
raise_synchronous_exceptions(bool, default:False) –Whether to raise exceptions that occur while executing commands synchronously, by default False. This is settable after instantiation, and can also be controlled per execution by calling
result.result()on the future object returned from theexecute_commandmethod. -
commands_reg_class(Type[CommandsRegistry], default:CommandsRegistry) –(Optionally) override the class to use when creating the CommandsRegistry
-
menus_reg_class(Type[MenusRegistry], default:MenusRegistry) –(Optionally) override the class to use when creating the MenusRegistry
-
keybindings_reg_class(Type[KeyBindingsRegistry], default:KeyBindingsRegistry) –(Optionally) override the class to use when creating the KeyBindingsRegistry
-
injection_store_class(Type[Store], default:Store) –(Optionally) override the class to use when creating the injection Store
-
context(Context | MutableMapping | None, default:None) –(Optionally) provide a context to use for this application. If a
MutableMappingis provided, it will be used to create aContextinstance. IfNone(the default), a newContextinstance will be created.
Attributes:
-
commands(CommandsRegistry) –The Commands Registry for this application.
-
menus(MenusRegistry) –The Menus Registry for this application.
-
keybindings(KeyBindingsRegistry) –The KeyBindings Registry for this application.
-
injection_store(Store) –The Injection Store for this application.
-
context(Context) –The Context for this application.
Methods:
-
destroy–Destroy the
Applicationnamedname. -
dispose–Dispose this
Application. -
get_app–Return app named
nameor None if it doesn't exist. -
get_or_create–Get app named
nameor create and return a new one if it doesn't exist. -
register_action–Register
Actioninstance with this application. -
register_actions–Register multiple
Actioninstances with this app.
Source code in src/app_model/_app.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
injection_store
property
#
injection_store: Store
Return the in_n_out.Store instance associated with this Application.
raise_synchronous_exceptions
property
writable
#
raise_synchronous_exceptions: bool
Whether to raise synchronous exceptions.
registered_actions
property
#
registered_actions: MappingProxyType[str, Action]
Return a Mapping of id->Action object for all registered actions.
Note that this only includes actions that were registered using
register_action. Commands registered directly via
Application.commands.register_action will not be included in this mapping.
destroy
classmethod
#
destroy(name: str) -> None
Destroy the Application named name.
This will call dispose(), destroy the
injection store, and remove the application from the list of stored
application names (allowing the name to be reused).
Source code in src/app_model/_app.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
dispose
#
dispose() -> None
Dispose this Application.
This calls all disposers functions (clearing all registries).
Source code in src/app_model/_app.py
202 203 204 205 206 207 208 209 | |
get_app
classmethod
#
get_app(name: str) -> Application | None
Return app named name or None if it doesn't exist.
Source code in src/app_model/_app.py
174 175 176 177 | |
get_or_create
classmethod
#
get_or_create(name: str) -> Application
Get app named name or create and return a new one if it doesn't exist.
Source code in src/app_model/_app.py
169 170 171 172 | |
register_action
#
register_action(action: Action) -> DisposeCallable
register_action(
action: str,
title: str,
*,
callback: Literal[None] = ...,
category: str | None = ...,
tooltip: str | None = ...,
icon: IconOrDict | None = ...,
enablement: Expr | None = ...,
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> CommandDecorator
register_action(
action: str,
title: str,
*,
callback: Callable[..., Any],
category: str | None = ...,
tooltip: str | None = ...,
icon: IconOrDict | None = ...,
enablement: Expr | None = ...,
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> DisposeCallable
register_action(
action: str | Action,
title: str | None = None,
*,
callback: Callable[..., Any] | None = None,
category: str | None = None,
tooltip: str | None = None,
icon: IconOrDict | None = None,
enablement: Expr | None = None,
menus: list[MenuRuleOrDict] | None = None,
keybindings: list[KeyBindingRuleOrDict] | None = None,
palette: bool = True,
) -> CommandDecorator | DisposeCallable
Register Action instance with this application.
An Action is the complete representation of a command,
including information about where and whether it appears in menus and optional
keybinding rules.
See register_action for complete
details on this function.
Source code in src/app_model/_app.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
register_actions
#
register_actions(
actions: Iterable[Action],
) -> DisposeCallable
Register multiple Action instances with this app.
Returns a function that may be called to undo the registration of actions.
Source code in src/app_model/_app.py
286 287 288 289 290 291 292 293 294 295 296 297 | |
register_action
#
register_action(
app: Application | str, id_or_action: Action
) -> DisposeCallable
register_action(
app: Application | str,
id_or_action: str,
title: str,
*,
callback: Literal[None] = ...,
category: str | None = ...,
tooltip: str | None = ...,
icon: IconOrDict | None = ...,
enablement: Expr | None = ...,
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> CommandDecorator
register_action(
app: Application | str,
id_or_action: str,
title: str,
*,
callback: Callable[..., Any],
category: str | None = ...,
tooltip: str | None = ...,
icon: IconOrDict | None = ...,
enablement: Expr | None = ...,
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> DisposeCallable
register_action(
app: Application | str,
id_or_action: str | Action,
title: str | None = None,
*,
callback: Callable[..., Any] | None = None,
category: str | None = None,
tooltip: str | None = None,
icon: IconOrDict | None = None,
enablement: Expr | None = None,
menus: list[MenuRuleOrDict] | None = None,
keybindings: list[KeyBindingRuleOrDict] | None = None,
palette: bool = True,
) -> CommandDecorator | DisposeCallable
Register an action.
This is a functional form of the
Application.register_action() method.
It accepts various overloads to allow for a more concise syntax. See examples
below.
An Action is the "complete" representation of a command. The command is the
function/callback itself, and an action also includes information about where and
whether it appears in menus and optional keybinding rules. Since, most of the
arguments to this function are simply passed through to the Action constructor,
see also docstrings for:
Parameters:
-
app(Application | str) –The app in which to register the action. If a string, the app is retrieved or created as necessary using
Application.get_or_create(app). -
id_or_action(str | Action) –Either a complete Action object or a string id of the command being registered. If an
Actionobject is provided, then all other arguments are ignored. -
title(str | None, default:None) –Title by which the command is represented in the UI. Required when
id_or_actionis a string. -
callback(CommandHandler | None, default:None) –Callable object that executes this command, by default None. If not provided, a decorator is returned that can be used to decorate a function that executes this action.
-
category(str | None, default:None) –Category string by which the command may be grouped in the UI, by default None
-
tooltip(str | None, default:None) –Tooltip to show when hovered., by default None
-
icon(Icon | None, default:None) –Iconused to represent this command, e.g. on buttons or in menus. by default None -
enablement(Expr | None, default:None) –Condition which must be true to enable the command in in the UI, by default None
-
menus(list[MenuRuleOrDict] | None, default:None) –List of
MenuRuleor kwargdictscontaining menu placements for this action, by default None -
keybindings(list[KeyBindingRuleOrDict] | None, default:None) –List of
KeyBindingRuleor kwargsdictscontaining default keybindings for this action, by default None -
palette(bool, default:True) –Whether to adds this command to the Command Palette, by default True
Returns:
-
CommandDecorator–If
callbackis not provided, then a decorator is returned that can be used to decorate a function as the executor of the command. -
DisposeCallable–If
callbackis provided, orid_or_actionis anActionobject, then a function is returned that may be used to unregister the action.
Raises:
-
ValueError–If
id_or_actionis a string andtitleis not provided. -
TypeError–If
id_or_actionis not a string or anActionobject.
Examples:
This function can be used directly or as a decorator, and accepts arguments in various forms.
Passing an existing Action object#
When the id_or_action argument is an instance of app_model.Action, then
all other arguments are ignored, the action object is registered directly, and the
return value is a function that may be used to unregister the action is returned.
from app_model import Application, Action, register_action
app = Application.get_or_create("myapp")
action = Action("my_action", title="My Action", callback=lambda: print("hi"))
register_action(app, action)
app.commands.execute_command("my_action") # prints "hi"
Creating a new Action#
When the id_or_action argument is a string, it is interpreted as the id
of the command being registered, in which case title must then also be provided.
All other arguments are optional, but may be used to customize the action being
created (with keybindings, menus, icons, etc).
register_action(
app,
"my_action2",
title="My Action2",
callback=lambda: print("hello again!"),
)
app.commands.execute_command("my_action2") # prints "hello again!"
Usage as a decorator#
If callback is not provided, then a decorator is returned that can be used
decorate a function as the executor of the command:
@register_action(app, "my_action3", title="My Action3")
def my_action3():
print("hello again, again!")
app.commands.execute_command("my_action3") # prints "hello again, again!"
Passing app as a string#
Note that in all of the above examples, the first app argument may be either an
instance of an Application object, or a string name of
an application. If a string is provided, then the application is retrieved or
created as necessary using
Application.get_or_create().
register_action(
"myapp", # app name instead of Application instance
"my_action4",
title="My Action4",
callback=lambda: print("hello again, again, again!"),
)
Source code in src/app_model/registries/_register.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |