from __future__ import generators |
from __future__ import generators |
from peak.api import * |
from peak.api import * |
from interfaces import * |
from interfaces import * |
from peak.util.EigenData import EigenDict |
|
from peak.util.imports import whenImported |
from peak.util.imports import whenImported |
from protocols.advice import getMRO |
from protocols.advice import getMRO |
from types import ClassType |
from types import ClassType |
|
|
__all__ = [ |
__all__ = [ |
'EigenRegistry', 'MultiKey', 'UnionOf', 'ProviderOf', 'FactoryFor', |
'MultiKey', 'UnionOf', 'ProviderOf', 'FactoryFor', |
] |
] |
|
|
|
|
|
|
|
|
|
|
|
|
class Wrapper(object): |
class Wrapper(object): |
|
|
__slots__ = 'subject','protocol' |
__slots__ = 'subject','protocol' |
) |
) |
|
|
|
|
class EigenRegistry(EigenDict): |
class ImmutableConfig(object): |
|
|
"""EigenDict that takes IConfigKey objects as keys, handling inheritance""" |
def __init__(self, baseMaps=(), items=()): |
|
|
def __init__(self): |
self.depth = depths = {} |
self.depth = {} |
self.keysIndex = keysIndex = {} |
self.keysIndex = {} |
self.data = data = {} |
super(EigenRegistry,self).__init__() |
|
|
for base in baseMaps: |
|
adapt(base,ImmutableConfig) |
|
for key, depth in base.depth.items(): |
|
old = depths.get(key,depth) |
|
if old>=depth: |
|
data[key] = base.data[key] |
|
depths[key] = depth |
|
|
def lookup(self, configKey, failobj=None): |
for ns,keysMap in base.keysIndex.items(): |
sc = self._setCell |
keysIndex.setdefault(ns,{}).update(keysMap) |
for key in configKey.lookupKeys(): |
|
cell = sc(key) |
for configKey,value in items: |
if cell.exists(): |
|
return cell.get() |
|
else: |
|
return failobj |
|
|
|
def register(self, configKey, item, depth=0): |
|
"""Register 'item' under 'configKey'""" |
|
for key,depth in adapt(configKey,IConfigKey).registrationKeys(): |
for key,depth in adapt(configKey,IConfigKey).registrationKeys(): |
if self.depth.get(key,depth)>=depth: |
|
self[key]=item |
if depths.get(key,depth)>=depth: |
self.depth[key] = depth |
|
key = adapt(key, IConfigKey) |
ckey = adapt(key, IConfigKey) |
for k in key.parentKeys(): |
|
self.keysIndex.setdefault(k,{})[key] = True |
for k in ckey.parentKeys(): |
|
keysIndex.setdefault(k,{})[ckey] = True |
|
|
|
data[key]=value |
|
depths[key] = depth |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lookup(self, configKey, failObj=None): |
|
data = self.data |
|
for key in configKey.lookupKeys(): |
|
if key in data: |
|
return data[key] |
|
return failObj |
|
|
|
|
def _configKeysMatching(self, configKey): |
def _configKeysMatching(self, configKey): |
|
|
index = self.keysIndex |
index = self.keysIndex |
|
|
if not index: |
if not index: |
|
|
|
|
|
|
def update(self,other): |
|
"""Conservatively merge in another EigenRegistry""" |
|
|
|
if not isinstance(other,EigenRegistry): |
|
raise TypeError("Not an EigenRegistry", other) |
|
|
|
mydepth = self.depth |
|
get = mydepth.get |
|
sc = self._setCell |
|
for iface, depth in other.depth.items(): |
|
old = get(iface,depth) |
|
if old>=depth: |
|
sc(iface).set(other[iface]) |
|
mydepth[iface] = depth |
|
|
|
for k,v in other.keysIndex.items(): |
|
self.keysIndex.setdefault(k,{}).update(v) |
|
|
|
|
|
def setdefault(self,key,failobj=None): |
|
raise NotImplementedError |
|
|
|
def __delitem__(self,key): |
|
raise NotImplementedError |
|
|
|
def clear(self): |
|
raise NotImplementedError |
|
|
|
|
|
|
|