From a40643bf40ae2b981c52085aff10ba9ef898aa91 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 17 May 2014 19:05:28 +0200 Subject: [PATCH] factorize aum's Contact.get_text() and boobmsg formatter into ICapContact --- modules/aum/contact.py | 39 -------------------------- weboob/applications/boobmsg/boobmsg.py | 39 ++------------------------ weboob/capabilities/contact.py | 39 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 75 deletions(-) diff --git a/modules/aum/contact.py b/modules/aum/contact.py index fdc8b32a..c0aed71e 100644 --- a/modules/aum/contact.py +++ b/modules/aum/contact.py @@ -265,42 +265,3 @@ class Contact(_Contact): self.profile[section] = s self._aum_profile = profile - - def get_text(self): - def print_node(node, level=1): - result = u'' - if node.flags & node.SECTION: - result += u'\t' * level + node.label + '\n' - for sub in node.value.itervalues(): - result += print_node(sub, level + 1) - else: - if isinstance(node.value, (tuple, list)): - value = ', '.join(unicode(v) for v in node.value) - elif isinstance(node.value, float): - value = '%.2f' % node.value - else: - value = node.value - result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value) - return result - - result = u'Nickname: %s\n' % self.name - if self.status & Contact.STATUS_ONLINE: - s = 'online' - elif self.status & Contact.STATUS_OFFLINE: - s = 'offline' - elif self.status & Contact.STATUS_AWAY: - s = 'away' - else: - s = 'unknown' - result += u'Status: %s (%s)\n' % (s, self.status_msg) - result += u'URL: %s\n' % self.url - result += u'Photos:\n' - for name, photo in self.photos.iteritems(): - result += u'\t%s%s\n' % (photo, ' (hidden)' if photo.hidden else '') - result += u'\nProfile:\n' - for head in self.profile.itervalues(): - result += print_node(head) - result += u'Description:\n' - for s in self.summary.split('\n'): - result += u'\t%s\n' % s - return result diff --git a/weboob/applications/boobmsg/boobmsg.py b/weboob/applications/boobmsg/boobmsg.py index 0868f3f6..246e5546 100644 --- a/weboob/applications/boobmsg/boobmsg.py +++ b/weboob/applications/boobmsg/boobmsg.py @@ -29,7 +29,7 @@ from lxml import etree from weboob.core import CallErrors from weboob.capabilities.messages import ICapMessages, Message, Thread from weboob.capabilities.account import ICapAccount -from weboob.capabilities.contact import ICapContact, Contact +from weboob.capabilities.contact import ICapContact from weboob.tools.application.repl import ReplApplication, defaultcount from weboob.tools.application.formatters.iformatter import IFormatter from weboob.tools.misc import html2text @@ -228,41 +228,8 @@ class ProfileFormatter(IFormatter): def flush(self): pass - def print_node(self, node, level=1): - result = u'' - if node.flags & node.SECTION: - result += u'\t' * level + node.label + '\n' - for sub in node.value.itervalues(): - result += self.print_node(sub, level+1) - else: - if isinstance(node.value, (tuple,list)): - value = ', '.join(unicode(v) for v in node.value) - else: - value = node.value - result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value) - return result - - def format_obj(self, obj, alias): - result = u'Nickname: %s\n' % obj.name - if obj.status & Contact.STATUS_ONLINE: - s = 'online' - elif obj.status & Contact.STATUS_OFFLINE: - s = 'offline' - elif obj.status & Contact.STATUS_AWAY: - s = 'away' - else: - s = 'unknown' - result += u'Status: %s (%s)\n' % (s, obj.status_msg) - result += u'Photos:\n' - for name, photo in obj.photos.iteritems(): - result += u'\t%s%s\n' % (photo, ' (hidden)' if photo.hidden else '') - result += u'Profile:\n' - for head in obj.profile.itervalues(): - result += self.print_node(head) - result += u'Description:\n' - for s in obj.summary.split('\n'): - result += u'\t%s\n' % s - return result + def format_obj(self, obj, alias=None): + return obj.get_text() class Boobmsg(ReplApplication): diff --git a/weboob/capabilities/contact.py b/weboob/capabilities/contact.py index d057b491..d0c1aebd 100644 --- a/weboob/capabilities/contact.py +++ b/weboob/capabilities/contact.py @@ -108,6 +108,45 @@ class Contact(CapBaseObject): for key, value in kwargs.iteritems(): setattr(photo, key, value) + def get_text(self): + def print_node(node, level=1): + result = u'' + if node.flags & node.SECTION: + result += u'\t' * level + node.label + '\n' + for sub in node.value.itervalues(): + result += print_node(sub, level + 1) + else: + if isinstance(node.value, (tuple, list)): + value = ', '.join(unicode(v) for v in node.value) + elif isinstance(node.value, float): + value = '%.2f' % node.value + else: + value = node.value + result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value) + return result + + result = u'Nickname: %s\n' % self.name + if self.status & Contact.STATUS_ONLINE: + s = 'online' + elif self.status & Contact.STATUS_OFFLINE: + s = 'offline' + elif self.status & Contact.STATUS_AWAY: + s = 'away' + else: + s = 'unknown' + result += u'Status: %s (%s)\n' % (s, self.status_msg) + result += u'URL: %s\n' % self.url + result += u'Photos:\n' + for name, photo in self.photos.iteritems(): + result += u'\t%s%s\n' % (photo, ' (hidden)' if photo.hidden else '') + result += u'\nProfile:\n' + for head in self.profile.itervalues(): + result += print_node(head) + result += u'Description:\n' + for s in self.summary.split('\n'): + result += u'\t%s\n' % s + return result + class QueryError(UserError): """