configglue team mailing list archive
-
configglue team
-
Mailing list archive
-
Message #00003
[Merge] lp:~configglue/configglue/fix-inheritance into lp:configglue
Ricardo Kirkner has proposed merging lp:~configglue/configglue/fix-inheritance into lp:configglue.
Requested reviews:
Configglue developers (configglue)
To make schema definition easier, we use class attributes, so as to mimic a
declarative language. This has the consequence however that when a schema
modifies an attribute it inherited, it effectively modifies all other schema
instances that also inherited from its parent schema.
To avoid this, we replace all class attributes by instance attributes upon
instantiation.
--
https://code.launchpad.net/~configglue/configglue/fix-inheritance/+merge/31990
Your team Configglue developers is requested to review the proposed merge of lp:~configglue/configglue/fix-inheritance into lp:configglue.
=== modified file 'configglue/pyschema/schema.py'
--- configglue/pyschema/schema.py 2010-07-31 01:15:59 +0000
+++ configglue/pyschema/schema.py 2010-08-06 19:34:46 +0000
@@ -15,10 +15,14 @@
#
###############################################################################
+from copy import deepcopy
+
from configglue.pyschema import ConfigOption, ConfigSection, super_vars
from configglue.pyschema.options import LinesConfigOption, StringConfigOption
+_internal = object.__dict__.keys() + ['__module__']
+
class Schema(object):
"""A complete description of a system configuration.
@@ -36,6 +40,21 @@
'__main__' section, that allows configuration files to include other
configuration files.
"""
+
+ def __new__(cls):
+ instance = super(Schema, cls).__new__(cls)
+
+ # override class attributes with instance attributes to correctly
+ # handle schema inheritance
+ schema_attributes = filter(
+ lambda x: x not in _internal and
+ isinstance(getattr(cls, x), (ConfigSection, ConfigOption)),
+ super_vars(cls))
+ for attr in schema_attributes:
+ setattr(instance, attr, deepcopy(getattr(cls, attr)))
+
+ return instance
+
def __init__(self):
self.includes = LinesConfigOption(item=StringConfigOption())
self._sections = {}