From 51c6b90160a6dccc664b7db0d35320a3ae6efebf Mon Sep 17 00:00:00 2001 From: Jean-Paul Chaput Date: Wed, 14 Oct 2020 15:06:42 +0200 Subject: [PATCH] Added property managment in @classdecorator. Unused :-( ... --- crlcore/python/helpers/overlay.py | 26 ++++++++++++++++++++++++-- crlcore/python/helpers/utils.py | 14 +++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crlcore/python/helpers/overlay.py b/crlcore/python/helpers/overlay.py index ea87a8e5..8488ab97 100644 --- a/crlcore/python/helpers/overlay.py +++ b/crlcore/python/helpers/overlay.py @@ -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 diff --git a/crlcore/python/helpers/utils.py b/crlcore/python/helpers/utils.py index 80f09d74..ac1f48c3 100644 --- a/crlcore/python/helpers/utils.py +++ b/crlcore/python/helpers/utils.py @@ -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__