Add _params to Wrapper.

This way subclasses don't have to override __init__ and wrap_dict to
share secondary data across instances.
This commit is contained in:
Steven Robertson 2012-04-22 15:37:37 -07:00
parent ebd9e5f205
commit 83e8eb465a
2 changed files with 13 additions and 11 deletions

View File

@ -27,12 +27,7 @@ class PackerWrapper(Wrapper):
mechanism, so you can easily nest objects by saying, for instance, mechanism, so you can easily nest objects by saying, for instance,
{{pcp.camera.rotation}} from within templated code. {{pcp.camera.rotation}} from within templated code.
""" """
def __init__(self, packer, val, spec=None, path=()): packer = property(lambda s: s._params['packer'])
super(PackerWrapper, self).__init__(val, spec)
self.packer, self.path = packer, path
def wrap_dict(self, path, spec, val):
return type(self)(self.packer, val or {}, spec, path)
def wrap_spline(self, path, spec, val): def wrap_spline(self, path, spec, val):
return PackerSpline(self.packer, path, spec) return PackerSpline(self.packer, path, spec)
@ -45,7 +40,8 @@ class PackerWrapper(Wrapper):
def _precalc(self): def _precalc(self):
"""Create a GenomePackerPrecalc object. See that class for details.""" """Create a GenomePackerPrecalc object. See that class for details."""
return PrecalcWrapper(self.packer, self._val, self.spec, self.path) return PrecalcWrapper(self._val, self.spec, self.path,
packer=self.packer)
class PackerSpline(object): class PackerSpline(object):
def __init__(self, packer, path, spec): def __init__(self, packer, path, spec):
@ -151,7 +147,7 @@ class GenomePacker(object):
def view(self, val={}): def view(self, val={}):
"""Create a DataPacker view. See DataPackerView class for details.""" """Create a DataPacker view. See DataPackerView class for details."""
return PackerWrapper(self, val, self.spec) return PackerWrapper(val, self.spec, packer=self)
def _require(self, spec, path): def _require(self, spec, path):
""" """

View File

@ -10,12 +10,17 @@ class Wrapper(object):
returned representation of the underlying genome according to the provided returned representation of the underlying genome according to the provided
spec without imposing flow control. spec without imposing flow control.
""" """
def __init__(self, val, spec=None, path=()): def __init__(self, val, spec=None, path=(), **params):
"""
``**params`` are passed to any instances of this class constructed
during traversal. It's not used in this class but may be used by
deriving classes.
"""
if spec is None: if spec is None:
assert val.get('type') in toplevels, 'Unrecognized dict type' assert val.get('type') in toplevels, 'Unrecognized dict type'
spec = toplevels[val['type']] spec = toplevels[val['type']]
# plain 'val' would conflict with some variation property names # plain 'val' would conflict with some variation property names
self._val, self.spec, self.path = val, spec, path self._val, self.spec, self.path, self._params = val, spec, path, params
def wrap(self, name, spec, val): def wrap(self, name, spec, val):
path = self.path + (name,) path = self.path + (name,)
@ -48,8 +53,9 @@ class Wrapper(object):
return val if val is not None else spec.default return val if val is not None else spec.default
def wrap_dict(self, path, spec, val): def wrap_dict(self, path, spec, val):
return type(self)(val or {}, spec, path) return type(self)(val or {}, spec, path, **self._params)
# I switched to uppercase for containers. I Don't Know Why.
def wrap_Map(self, path, spec, val): def wrap_Map(self, path, spec, val):
return self.wrap_dict(path, spec, val) return self.wrap_dict(path, spec, val)