Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/xmpp/commands.py')
-rw-r--r--src/common/xmpp/commands.py64
1 files changed, 32 insertions, 32 deletions
diff --git a/src/common/xmpp/commands.py b/src/common/xmpp/commands.py
index 1eb105978..6feee02f0 100644
--- a/src/common/xmpp/commands.py
+++ b/src/common/xmpp/commands.py
@@ -19,13 +19,13 @@
There are 3 classes here, a command processor Commands like the Browser, and a command template plugin Command, and an example command.
To use this module:
-
+
Instansiate the module with the parent transport and disco browser manager as parameters.
'Plug in' commands using the command template.
The command feature must be added to existing disco replies where neccessary.
-
+
What it supplies:
-
+
Automatic command registration with the disco browser manager.
Automatic listing of commands in the public command list.
A means of handling requests, by redirection though the command manager.
@@ -39,9 +39,9 @@ DBG_COMMANDS = 'commands'
class Commands(PlugIn):
"""Commands is an ancestor of PlugIn and can be attached to any session.
-
- The commands class provides a lookup and browse mechnism. It follows the same priciple of the Browser class, for Service Discovery to provide the list of commands, it adds the 'list' disco type to your existing disco handler function.
-
+
+ The commands class provides a lookup and browse mechnism. It follows the same priciple of the Browser class, for Service Discovery to provide the list of commands, it adds the 'list' disco type to your existing disco handler function.
+
How it works:
The commands are added into the existing Browser on the correct nodes. When the command list is built the supplied discovery handler function needs to have a 'list' option in type. This then gets enumerated, all results returned as None are ignored.
The command executed is then called using it's Execute method. All session management is handled by the command itself.
@@ -53,7 +53,7 @@ class Commands(PlugIn):
self._exported_methods=[]
self._handlers={'':{}}
self._browser = browser
-
+
def plugin(self, owner):
"""Makes handlers within the session"""
# Plug into the session and the disco manager
@@ -61,15 +61,15 @@ class Commands(PlugIn):
owner.RegisterHandler('iq',self._CommandHandler,typ='set',ns=NS_COMMANDS)
owner.RegisterHandler('iq',self._CommandHandler,typ='get',ns=NS_COMMANDS)
self._browser.setDiscoHandler(self._DiscoHandler,node=NS_COMMANDS,jid='')
- owner.debug_flags.append(DBG_COMMANDS)
-
+ owner.debug_flags.append(DBG_COMMANDS)
+
def plugout(self):
"""Removes handlers from the session"""
# unPlug from the session and the disco manager
self._owner.UnregisterHandler('iq',self._CommandHandler,ns=NS_COMMANDS)
for jid in self._handlers:
self._browser.delDiscoHandler(self._DiscoHandler,node=NS_COMMANDS,jid=jid)
-
+
def _CommandHandler(self,conn,request):
"""The internal method to process the routing of command execution requests"""
# This is the command handler itself.
@@ -93,7 +93,7 @@ class Commands(PlugIn):
else:
conn.send(Error(request,ERR_ITEM_NOT_FOUND))
raise NodeProcessed
-
+
def _DiscoHandler(self,conn,request,typ):
"""The internal method to process service discovery requests"""
# This is the disco manager handler.
@@ -130,7 +130,7 @@ class Commands(PlugIn):
raise NodeProcessed
elif typ == 'info':
return {'ids':[{'category':'automation','type':'command-list'}],'features':[]}
-
+
def addCommand(self,name,cmddisco,cmdexecute,jid=''):
"""The method to call if adding a new command to the session, the requred parameters of cmddisco and cmdexecute are the methods to enable that command to be executed"""
# This command takes a command object and the name of the command for registration
@@ -146,7 +146,7 @@ class Commands(PlugIn):
self._handlers[jid][name]={'disco':cmddisco,'execute':cmdexecute}
# Need to add disco stuff here
self._browser.setDiscoHandler(cmddisco,node=name,jid=jid)
-
+
def delCommand(self,name,jid=''):
"""Removed command from the session"""
# This command takes a command object and the name used for registration
@@ -162,7 +162,7 @@ class Commands(PlugIn):
command = self.getCommand(name,jid)['disco']
del self._handlers[jid][name]
self._browser.delDiscoHandler(command,node=name,jid=jid)
-
+
def getCommand(self,name,jid=''):
"""Returns the command tuple"""
# This gets the command object with name
@@ -174,19 +174,19 @@ class Commands(PlugIn):
raise NameError,'Command not found'
else:
return self._handlers[jid][name]
-
+
class Command_Handler_Prototype(PlugIn):
- """This is a prototype command handler, as each command uses a disco method
- and execute method you can implement it any way you like, however this is
- my first attempt at making a generic handler that you can hang process
+ """This is a prototype command handler, as each command uses a disco method
+ and execute method you can implement it any way you like, however this is
+ my first attempt at making a generic handler that you can hang process
stages on too. There is an example command below.
-
+
The parameters are as follows:
name : the name of the command within the jabber environment
description : the natural language description
discofeatures : the features supported by the command
initial : the initial command in the from of {'execute':commandname}
-
+
All stages set the 'actions' dictionary for each session to represent the possible options available.
"""
name = 'examplecommand'
@@ -203,14 +203,14 @@ class Command_Handler_Prototype(PlugIn):
# Disco information for command list pre-formatted as a tuple
self.discoinfo = {'ids':[{'category':'automation','type':'command-node','name':self.description}],'features': self.discofeatures}
self._jid = jid
-
+
def plugin(self,owner):
"""Plug command into the commands class"""
# The owner in this instance is the Command Processor
self._commands = owner
self._owner = owner._owner
self._commands.addCommand(self.name,self._DiscoHandler,self.Execute,jid=self._jid)
-
+
def plugout(self):
"""Remove command from the commands class"""
self._commands.delCommand(name,self._jid)
@@ -219,7 +219,7 @@ class Command_Handler_Prototype(PlugIn):
"""Returns an id for the command session"""
self.count = self.count+1
return 'cmd-%s-%d'%(self.name,self.count)
-
+
def Execute(self,conn,request):
"""The method that handles all the commands, and routes them to the correct method for that stage."""
# New request or old?
@@ -254,7 +254,7 @@ class Command_Handler_Prototype(PlugIn):
else:
# New session
self.initial[action](conn,request)
-
+
def _DiscoHandler(self,conn,request,type_):
"""The handler for discovery events"""
if type_ == 'list':
@@ -263,9 +263,9 @@ class Command_Handler_Prototype(PlugIn):
return []
elif type_ == 'info':
return self.discoinfo
-
+
class TestCommand(Command_Handler_Prototype):
- """ Example class. You should read source if you wish to understate how it works.
+ """ Example class. You should read source if you wish to understate how it works.
Generally, it presents a "master" that giudes user through to calculate something.
"""
name = 'testcommand'
@@ -275,7 +275,7 @@ class TestCommand(Command_Handler_Prototype):
Command_Handler_Prototype.__init__(self,jid)
self.pi = 3.14
self.initial = {'execute':self.cmdFirstStage}
-
+
def cmdFirstStage(self,conn,request):
""" Determine """
# This is the only place this should be repeated as all other stages should have SessionIDs
@@ -300,7 +300,7 @@ class TestCommand(Command_Handler_Prototype):
sessions[request.getTagAttr('command','sessionid')]['actions']={'cancel':self.cmdCancel,None:self.cmdThirdStage,'previous':cmdFirstStage}
# The form generation is split out to another method as it may be called by cmdThirdStage
self.cmdSecondStageReply(conn,request)
-
+
def cmdSecondStageReply(self,conn,request):
reply = request.buildReply('result')
form = DataForm(title = 'Enter the radius', data=['Enter the radius of the circle (numbers only)',DataField(label='Radius',name='radius',typ='text-single')])
@@ -308,7 +308,7 @@ class TestCommand(Command_Handler_Prototype):
reply.addChild(name='command',attrs={'xmlns':NS_COMMAND,'node':request.getTagAttr('command','node'),'sessionid':request.getTagAttr('command','sessionid'),'status':'executing'},payload=replypayload)
self._owner.send(reply)
raise NodeProcessed
-
+
def cmdThirdStage(self,conn,request):
form = DataForm(node = result.getTag(name='command').getTag(name='x',namespace=NS_DATA))
try:
@@ -324,13 +324,13 @@ class TestCommand(Command_Handler_Prototype):
reply.addChild(name='command',attrs={'xmlns':NS_COMMAND,'node':request.getTagAttr('command','node'),'sessionid':request.getTagAttr('command','sessionid'),'status':'completed'},payload=form)
self._owner.send(reply)
raise NodeProcessed
-
+
def cmdCancel(self,conn,request):
reply = request.buildReply('result')
reply.addChild(name='command',attrs={'xmlns':NS_COMMAND,'node':request.getTagAttr('command','node'),'sessionid':request.getTagAttr('command','sessionid'),'status':'cancelled'})
self._owner.send(reply)
del self.sessions[request.getTagAttr('command','sessionid')]
-
-
+
+
# vim: se ts=3: