Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Properties(util.ComparableMixin):
- """
- I represent a set of properties that can be interpolated into various
- strings in buildsteps.
- @ivar properties: dictionary mapping property values to tuples
- (value, source), where source is a string identifying the source
- of the property.
- Objects of this class can be read like a dictionary -- in this case,
- only the property value is returned.
- As a special case, a property value of None is returned as an empty
- string when used as a mapping.
- """
- compare_attrs = ('properties',)
- implements(IProperties)
- def __init__(self, **kwargs):
- """
- @param kwargs: initial property values (for testing)
- """
- self.properties = {}
- # Track keys which are 'runtime', and should not be
- # persisted if a build is rebuilt
- self.runtime = set()
- self.build = None # will be set by the Build when starting
- if kwargs:
- self.update(kwargs, "TEST")
- @classmethod
- def fromDict(cls, propDict):
- properties = cls()
- for name, (value, source) in propDict.iteritems():
- properties.setProperty(name, value, source)
- return properties
- def __getstate__(self):
- d = self.__dict__.copy()
- d['build'] = None
- return d
- def __setstate__(self, d):
- self.__dict__ = d
- if not hasattr(self, 'runtime'):
- self.runtime = set()
- def __contains__(self, name):
- return name in self.properties
- def __getitem__(self, name):
- """Just get the value for this property."""
- rv = self.properties[name][0]
- return rv
- def __nonzero__(self):
- return not not self.properties
- def getPropertySource(self, name):
- return self.properties[name][1]
- def asList(self):
- """Return the properties as a sorted list of (name, value, source)"""
- l = sorted([(k, v[0], v[1]) for k, v in self.properties.iteritems()])
- return l
- def asDict(self):
- """Return the properties as a simple key:value dictionary,
- properly unicoded"""
- return dict((k, (v, s)) for k, (v, s) in self.properties.iteritems())
- def __repr__(self):
- return ('Properties(**' +
- repr(dict((k, v[0]) for k, v in self.properties.iteritems())) +
- ')')
- def update(self, dict, source, runtime=False):
- """Update this object from a dictionary, with an explicit source specified."""
- for k, v in dict.items():
- self.setProperty(k, v, source, runtime=runtime)
- def updateFromProperties(self, other):
- """Update this object based on another object; the other object's """
- self.properties.update(other.properties)
- self.runtime.update(other.runtime)
- def updateFromPropertiesNoRuntime(self, other):
- """Update this object based on another object, but don't
- include properties that were marked as runtime."""
- for k, v in other.properties.iteritems():
- if k not in other.runtime:
- self.properties[k] = v
- # IProperties methods
- def getProperty(self, name, default=None):
- return self.properties.get(name, (default,))[0]
- def hasProperty(self, name):
- return name in self.properties
- has_key = hasProperty
- def setProperty(self, name, value, source, runtime=False):
- name = util.ascii2unicode(name)
- json.dumps(value) # Let the exception propagate ...
- source = util.ascii2unicode(source)
- self.properties[name] = (value, source)
- if runtime:
- self.runtime.add(name)
- def getProperties(self):
- return self
- def getBuild(self):
- return self.build
- def render(self, value):
- renderable = IRenderable(value)
- return defer.maybeDeferred(renderable.getRenderingFor, self)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement