-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commands
Before you can use a command, it needs to be declared. Lets say you want to defined a command called MyCommand:
from core.Kcommand import Kcommand
class MyCommand(Kcommand):
myCommandHash = str(uuid.uuid4())
def __init__(self):
super(MyCommand, self).__init__(
"MyCommand", self.myCommandHash)
The most important part of this declaration is the hash. The hash is the basis on which commands are matched to subscriptions. Thus, if you use a command only inside your app, it is fine to just use a UUID, as shown here. If you want to share the command with other applications, it makes sense to define a fixed hash. The launcher e.g., uses "kapps-launcher-command-hash".
To receive commands, you need to subscribe to them. Lets say you want to make sure you always receive MyCommand throughout the lifetime of your app, called MyApp. Then you may subscribe in the register() method:
class MyApp(Kapp):
name = "MyApp"
def myCommandCallback(self, kcommand):
print("MyCommand received")
def register(appID, appPath, ctx):
app = MyApp(appID, appPath, ctx)
app.subscribe(MyCommand(), app.myCommandCallback)
return app
Subscriptions can be made anywhere in the app and only needs an instance of the command, as well as the callback method.
To publish the command, simply call the publish method anywhere in the app:
self.publish(MyCommand())
You may also use the command in the frontend, e.g., in your apps home screen:
def homeCallback(self, kcommand):
return HTTPResponse(content="<html><a href=" + MyCommand().toURL() + ">MyCommand</a></html>")
You can also add parameters to a command, e.g., in the Notify command:
self.publish(Notify().setParam("title", "Test").setParam(
"message", "TestApp started"))
Of course, this can also be used in an URL.
The given callback is called whenever a the command is published, or a link with the command URL is clicked.
kapps Documentation, license: MIT, website: https://github.com/PhilippMundhenk/kapps