summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2015-01-15 21:41:47 +0100
committermathieui <mathieui@mathieui.net>2015-01-15 21:45:57 +0100
commit889eea463dfccf719350480de58430df430b06b4 (patch)
treecb70b5c4c7794d91cf7b5439d3e5154d9ede40aa /src
parent45f280f56c93fd3504029c4022f39b097b634622 (diff)
downloadpoezio-889eea463dfccf719350480de58430df430b06b4.tar.gz
poezio-889eea463dfccf719350480de58430df430b06b4.tar.bz2
poezio-889eea463dfccf719350480de58430df430b06b4.tar.xz
poezio-889eea463dfccf719350480de58430df430b06b4.zip
Fix a fallback path in bookmarks.py and use a single-dispatch function
(if we gave parse_from_element an Element instead of a Conference object, it would crash)
Diffstat (limited to 'src')
-rw-r--r--src/bookmark.py36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/bookmark.py b/src/bookmark.py
index 7c519db2..afe38e5e 100644
--- a/src/bookmark.py
+++ b/src/bookmark.py
@@ -74,34 +74,38 @@ class Bookmark(object):
config.set_and_save('password', self.password, section=self.jid)
return local
+ @functools.singledispatch
@staticmethod
- def parse_from_stanza(el, method=None):
- jid = el['jid']
- autojoin = el['autojoin']
- password = el['password']
- nick = el['nick']
- name = el['name']
- return Bookmark(jid, name, autojoin, nick, password, method)
-
- @staticmethod
- def parse_from_element(el, method=None):
+ def parse(el, method=None):
"""
Generate a Bookmark object from a <conference/> element
+ (this is a fallback for raw XML Elements)
"""
- if isinstance(el, Conference):
- return Bookmark.parse_from_stanza(el, method)
jid = el.get('jid')
name = el.get('name')
autojoin = True if el.get('autojoin', 'false').lower() in ('true', '1') else False
nick = None
- for n in xml.iter(el, 'nick'):
+ for n in el.iter('nick'):
nick = n.text
password = None
- for p in xml.iter(el, 'password'):
+ for p in el.iter('password'):
password = p.text
return Bookmark(jid, name, autojoin, nick, password, method)
+ @staticmethod
+ @parse.register(Conference)
+ def parse_from_stanza(el, method=None):
+ """
+ Parse a Conference element into a Bookmark object
+ """
+ jid = el['jid']
+ autojoin = el['autojoin']
+ password = el['password']
+ nick = el['nick']
+ name = el['name']
+ return Bookmark(jid, name, autojoin, nick, password, method)
+
bookmarks = []
def get_by_jid(value):
@@ -178,7 +182,7 @@ def get_pep(xmpp, available_methods, callback):
for conf in iq['pubsub']['items']['item']['bookmarks']['conferences']:
if isinstance(conf, URL):
continue
- b = Bookmark.parse_from_element(conf, method='pep')
+ b = Bookmark.parse(conf, method='pep')
if not get_by_jid(b.jid):
bookmarks.append(b)
if callback:
@@ -195,7 +199,7 @@ def get_privatexml(xmpp, available_methods, callback):
else:
available_methods["privatexml"] = True
for conf in iq['private']['bookmarks']['conferences']:
- b = Bookmark.parse_from_element(conf, method='privatexml')
+ b = Bookmark.parse(conf, method='privatexml')
if not get_by_jid(b.jid):
bookmarks.append(b)
if callback: