string.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import constants
  2. from packet import Packet
  3. class String(object):
  4. def __init__(self, data=None, label=None, size=None):
  5. if label is None:
  6. label = "1"
  7. if size is None:
  8. size = 32
  9. if len(data) > size:
  10. size = len(data)
  11. if size > 125:
  12. size = 125
  13. if size < 1:
  14. size = 1
  15. self.label = label
  16. self.size = size
  17. self.data = data
  18. def allocate(self):
  19. """Create a STRING.
  20. This is necessary to allocate memory for the STRING on the sign
  21. Args:
  22. label: label of the STRING to create
  23. size: size of the STRING to create, in bytes. 125 max.
  24. """
  25. size_hex = "%04x" % self.size
  26. packet = Packet("%s%s%s%s%s%s%s%s%s%s%s%s%s" %
  27. (constants.WRITE_SPECIAL, "\$",
  28. "A", # call label.. why does this matter?
  29. "A", # text file type
  30. "U", # this TEXT file is unlocked
  31. "0100", # text file size in hex
  32. "FF", # text file's start time (FF = always)
  33. "00", # text file's stop time
  34. self.label,
  35. "B", # string file type
  36. "L", # this string file is locked
  37. size_hex,
  38. "0000")) # padding
  39. return packet
  40. def call(self):
  41. """Call a STRING.
  42. This is for inserting a STRING file into a TEXT file.
  43. Returns:
  44. control code and specified string label
  45. """
  46. return "\x10%s" % self.label
  47. def __str__(self):
  48. return str(Packet("%s%s%s" % (constants.WRITE_STRING, self.label,
  49. self.data)))
  50. def __repr__(self):
  51. return repr(self.__str__())