text.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import constants
  2. import modes
  3. import positions
  4. from packet import Packet
  5. class Text(object):
  6. """Class representing a TEXT file.
  7. This class is aliased as :class:`alphasign.Text` in :mod:`alphasign.__init__`.
  8. """
  9. def __init__(self, data=None, label=None, size=None,
  10. position=None, mode=None, priority=False):
  11. """
  12. :param data: initial string to insert into object
  13. :param label: file label (default: "A")
  14. :param size: amount of bytes to allocate for object on sign (default: 64)
  15. :param position: constant from :mod:`alphasign.positions`
  16. :param mode: constant from :mod:`alphasign.modes`
  17. :param priority: set this text to be displayed instead of
  18. all other TEXT files. Set to True with an empty message to
  19. clear a priority TEXT.
  20. """
  21. if data is None:
  22. data = ""
  23. if label is None:
  24. label = "A"
  25. if size is None:
  26. size = 64
  27. if len(data) > size:
  28. size = len(data)
  29. if size > 125:
  30. size = 125
  31. if size < 1:
  32. size = 1
  33. if position is None:
  34. position = positions.MIDDLE_LINE
  35. if mode is None:
  36. mode = modes.ROTATE
  37. self.label = label
  38. self.size = size
  39. self.data = data
  40. self.position = position
  41. self.mode = mode
  42. self.priority = priority
  43. def __str__(self):
  44. # [WRITE_TEXT][File Label][ESC][Display Position][Mode Code]
  45. # [Special Specifier][ASCII Message]
  46. if self.data:
  47. packet = Packet("%s%s%s%s%s%s" % (constants.WRITE_TEXT,
  48. (self.priority and "0" or self.label),
  49. constants.ESC,
  50. self.position,
  51. self.mode,
  52. self.data))
  53. else:
  54. packet = Packet("%s%s" % (constants.WRITE_TEXT,
  55. (self.priority and "0" or self.label)))
  56. return str(packet)
  57. def __repr__(self):
  58. return repr(self.__str__())