SeqValue.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. .. deprecated:: 1.10.0
  3. The p3d packaging system has been replaced with the new setuptools-based
  4. system. See the :ref:`distribution` manual section.
  5. """
  6. __all__ = ["SeqValue"]
  7. class SeqValue:
  8. """ This represents a sequence value read from a contents.xml
  9. file, either from the <contents> or the <package> section. It's
  10. represented as series of dotted integers in the xml file, and
  11. stored internally as a tuple of integers.
  12. It may be incremented, which increments only the last integer in
  13. the series; or it may be compared with another SeqValue, which
  14. compares all of the integers componentwise. """
  15. def __init__(self, value = None):
  16. self.value = ()
  17. if value is not None:
  18. self.set(value)
  19. def set(self, value):
  20. """ Sets the seq from the indicated value of unspecified
  21. type. """
  22. if isinstance(value, tuple):
  23. self.setFromTuple(value)
  24. elif isinstance(value, str):
  25. self.setFromString(value)
  26. else:
  27. raise TypeError('Invalid sequence type: %s' % (value,))
  28. def setFromTuple(self, value):
  29. """ Sets the seq from the indicated tuple of integers. """
  30. assert isinstance(value, tuple)
  31. self.value = value
  32. def setFromString(self, value):
  33. """ Sets the seq from the indicated string of dot-separated
  34. integers. Raises ValueError on error. """
  35. assert isinstance(value, str)
  36. self.value = ()
  37. if value:
  38. value = value.split('.')
  39. value = map(int, value)
  40. self.value = tuple(value)
  41. def loadXml(self, xelement, attribute = 'seq'):
  42. """ Reads the seq from the indicated XML element. Returns
  43. true if loaded, false if not given or if there was an
  44. error. """
  45. self.value = ()
  46. value = xelement.Attribute(attribute)
  47. if value:
  48. try:
  49. self.setFromString(value)
  50. except ValueError:
  51. return False
  52. return True
  53. return False
  54. def storeXml(self, xelement, attribute = 'seq'):
  55. """ Adds the seq to the indicated XML element. """
  56. if self.value:
  57. value = '.'.join(map(str, self.value))
  58. xelement.SetAttribute(attribute, value)
  59. def __add__(self, inc):
  60. """ Increments the seq value, returning the new value. """
  61. if not self.value:
  62. value = (1,)
  63. else:
  64. value = self.value[:-1] + (self.value[-1] + inc,)
  65. return SeqValue(value)
  66. def __cmp__(self, other):
  67. """ Compares to another seq value. """
  68. return cmp(self.value, other.value)
  69. def __lt__(self, other):
  70. return self.value < other.value
  71. def __gt__(self, other):
  72. return self.value > other.value
  73. def __bool__(self):
  74. return bool(self.value)
  75. def __str__(self):
  76. return 'SeqValue%s' % (repr(self.value))