utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import unicode_literals
  2. from .base import DEFAULT_ATTRS, Attrs
  3. __all__ = (
  4. 'split_token_in_parts',
  5. 'merge_attrs',
  6. )
  7. def split_token_in_parts(token):
  8. """
  9. Take a Token, and turn it in a list of tokens, by splitting
  10. it on ':' (taking that as a separator.)
  11. """
  12. result = []
  13. current = []
  14. for part in token + (':', ):
  15. if part == ':':
  16. if current:
  17. result.append(tuple(current))
  18. current = []
  19. else:
  20. current.append(part)
  21. return result
  22. def merge_attrs(list_of_attrs):
  23. """
  24. Take a list of :class:`.Attrs` instances and merge them into one.
  25. Every `Attr` in the list can override the styling of the previous one.
  26. """
  27. result = DEFAULT_ATTRS
  28. for attr in list_of_attrs:
  29. result = Attrs(
  30. color=attr.color or result.color,
  31. bgcolor=attr.bgcolor or result.bgcolor,
  32. bold=attr.bold or result.bold,
  33. underline=attr.underline or result.underline,
  34. italic=attr.italic or result.italic,
  35. blink=attr.blink or result.blink,
  36. reverse=attr.reverse or result.reverse)
  37. return result