PageRenderTime 84ms CodeModel.GetById 54ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgqt/htmlui.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 98 lines | 87 code | 5 blank | 6 comment | 1 complexity | 053edf044bbba56f43b9d98afbb31554 MD5 | raw file
 1# htmlui.py - mercurial.ui.ui class which emits HTML/Rich Text
 2#
 3# Copyright 2010 Steve Borho <steve@borho.org>
 4#
 5# This software may be used and distributed according to the terms of the
 6# GNU General Public License version 2, incorporated herein by reference.
 7
 8import os, time
 9
10from mercurial import ui
11from PyQt4 import QtCore
12from tortoisehg.hgqt import qtlib
13from tortoisehg.util import hglib
14
15BEGINTAG = '\033' + str(time.time())
16ENDTAG = '\032' + str(time.time())
17
18class htmlui(ui.ui):
19    def __init__(self, src=None):
20        super(htmlui, self).__init__(src)
21        self.setconfig('ui', 'interactive', 'off')
22        self.setconfig('progress', 'disable', 'True')
23        self.output, self.error = [], []
24        os.environ['TERM'] = 'dumb'
25
26    def write(self, *args, **opts):
27        label = opts.get('label', '')
28        if self._buffers:
29            self._buffers[-1].extend([(str(a), label) for a in args])
30        else:
31            self.output.extend(self.smartlabel(''.join(args), label))
32
33    def write_err(self, *args, **opts):
34        label = opts.get('label', 'ui.error')
35        self.error.extend(self.smartlabel(''.join(args), label))
36
37    def label(self, msg, label):
38        '''
39        Called by Mercurial to apply styling (formatting) to a piece of
40        text.  Our implementation wraps tags around the data so we can
41        find it later when it is passed to ui.write()
42        '''
43        return BEGINTAG + self.style(msg, label) + ENDTAG
44
45    def style(self, msg, label):
46        'Escape message for safe HTML, then apply specified style'
47        msg = QtCore.Qt.escape(msg)
48        msg = msg.replace('\n', '<br />')
49        style = qtlib.geteffect(label)
50        return '<span style="%s">%s</span>' % (style, msg)
51
52    def smartlabel(self, text, label):
53        '''
54        Escape and apply style, excluding any text between BEGINTAG and
55        ENDTAG.  That text has already been escaped and styled.
56        '''
57        parts = []
58        try:
59            while True:
60                b = text.index(BEGINTAG)
61                e = text.index(ENDTAG)
62                if e > b:
63                    if b:
64                        parts.append(self.style(text[:b], label))
65                    parts.append(text[b + len(BEGINTAG):e])
66                    text = text[e + len(ENDTAG):]
67                else:
68                    # invalid range, assume ENDTAG and BEGINTAG
69                    # are naturually occuring.  Style, append, and
70                    # consume up to the BEGINTAG and repeat.
71                    parts.append(self.style(text[:b], label))
72                    text = text[b:]
73        except ValueError:
74            pass
75        if text:
76            parts.append(self.style(text, label))
77        return parts
78
79    def popbuffer(self, labeled=False):
80        b = self._buffers.pop()
81        if labeled:
82            return ''.join(self.style(a, label) for a, label in b)
83        return ''.join(a for a, label in b)
84
85    def plain(self):
86        return True
87
88    def getdata(self):
89        d, e = ''.join(self.output), ''.join(self.error)
90        self.output, self.error = [], []
91        return d, e
92
93if __name__ == "__main__":
94    from mercurial import hg
95    u = htmlui()
96    repo = hg.repository(u)
97    repo.status()
98    print u.getdata()[0]