string.py 1.0 KB

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