Make all command return the same DiscoInfo

darcs-hash:20070627160241-86b55-47ded2f196dcccdf85b175a93d36a0ea77ab3aac.gz
This commit is contained in:
David Rousselie
2007-06-27 18:02:41 +02:00
parent 18925a19c8
commit 147fcace09
3 changed files with 57 additions and 23 deletions

View File

@@ -42,10 +42,17 @@ class CommandManager(object):
self.commands = [] self.commands = []
self.command_re = re.compile("([^#]*#)?(.*)") self.command_re = re.compile("([^#]*#)?(.*)")
def get_short_command_name(self, command_name):
"""
Return short command name associated to given command name:
'http://jabber.org/protocol/admin#add-user' -> 'add-user'
"""
match = self.command_re.match(command_name)
return match.group(2)
def get_command_desc(self, command_name, lang_class): def get_command_desc(self, command_name, lang_class):
"""Return localized command description""" """Return localized command description"""
match = self.command_re.match(command_name) short_command_name = self.get_short_command_name(command_name)
short_command_name = match.group(2)
command_desc_attribut = "command_" + short_command_name command_desc_attribut = "command_" + short_command_name
if hasattr(lang_class, command_desc_attribut): if hasattr(lang_class, command_desc_attribut):
command_desc = getattr(lang_class, command_desc_attribut) command_desc = getattr(lang_class, command_desc_attribut)
@@ -63,18 +70,19 @@ class CommandManager(object):
lang_class)) lang_class))
return disco_items return disco_items
def get_command_info(self, disco_info, command, lang_class): def get_command_info(self, disco_info, command_name, lang_class):
"""Return command infos""" """Return command infos"""
get_command_info_method_name = "get_" + command + "_info" disco_info.add_feature(COMMAND_NS)
if hasattr(self, get_command_info_method_name): DiscoIdentity(disco_info,
return getattr(self, get_command_info_method_name)(disco_info, self.get_command_desc(command_name, lang_class),
lang_class) "automation",
else: "command-node")
return disco_info return disco_info
def apply_command_action(self, info_query, command, action): def apply_command_action(self, info_query, command_name, action):
"""Apply action on command""" """Apply action on command"""
action_command_method = action + "_" + command short_command_name = self.get_short_command_name(command_name)
action_command_method = action + "_" + short_command_name
if hasattr(self, action_command_method): if hasattr(self, action_command_method):
return getattr(self, action_command_method)(info_query) return getattr(self, action_command_method)(info_query)
else: else:
@@ -88,16 +96,42 @@ class JCLCommandManager(CommandManager):
def __init__(self, component=None, account_manager=None): def __init__(self, component=None, account_manager=None):
"""JCLCommandManager constructor""" """JCLCommandManager constructor"""
CommandManager.__init__(self, component, account_manager) CommandManager.__init__(self, component, account_manager)
self.commands.extend(["list"]) self.commands.extend(["list",
"http://jabber.org/protocol/admin#add-user",
def get_list_info(self, disco_info, lang_class): "http://jabber.org/protocol/admin#delete-user",
"""Return infos for 'list' command""" "http://jabber.org/protocol/admin#disable-user",
disco_info.add_feature(COMMAND_NS) "http://jabber.org/protocol/admin#reenable-user",
DiscoIdentity(disco_info, "http://jabber.org/protocol/admin#end-user-session",
self.get_command_desc("list", lang_class), "http://jabber.org/protocol/admin#get-user-password",
"automation", "http://jabber.org/protocol/admin#change-user-password",
"command-node") "http://jabber.org/protocol/admin#get-user-roster",
return disco_info "http://jabber.org/protocol/admin#get-user-lastlogin",
"http://jabber.org/protocol/admin#user-stats",
"http://jabber.org/protocol/admin#edit-blacklist",
"http://jabber.org/protocol/admin#add-to-blacklist-in",
"http://jabber.org/protocol/admin#add-to-blacklist-out",
"http://jabber.org/protocol/admin#edit-whitelist",
"http://jabber.org/protocol/admin#add-to-whitelist-in",
"http://jabber.org/protocol/admin#add-to-whitelist-out",
"http://jabber.org/protocol/admin#get-registered-users-num",
"http://jabber.org/protocol/admin#get-disabled-users-num",
"http://jabber.org/protocol/admin#get-online-users-num",
"http://jabber.org/protocol/admin#get-active-users-num",
"http://jabber.org/protocol/admin#get-idle-users-num",
"http://jabber.org/protocol/admin#get-registered-users-list",
"http://jabber.org/protocol/admin#get-disabled-users-list"
"http://jabber.org/protocol/admin#get-online-users",
"http://jabber.org/protocol/admin#get-active-users",
"http://jabber.org/protocol/admin#get-idle-users",
"http://jabber.org/protocol/admin#announce",
"http://jabber.org/protocol/admin#set-motd",
"http://jabber.org/protocol/admin#edit-motd",
"http://jabber.org/protocol/admin#delete-motd",
"http://jabber.org/protocol/admin#set-welcome",
"http://jabber.org/protocol/admin#delete-welcome",
"http://jabber.org/protocol/admin#edit-admin",
"http://jabber.org/protocol/admin#restart",
"http://jabber.org/protocol/admin#shutdown"])
def execute_list(self, info_query): def execute_list(self, info_query):
"""Execute command 'list'. List accounts""" """Execute command 'list'. List accounts"""

View File

@@ -3,7 +3,7 @@ __revision__ = ""
import unittest import unittest
from jcl.jabber.tests import component, feeder from jcl.jabber.tests import component, feeder, command
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()

View File

@@ -741,7 +741,7 @@ class JCLComponent_TestCase(unittest.TestCase):
to_jid="jcl.test.com") to_jid="jcl.test.com")
disco_items = self.comp.disco_get_items("http://jabber.org/protocol/commands", disco_items = self.comp.disco_get_items("http://jabber.org/protocol/commands",
info_query) info_query)
self.assertEquals(len(disco_items.get_items()), 1) self.assertEquals(len(disco_items.get_items()), 35)
item = disco_items.get_items()[0] item = disco_items.get_items()[0]
self.assertEquals(item.get_node(), "list") self.assertEquals(item.get_node(), "list")
self.assertEquals(item.get_name(), Lang.en.command_list) self.assertEquals(item.get_name(), Lang.en.command_list)