Added property managment in @classdecorator. Unused :-( ...

This commit is contained in:
Jean-Paul Chaput 2020-10-14 15:06:42 +02:00
parent 79f3350eba
commit 51c6b90160
2 changed files with 35 additions and 5 deletions

View File

@ -124,6 +124,16 @@ class CfgCache ( object ):
# Effective setting of the Cfg parameter.
cache.apply()
If a cache parameter is assigned to ``None``, it triggers the
loading of the value from the disk, it it exists.
.. code-block:: python
# Setup of a CfgCache parameter.
cache = CfgCache('')
cache.katana.eventsLimit = None
# The parameter will read it's value from the disk (4000000).
This is done by overloading ``__setattr__()`` and ``__getattr__()``
which recursively create CfgCache objects for intermediate levels
@ -190,7 +200,6 @@ class CfgCache ( object ):
self._priority = priority
self._path = path
self._rattr = {}
return
def __setattr__ ( self, attr, v ):
"""
@ -232,7 +241,6 @@ class CfgCache ( object ):
if v is None:
v = CfgCache.getDefaultCfgParameter( self._path+'.'+attr )
self._rattr[ attr ] = v
return
def __getattr__ ( self, attr ):
"""
@ -244,6 +252,20 @@ class CfgCache ( object ):
self._rattr[attr] = CfgCache( path, self._priority )
return self._rattr[attr]
def _hasCachedParam ( self, elements ):
print( elements )
if not self._rattr.has_key(elements[0]):
return False
if len(elements) == 1:
return True
rattr = self._rattr[ elements[0] ]
if not isinstance(rattr,CfgCache):
return False
return rattr._hasCachedParam( elements[1:] )
def hasCachedParam ( self, attr ):
return self._hasCachedParam( attr.split('.') )
def apply ( self, priority=None ):
"""Apply the parameters values stored in the cache to the ``Cfg`` database."""
if priority is None: priority = self._priority

View File

@ -79,6 +79,8 @@ def classdecorator ( cls ):
be the case.
"""
def isprop ( attr ): return isinstance( attr, property )
def wrappedSetattr ( self, attr, v ):
if attr != '_baseClass' and self._baseClass.__dict__.has_key(attr):
self._baseClass.__setattr__( attr, v )
@ -87,10 +89,16 @@ def classdecorator ( cls ):
def wrappedGetattr ( self, attr ):
if attr == '_baseClass': return self.__dict__['_baseClass']
if self.__dict__.has_key(attr): return self.__dict__[attr]
if not hasattr(self._baseClass,attr):
raise AttributeError( '\'{}\' object has no attribute \'{}\'' \
selfClass = type( self )
if selfClass.__dict__.has_key(attr):
prop = selfClass.__dict__[attr]
if isprop(prop):
return prop.__get__(self)
return prop
if not hasattr(self,'_baseClass'):
raise AttributeError( '\'{}\' object has no attribute or method named \'{}\'' \
.format(self.__class__.__name__,attr) )
return getattr( self._baseClass, attr )
return wrappedGetattr( self._baseClass, attr )
classInit = cls.__init__