Advertisement
Guest User

Untitled

a guest
May 29th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. class Properties(util.ComparableMixin):
  2.  
  3. """
  4. I represent a set of properties that can be interpolated into various
  5. strings in buildsteps.
  6.  
  7. @ivar properties: dictionary mapping property values to tuples
  8. (value, source), where source is a string identifying the source
  9. of the property.
  10.  
  11. Objects of this class can be read like a dictionary -- in this case,
  12. only the property value is returned.
  13.  
  14. As a special case, a property value of None is returned as an empty
  15. string when used as a mapping.
  16. """
  17.  
  18. compare_attrs = ('properties',)
  19. implements(IProperties)
  20.  
  21. def __init__(self, **kwargs):
  22. """
  23. @param kwargs: initial property values (for testing)
  24. """
  25. self.properties = {}
  26. # Track keys which are 'runtime', and should not be
  27. # persisted if a build is rebuilt
  28. self.runtime = set()
  29. self.build = None # will be set by the Build when starting
  30. if kwargs:
  31. self.update(kwargs, "TEST")
  32.  
  33. @classmethod
  34. def fromDict(cls, propDict):
  35. properties = cls()
  36. for name, (value, source) in propDict.iteritems():
  37. properties.setProperty(name, value, source)
  38. return properties
  39.  
  40. def __getstate__(self):
  41. d = self.__dict__.copy()
  42. d['build'] = None
  43. return d
  44.  
  45. def __setstate__(self, d):
  46. self.__dict__ = d
  47. if not hasattr(self, 'runtime'):
  48. self.runtime = set()
  49.  
  50. def __contains__(self, name):
  51. return name in self.properties
  52.  
  53. def __getitem__(self, name):
  54. """Just get the value for this property."""
  55. rv = self.properties[name][0]
  56. return rv
  57.  
  58. def __nonzero__(self):
  59. return not not self.properties
  60.  
  61. def getPropertySource(self, name):
  62. return self.properties[name][1]
  63.  
  64. def asList(self):
  65. """Return the properties as a sorted list of (name, value, source)"""
  66. l = sorted([(k, v[0], v[1]) for k, v in self.properties.iteritems()])
  67. return l
  68.  
  69. def asDict(self):
  70. """Return the properties as a simple key:value dictionary,
  71. properly unicoded"""
  72. return dict((k, (v, s)) for k, (v, s) in self.properties.iteritems())
  73.  
  74. def __repr__(self):
  75. return ('Properties(**' +
  76. repr(dict((k, v[0]) for k, v in self.properties.iteritems())) +
  77. ')')
  78.  
  79. def update(self, dict, source, runtime=False):
  80. """Update this object from a dictionary, with an explicit source specified."""
  81. for k, v in dict.items():
  82. self.setProperty(k, v, source, runtime=runtime)
  83.  
  84. def updateFromProperties(self, other):
  85. """Update this object based on another object; the other object's """
  86. self.properties.update(other.properties)
  87. self.runtime.update(other.runtime)
  88.  
  89. def updateFromPropertiesNoRuntime(self, other):
  90. """Update this object based on another object, but don't
  91. include properties that were marked as runtime."""
  92. for k, v in other.properties.iteritems():
  93. if k not in other.runtime:
  94. self.properties[k] = v
  95.  
  96. # IProperties methods
  97.  
  98. def getProperty(self, name, default=None):
  99. return self.properties.get(name, (default,))[0]
  100.  
  101. def hasProperty(self, name):
  102. return name in self.properties
  103.  
  104. has_key = hasProperty
  105.  
  106. def setProperty(self, name, value, source, runtime=False):
  107. name = util.ascii2unicode(name)
  108. json.dumps(value) # Let the exception propagate ...
  109. source = util.ascii2unicode(source)
  110.  
  111. self.properties[name] = (value, source)
  112. if runtime:
  113. self.runtime.add(name)
  114.  
  115. def getProperties(self):
  116. return self
  117.  
  118. def getBuild(self):
  119. return self.build
  120.  
  121. def render(self, value):
  122. renderable = IRenderable(value)
  123. return defer.maybeDeferred(renderable.getRenderingFor, self)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement