string.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import constants
  2. from packet import Packet
  3. class String(object):
  4. """Class representing a STRING file.
  5. :ivar data: string contained within object
  6. :ivar label: label of string object
  7. """
  8. def __init__(self, data=None, label=None, size=None):
  9. """
  10. :param data: initial string to insert into object
  11. :param label: file label (default: "1")
  12. :param size: maximum size of string data in bytes (default: 32)
  13. """
  14. if data is None:
  15. data = ""
  16. if label is None:
  17. label = "1"
  18. if size is None:
  19. size = 32
  20. if len(data) > size:
  21. size = len(data)
  22. if size > 125:
  23. size = 125
  24. if size < 1:
  25. size = 1
  26. self.label = label
  27. self.size = size
  28. self.data = data
  29. def call(self):
  30. """Call a STRING.
  31. This is for inserting a STRING file into a TEXT file.
  32. :returns: control code and specified string label
  33. :rtype: string
  34. """
  35. return "\x10%s" % self.label
  36. def __str__(self):
  37. return str(Packet("%s%s%s" % (constants.WRITE_STRING, self.label,
  38. self.data)))
  39. def __repr__(self):
  40. return repr(self.__str__())