Debian Codegraph @

lib/html_log.py

raw symbols SCIP
1 # Copyright (C) 2009 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17
18 from html import escape
19 import codecs
20 from io import StringIO, BytesIO
21
22 from breezy import log
23
24
25 # def log_as_html(branch, rqst):
26 # """Get the log for a branch as HTML text."""
27 # sio = codecs.getwriter('utf-8')(StringIO())
28 # #sio.encoding = 'utf-8'
29 # try:
30 # lf = HtmlLogFormatter(sio)
31 # log.Logger(branch, rqst).show(lf)
32 # return sio.getvalue().decode('utf-8')
33 # finally:
34 # sio.close()
35
36
37 def log_as_html(branch, rqst):
38 """Get the log for a branch as HTML text."""
39 sio = codecs.getwriter('utf-8')(BytesIO())
40 #sio.encoding = 'utf-8'
41 try:
42 lf = HtmlLogFormatter(sio)
43 log.Logger(branch, rqst).show(lf)
44 return sio.getvalue().decode('utf-8')
45 finally:
46 sio.close()
47
48 class HtmlLogFormatter(log.LineLogFormatter):
49
50 def __init__(self, *args, **kwargs):
51 super(HtmlLogFormatter, self).__init__(*args, **kwargs)
52 self.show_markers = False
53
54 def begin_log(self):
55 self.to_file.write('<table>\n')
56 headers = ['Rev', 'Message', 'Date', 'Author']
57 if self.show_markers:
58 headers.append('Markers')
59 self.to_file.write(self._make_row(headers, header=True))
60
61 def end_log(self):
62 self.to_file.write("</table>\n")
63
64 def log_revision(self, revision):
65 rev = revision.rev
66 cells = []
67 if revision.revno:
68 # Is it worth doing anything smart with depth here?
69 revno_str = "%s" % revision.revno
70 else:
71 # don't show revno when it is None
72 revno_str = ""
73 cells.append(revno_str)
74 cells.append(rev.get_summary())
75 cells.append(self.date_string(rev))
76 cells.append(self.author_string(rev))
77 if self.show_markers:
78 markers = []
79 if len(rev.parent_ids) > 1:
80 markers.append('[merge]')
81 if revision.tags:
82 markers.append('{%s} ' % (', '.join(revision.tags)))
83 cells.append(" ".join(markers))
84 self.to_file.write(self._make_row(cells))
85
86 def author_string(self, rev):
87 return self.short_author(rev)
88
89 def _make_row(self, cells, header=False):
90 if header:
91 cells = ['<th align="left">%s</th>' % escape(cell)
92 for cell in cells]
93 else:
94 cells = ["<td>%s&nbsp;</td>" % escape(cell)
95 for cell in cells]
96 return "<tr>%s</tr>\n" % ''.join(cells)