You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_textattributes.py 8.9KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # -*- test-case-name: twisted.python.test.test_textattributes -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module provides some common functionality for the manipulation of
  6. formatting states.
  7. Defining the mechanism by which text containing character attributes is
  8. constructed begins by subclassing L{CharacterAttributesMixin}.
  9. Defining how a single formatting state is to be serialized begins by
  10. subclassing L{_FormattingStateMixin}.
  11. Serializing a formatting structure is done with L{flatten}.
  12. @see: L{twisted.conch.insults.helper._FormattingState}
  13. @see: L{twisted.conch.insults.text._CharacterAttributes}
  14. @see: L{twisted.words.protocols.irc._FormattingState}
  15. @see: L{twisted.words.protocols.irc._CharacterAttributes}
  16. """
  17. from __future__ import print_function
  18. from twisted.python.util import FancyEqMixin
  19. class _Attribute(FancyEqMixin, object):
  20. """
  21. A text attribute.
  22. Indexing a text attribute with a C{str} or another text attribute adds that
  23. object as a child, indexing with a C{list} or C{tuple} adds the elements as
  24. children; in either case C{self} is returned.
  25. @type children: C{list}
  26. @ivar children: Child attributes.
  27. """
  28. compareAttributes = ('children',)
  29. def __init__(self):
  30. self.children = []
  31. def __repr__(self):
  32. return '<%s %r>' % (type(self).__name__, vars(self))
  33. def __getitem__(self, item):
  34. assert isinstance(item, (list, tuple, _Attribute, str))
  35. if isinstance(item, (list, tuple)):
  36. self.children.extend(item)
  37. else:
  38. self.children.append(item)
  39. return self
  40. def serialize(self, write, attrs=None, attributeRenderer='toVT102'):
  41. """
  42. Serialize the text attribute and its children.
  43. @param write: C{callable}, taking one C{str} argument, called to output
  44. a single text attribute at a time.
  45. @param attrs: A formatting state instance used to determine how to
  46. serialize the attribute children.
  47. @type attributeRenderer: C{str}
  48. @param attributeRenderer: Name of the method on I{attrs} that should be
  49. called to render the attributes during serialization. Defaults to
  50. C{'toVT102'}.
  51. """
  52. if attrs is None:
  53. attrs = DefaultFormattingState()
  54. for ch in self.children:
  55. if isinstance(ch, _Attribute):
  56. ch.serialize(write, attrs.copy(), attributeRenderer)
  57. else:
  58. renderMeth = getattr(attrs, attributeRenderer)
  59. write(renderMeth())
  60. write(ch)
  61. class _NormalAttr(_Attribute):
  62. """
  63. A text attribute for normal text.
  64. """
  65. def serialize(self, write, attrs, attributeRenderer):
  66. attrs.__init__()
  67. _Attribute.serialize(self, write, attrs, attributeRenderer)
  68. class _OtherAttr(_Attribute):
  69. """
  70. A text attribute for text with formatting attributes.
  71. The unary minus operator returns the inverse of this attribute, where that
  72. makes sense.
  73. @type attrname: C{str}
  74. @ivar attrname: Text attribute name.
  75. @ivar attrvalue: Text attribute value.
  76. """
  77. compareAttributes = ('attrname', 'attrvalue', 'children')
  78. def __init__(self, attrname, attrvalue):
  79. _Attribute.__init__(self)
  80. self.attrname = attrname
  81. self.attrvalue = attrvalue
  82. def __neg__(self):
  83. result = _OtherAttr(self.attrname, not self.attrvalue)
  84. result.children.extend(self.children)
  85. return result
  86. def serialize(self, write, attrs, attributeRenderer):
  87. attrs = attrs._withAttribute(self.attrname, self.attrvalue)
  88. _Attribute.serialize(self, write, attrs, attributeRenderer)
  89. class _ColorAttr(_Attribute):
  90. """
  91. Generic color attribute.
  92. @param color: Color value.
  93. @param ground: Foreground or background attribute name.
  94. """
  95. compareAttributes = ('color', 'ground', 'children')
  96. def __init__(self, color, ground):
  97. _Attribute.__init__(self)
  98. self.color = color
  99. self.ground = ground
  100. def serialize(self, write, attrs, attributeRenderer):
  101. attrs = attrs._withAttribute(self.ground, self.color)
  102. _Attribute.serialize(self, write, attrs, attributeRenderer)
  103. class _ForegroundColorAttr(_ColorAttr):
  104. """
  105. Foreground color attribute.
  106. """
  107. def __init__(self, color):
  108. _ColorAttr.__init__(self, color, 'foreground')
  109. class _BackgroundColorAttr(_ColorAttr):
  110. """
  111. Background color attribute.
  112. """
  113. def __init__(self, color):
  114. _ColorAttr.__init__(self, color, 'background')
  115. class _ColorAttribute(object):
  116. """
  117. A color text attribute.
  118. Attribute access results in a color value lookup, by name, in
  119. I{_ColorAttribute.attrs}.
  120. @type ground: L{_ColorAttr}
  121. @param ground: Foreground or background color attribute to look color names
  122. up from.
  123. @param attrs: Mapping of color names to color values.
  124. @type attrs: Dict like object.
  125. """
  126. def __init__(self, ground, attrs):
  127. self.ground = ground
  128. self.attrs = attrs
  129. def __getattr__(self, name):
  130. try:
  131. return self.ground(self.attrs[name])
  132. except KeyError:
  133. raise AttributeError(name)
  134. class CharacterAttributesMixin(object):
  135. """
  136. Mixin for character attributes that implements a C{__getattr__} method
  137. returning a new C{_NormalAttr} instance when attempting to access
  138. a C{'normal'} attribute; otherwise a new C{_OtherAttr} instance is returned
  139. for names that appears in the C{'attrs'} attribute.
  140. """
  141. def __getattr__(self, name):
  142. if name == 'normal':
  143. return _NormalAttr()
  144. if name in self.attrs:
  145. return _OtherAttr(name, True)
  146. raise AttributeError(name)
  147. class DefaultFormattingState(FancyEqMixin, object):
  148. """
  149. A character attribute that does nothing, thus applying no attributes to
  150. text.
  151. """
  152. compareAttributes = ('_dummy',)
  153. _dummy = 0
  154. def copy(self):
  155. """
  156. Make a copy of this formatting state.
  157. @return: A formatting state instance.
  158. """
  159. return type(self)()
  160. def _withAttribute(self, name, value):
  161. """
  162. Add a character attribute to a copy of this formatting state.
  163. @param name: Attribute name to be added to formatting state.
  164. @param value: Attribute value.
  165. @return: A formatting state instance with the new attribute.
  166. """
  167. return self.copy()
  168. def toVT102(self):
  169. """
  170. Emit a VT102 control sequence that will set up all the attributes this
  171. formatting state has set.
  172. @return: A string containing VT102 control sequences that mimic this
  173. formatting state.
  174. """
  175. return ''
  176. class _FormattingStateMixin(DefaultFormattingState):
  177. """
  178. Mixin for the formatting state/attributes of a single character.
  179. """
  180. def copy(self):
  181. c = DefaultFormattingState.copy(self)
  182. c.__dict__.update(vars(self))
  183. return c
  184. def _withAttribute(self, name, value):
  185. if getattr(self, name) != value:
  186. attr = self.copy()
  187. attr._subtracting = not value
  188. setattr(attr, name, value)
  189. return attr
  190. else:
  191. return self.copy()
  192. def flatten(output, attrs, attributeRenderer='toVT102'):
  193. """
  194. Serialize a sequence of characters with attribute information
  195. The resulting string can be interpreted by compatible software so that the
  196. contained characters are displayed and, for those attributes which are
  197. supported by the software, the attributes expressed. The exact result of
  198. the serialization depends on the behavior of the method specified by
  199. I{attributeRenderer}.
  200. For example, if your terminal is VT102 compatible, you might run
  201. this for a colorful variation on the \"hello world\" theme::
  202. from twisted.conch.insults.text import flatten, attributes as A
  203. from twisted.conch.insults.helper import CharacterAttribute
  204. print(flatten(
  205. A.normal[A.bold[A.fg.red['He'], A.fg.green['ll'], A.fg.magenta['o'], ' ',
  206. A.fg.yellow['Wo'], A.fg.blue['rl'], A.fg.cyan['d!']]],
  207. CharacterAttribute()))
  208. @param output: Object returned by accessing attributes of the
  209. module-level attributes object.
  210. @param attrs: A formatting state instance used to determine how to
  211. serialize C{output}.
  212. @type attributeRenderer: C{str}
  213. @param attributeRenderer: Name of the method on I{attrs} that should be
  214. called to render the attributes during serialization. Defaults to
  215. C{'toVT102'}.
  216. @return: A string expressing the text and display attributes specified by
  217. L{output}.
  218. """
  219. flattened = []
  220. output.serialize(flattened.append, attrs, attributeRenderer)
  221. return ''.join(flattened)
  222. __all__ = [
  223. 'flatten', 'DefaultFormattingState', 'CharacterAttributesMixin']