PageRenderTime 15ms CodeModel.GetById 8ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0
  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. import os, time
  8. from mercurial import ui
  9. from PyQt4 import QtCore
  10. from tortoisehg.hgqt import qtlib
  11. from tortoisehg.util import hglib
  12. BEGINTAG = '\033' + str(time.time())
  13. ENDTAG = '\032' + str(time.time())
  14. class htmlui(ui.ui):
  15. def __init__(self, src=None):
  16. super(htmlui, self).__init__(src)
  17. self.setconfig('ui', 'interactive', 'off')
  18. self.setconfig('progress', 'disable', 'True')
  19. self.output, self.error = [], []
  20. os.environ['TERM'] = 'dumb'
  21. def write(self, *args, **opts):
  22. label = opts.get('label', '')
  23. if self._buffers:
  24. self._buffers[-1].extend([(str(a), label) for a in args])
  25. else:
  26. self.output.extend(self.smartlabel(''.join(args), label))
  27. def write_err(self, *args, **opts):
  28. label = opts.get('label', 'ui.error')
  29. self.error.extend(self.smartlabel(''.join(args), label))
  30. def label(self, msg, label):
  31. '''
  32. Called by Mercurial to apply styling (formatting) to a piece of
  33. text. Our implementation wraps tags around the data so we can
  34. find it later when it is passed to ui.write()
  35. '''
  36. return BEGINTAG + self.style(msg, label) + ENDTAG
  37. def style(self, msg, label):
  38. 'Escape message for safe HTML, then apply specified style'
  39. msg = QtCore.Qt.escape(msg)
  40. msg = msg.replace('\n', '<br />')
  41. style = qtlib.geteffect(label)
  42. return '<span style="%s">%s</span>' % (style, msg)
  43. def smartlabel(self, text, label):
  44. '''
  45. Escape and apply style, excluding any text between BEGINTAG and
  46. ENDTAG. That text has already been escaped and styled.
  47. '''
  48. parts = []
  49. try:
  50. while True:
  51. b = text.index(BEGINTAG)
  52. e = text.index(ENDTAG)
  53. if e > b:
  54. if b:
  55. parts.append(self.style(text[:b], label))
  56. parts.append(text[b + len(BEGINTAG):e])
  57. text = text[e + len(ENDTAG):]
  58. else:
  59. # invalid range, assume ENDTAG and BEGINTAG
  60. # are naturually occuring. Style, append, and
  61. # consume up to the BEGINTAG and repeat.
  62. parts.append(self.style(text[:b], label))
  63. text = text[b:]
  64. except ValueError:
  65. pass
  66. if text:
  67. parts.append(self.style(text, label))
  68. return parts
  69. def popbuffer(self, labeled=False):
  70. b = self._buffers.pop()
  71. if labeled:
  72. return ''.join(self.style(a, label) for a, label in b)
  73. return ''.join(a for a, label in b)
  74. def plain(self):
  75. return True
  76. def getdata(self):
  77. d, e = ''.join(self.output), ''.join(self.error)
  78. self.output, self.error = [], []
  79. return d, e
  80. if __name__ == "__main__":
  81. from mercurial import hg
  82. u = htmlui()
  83. repo = hg.repository(u)
  84. repo.status()
  85. print u.getdata()[0]