From 889eea463dfccf719350480de58430df430b06b4 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 15 Jan 2015 21:41:47 +0100 Subject: [PATCH] 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) --- src/bookmark.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/bookmark.py b/src/bookmark.py index 7c519db2..afe38e5e 100644 --- a/src/bookmark.py +++ b/src/bookmark.py @@ -74,8 +74,31 @@ class Bookmark(object): config.set_and_save('password', self.password, section=self.jid) return local + @functools.singledispatch @staticmethod + def parse(el, method=None): + """ + Generate a Bookmark object from a element + (this is a fallback for raw XML Elements) + """ + 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 el.iter('nick'): + nick = n.text + password = None + 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'] @@ -83,25 +106,6 @@ class Bookmark(object): name = el['name'] return Bookmark(jid, name, autojoin, nick, password, method) - @staticmethod - def parse_from_element(el, method=None): - """ - Generate a Bookmark object from a element - """ - 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'): - nick = n.text - password = None - for p in xml.iter(el, 'password'): - password = p.text - - 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: