from peak.naming.names import toName, Syntax, CompoundName |
from peak.naming.names import toName, Syntax, CompoundName |
from peak.util.EigenData import EigenRegistry, EigenCell |
from peak.util.EigenData import EigenRegistry, EigenCell |
|
|
from Interface import Interface |
|
from peak.api import config, NOT_FOUND, NOT_GIVEN, exceptions |
from peak.api import config, NOT_FOUND, NOT_GIVEN, exceptions |
|
|
from peak.config.interfaces import IConfigKey |
from peak.config.interfaces import IConfigKey, IPropertyMap |
|
|
|
|
__all__ = [ |
__all__ = [ |
'Base', 'Component','AutoCreated','Provider','CachingProvider', |
'Base', 'Component','AutoCreated','Provider','CachingProvider', |
] |
] |
|
|
|
|
InterfaceClass = Interface.__class__ |
|
|
|
|
|
|
|
|
|
|
|
def Provider(callable): |
def Provider(callable): |
return lambda foundIn, forObj: callable(forObj) |
return lambda foundIn, configKey, forObj: callable(forObj) |
|
|
|
|
def CachingProvider(callable, weak=False): |
def CachingProvider(callable, weak=False): |
|
|
def provider(foundIn, forObj): |
def provider(foundIn, configKey, forObj): |
|
|
|
# get the owner of the property map |
|
foundIn = foundIn.getParentComponent() |
|
|
fid = id(foundIn) |
fid = id(foundIn) |
utility = provider.cache.get(fid) |
utility = provider.cache.get(fid) |
|
|
|
|
|
|
|
|
|
|
|
|
def getParentComponent(component): |
def getParentComponent(component): |
|
|
"""Return parent of 'component', or 'None' if root or non-component""" |
"""Return parent of 'component', or 'None' if root or non-component""" |
|
|
for component in iterParents(component): |
for component in iterParents(component): |
|
|
utility = component._getUtility(iface, forObj) |
utility = component._getConfigData(iface, forObj) |
|
|
if utility is not NOT_FOUND: |
if utility is not NOT_FOUND: |
yield utility |
yield utility |
will result in a 'config.PropertyNotFound' exception. |
will result in a 'config.PropertyNotFound' exception. |
""" |
""" |
|
|
|
propName = config.Property(propName) |
|
|
return Once(lambda s,d,a: config.getProperty(s,propName,default), |
return Once(lambda s,d,a: config.getProperty(s,propName,default), |
provides=provides, doc=doc |
provides=provides, doc=doc |
) |
) |
|
|
|
|
|
|
|
|
|
|
class Meta(meta.AssertInterfaces, meta.ActiveDescriptors): |
class Meta(meta.AssertInterfaces, meta.ActiveDescriptors): |
pass |
pass |
|
|
return self.__parentCell.get() |
return self.__parentCell.get() |
|
|
|
|
def _getUtility(self, iface, forObj): |
def _getConfigData(self, configKey, forObj): |
return NOT_FOUND |
return NOT_FOUND |
|
|
|
|
|
|
_componentName = Once(_componentName) |
_componentName = Once(_componentName) |
|
|
__instance_provides__ = New(EigenRegistry) |
def __instance_provides__(self,d,a): |
|
from peak.config.config_components import PropertyMap |
|
return PropertyMap(self) |
|
|
|
__instance_provides__ = Once(__instance_provides__, provides=IPropertyMap) |
|
|
__class_provides__ = EigenRegistry() |
__class_provides__ = EigenRegistry() |
|
|
|
|
def _getUtility(self, iface, forObj): |
def _getConfigData(self, configKey, forObj): |
|
|
provider = self.__instance_provides__.get(iface,NOT_FOUND) |
value = self.__instance_provides__.getValueFor(configKey, forObj) |
|
|
if provider is not NOT_FOUND: |
if value is not NOT_FOUND: |
return provider(self,forObj) |
return value |
|
|
attr = self.__class_provides__.get(iface,NOT_FOUND) |
attr = self.__class_provides__.get(configKey) |
|
|
if attr is not NOT_FOUND: |
if attr: |
return getattr(self, attr, NOT_FOUND) |
return getattr(self, attr, NOT_FOUND) |
|
|
return NOT_FOUND |
return NOT_FOUND |
|
|
|
|
def registerProvider(self, ifaces, provider): |
def registerProvider(self, ifaces, provider): |
self.__instance_provides__.register(ifaces, provider) |
self.__instance_provides__.registerProvider(ifaces, provider) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|