support colors in issues formatter

This commit is contained in:
Romain Bignon 2014-01-21 21:06:05 +01:00
commit 1a0dc93388

View file

@ -21,6 +21,7 @@
from datetime import timedelta from datetime import timedelta
import sys import sys
from weboob.capabilities.base import empty, CapBaseObject
from weboob.capabilities.bugtracker import ICapBugTracker, Query, Update, Project, Issue from weboob.capabilities.bugtracker import ICapBugTracker, Query, Update, Project, Issue
from weboob.tools.application.repl import ReplApplication, defaultcount from weboob.tools.application.repl import ReplApplication, defaultcount
from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter
@ -33,30 +34,52 @@ __all__ = ['BoobTracker']
class IssueFormatter(IFormatter): class IssueFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'project', 'title', 'body', 'author') MANDATORY_FIELDS = ('id', 'project', 'title', 'body', 'author')
def format_attr(self, obj, attr):
if not hasattr(obj, attr) or empty(getattr(obj, attr)):
return u''
value = getattr(obj, attr)
if isinstance(value, CapBaseObject):
value = value.name
return self.format_key(attr.capitalize(), value)
def format_key(self, key, value):
return '%s %s\n' % (self.colored('%s:' % key, 'green'),
value)
def format_obj(self, obj, alias): def format_obj(self, obj, alias):
result = u'%s%s - #%s - %s%s\n' % (self.BOLD, obj.project.name, obj.fullid, obj.title, self.NC) result = u'%s %s %s %s %s\n' % (self.colored(obj.project.name, 'blue', 'bold'),
self.colored(u'', 'cyan', 'bold'),
self.colored(obj.fullid, 'red', 'bold'),
self.colored(u'', 'cyan', 'bold'),
self.colored(obj.title, 'yellow', 'bold'))
result += '\n%s\n\n' % obj.body result += '\n%s\n\n' % obj.body
result += 'Author: %s (%s)\n' % (obj.author.name, obj.creation) result += self.format_key('Author', '%s (%s)' % (obj.author.name, obj.creation))
if hasattr(obj, 'status') and obj.status: result += self.format_attr(obj, 'status')
result += 'Status: %s\n' % obj.status.name result += self.format_attr(obj, 'version')
if hasattr(obj, 'version') and obj.version: result += self.format_attr(obj, 'category')
result += 'Version: %s\n' % obj.version.name result += self.format_attr(obj, 'assignee')
if hasattr(obj, 'category') and obj.category: if hasattr(obj, 'fields') and not empty(obj.fields):
result += 'Category: %s\n' % obj.category for key, value in obj.fields.iteritems():
if hasattr(obj, 'assignee') and obj.assignee: result += self.format_key(key.capitalize(), value)
result += 'Assignee: %s\n' % (obj.assignee.name)
if hasattr(obj, 'attachments') and obj.attachments: if hasattr(obj, 'attachments') and obj.attachments:
result += '\nAttachments:\n' result += '\n%s\n' % self.colored('Attachments:', 'green')
for a in obj.attachments: for a in obj.attachments:
result += '* %s%s%s <%s>\n' % (self.BOLD, a.filename, self.NC, a.url) result += '* %s%s%s <%s>\n' % (self.BOLD, a.filename, self.NC, a.url)
if hasattr(obj, 'history') and obj.history: if hasattr(obj, 'history') and obj.history:
result += '\nHistory:\n' result += '\n%s\n' % self.colored('History:', 'green')
for u in obj.history: for u in obj.history:
result += '* %s%s - %s%s\n' % (self.BOLD, u.date, u.author.name, self.NC) result += '%s %s %s %s\n' % (self.colored('*', 'red', 'bold'),
self.colored(u.date, 'yellow', 'bold'),
self.colored(u'', 'cyan', 'bold'),
self.colored(u.author.name, 'blue', 'bold'))
for change in u.changes: for change in u.changes:
result += ' - %s%s%s: %s -> %s\n' % (self.BOLD, change.field, self.NC, change.last, change.new) result += ' - %s %s %s %s\n' % (self.colored(change.field, 'green'),
change.last,
self.colored('->', 'magenta'), change.new)
if u.message: if u.message:
result += html2text(u.message) result += ' %s\n' % html2text(u.message).strip().replace('\n', '\n ')
return result return result