1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
from ComputedAttribute import ComputedAttribute
from Proxy import ProxyManager
from Products.PlugIns import defaultConstructors, PlugInContainer
from Providers import Provider, NullProvider
from Globals import default__class_init__
from string import split,strip
from Acquisition import aq_base
from Expressions import *
from ZClasses.Method import MWp
from OFS.Folder import Folder
import sys
from zLOG import LOG, WARNING
NOT_FOUND = []
eaKey = 'ZPatterns.AttributeProviders', 'ExternalAttributes'
class AttributeProvider(Provider):
"""
AttributeProvider example class. This attribute provider assumes that
the object containing the attributes is persistent and can save
whatever is desired. All of its methods would need to be redefined
for providers that implement fixed-schema and/or non-persistence-based
attributes.
"""
__plugin_kind__ = 'Attribute Provider'
def namesForRegistration(self,container):
"""
Return a tuple (readnames,writenames) where each is a sequence of attribute
names which this attribute provider supports reading and writing, respectively.
'*' may be included in either or both sequences, which means the provider will
be called if a more specific provider could not be found for a given attribute.
This is so that attribute providers can potentially create attributes
dynamically based on the requested name.
"""
return {
'provides':('attributes',),
'setattr': self.Attributes, 'delattr': self.Attributes
}
def _AttributeFor(self,client,name,default=None):
"""
Since persistent attributes are stored in the object, if we get
here, then we are trying to get something that doesn't exist!
"""
return default
def _SetAttributeFor(self,client,name,value):
"""Set the attribute and return true if successful"""
client.__dict__[name]=value; client._p_changed = 1
return 1
def _DelAttributeFor(self,client,name):
"""Delete the attribute and return true if successful"""
if client._v_changedAttrs_[name] is NOT_FOUND: del client._v_changedAttrs_[name]
return 1 # Nothing else to do, since DataSkin has already deleted persistent attr
# Class Metadata
meta_type='Persistent Internal Attribute Provider'
Attributes = ('*',)
_properties=(
{'id':'title', 'type': 'string', 'mode': 'w'},
{'id':'Attributes', 'type': 'tokens', 'mode': 'w'},
)
class ExternalAttributeProvider(AttributeProvider):
"""
This attribute provider stores persistent attributes outside the object
itself, in its persistent 'slot'. This allows persistent attributes to
be used even with non-persistent DataSkins (e.g. those which are only
virtually stored in a Rack.
"""
def _AttributeFor(self,client,name,default=None):
return client._v_readableSlot.get(eaKey,{}).get(name,default)
def _SetAttributeFor(self,client,name,value):
"""Set the attribute and return true if successful"""
attrs = client._v_readableSlot.get(eaKey,{}); attrs[name]=value
client._v_writeableSlot[eaKey] = attrs
return 1
def _DelAttributeFor(self,client,name):
"""Delete the attribute and return true if successful"""
if client._v_changedAttrs_[name] is NOT_FOUND: del client._v_changedAttrs_[name]
attrs = client._v_readableSlot.get(eaKey,{})
if attrs.has_key(name):
del attrs[name]
client._v_writeableSlot[eaKey] = attrs
return 1
# Class Metadata
meta_type='Persistent External Attribute Provider'
def namesForRegistration(self,container):
return {
'provides':('attributes',), 'getattr': self.Attributes,
'setattr': self.Attributes, 'delattr': self.Attributes
}
class ClassExtender(NullProvider,PlugInContainer):
"""Thing that provides its contents as attributes"""
__plugin_kind__ = 'Attribute Provider'
__plugin_groups__ = ()
meta_type='DataSkin Class Extender'
manage_options_right = tuple(
Folder.manage_options[1:-3]+(
{'label':'Security','action':'manage_access',
'help':('OFSP','Security.stx'),},
)+Folder.manage_options[-2:]
)
def namesForRegistration(self,container):
return {
'provides':('attributes',),
'getattr': self.objectIds(),
}
def _AttributeFor(self,client,name,default=None):
return MWp(self.__dict__.get(name,default))
def _SetAttributeFor(self,client,name,value):
pass
def _DelAttributeFor(self,client,name):
pass
def _setObject(self,id,object,roles=None,user=None, set_owner=1):
r = PlugInContainer._setObject.im_func(self,id,object,roles,user,set_owner)
self.aq_inner.aq_parent.manage_refreshPlugIns()
return r
def _delObject(self, id, dp=1):
PlugInContainer._delObject.im_func(self,id,dp)
self.aq_inner.aq_parent.manage_refreshPlugIns()
# Permission mapping stuff
def permissionMappingPossibleValues(self):
return self.possible_permissions()
_isBeingUsedAsAMethod_=1
def _isBeingUsedAsAMethod(self, REQUEST=None, wannaBe=0):
if REQUEST is not None and wannaBe: REQUEST.response.notFoundError()
return 0
class GAPMixin:
def namesForRegistration(self,container):
"""XXX"""
d={}
for attr in self.Attributes: d[attr[0]]=1
for attr in self.Defaults: d[attr[0]]=1
return {
'provides': ('attributes',),
'getattr': d.keys()
}
Attributes = ()
Defaults = ()
IsQuery = None
dependencies=()
def _AttributeFor(self,client,name,default=None):
try:
t = NamespaceStack()
pushProxy(self)
try:
data = {'self':client, 'ATTRIBUTE_NAME':name, 'NOT_FOUND':default}
if self._fromex is not None:
t._push(InstanceDict(self, t))
t._push(data)
try:
result = data['RESULT'] = self._fromex.eval(t)
finally:
t._pop(2)
if self.IsQuery:
if len(result):
result=result[0]
else:
result=default
else:
# use 'self' as result
result = data['RESULT'] = client
if result is default:
exprlist = self.Defaults
else:
exprlist = self.Attributes
if not exprlist:
return default
t._push(InstanceDict(result, t))
t._push(data)
try:
c = client._getCache()
sd = client._setDependencies
deps = self.dependencies
for a,e in exprlist:
# we set dependencies before each one so they'll
# still be set, and up through however far we got
if deps: sd(a,deps)
c[a] = e.eval(t)
finally:
t._pop(2)
finally:
popProxy(self)
return c.get(name,default)
except:
LOG('ZPatterns',WARNING,('Error computing attribute %s' % name),
'', sys.exc_info(), 1)
return default
class GenericAttributeProvider(GAPMixin,AttributeProvider,ProxyManager):
"""
XXX Explain this
"""
manage_options = AttributeProvider.manage_options + ProxyManager.manage_options
def _propertiesChanged(self):
self.Attributes = []
if self.fromexpr:
self._fromex = Expression(self.fromexpr)
else:
self._fromex = None
for l in filter(None,map(strip,self.attrsexprs)):
if '=' in l:
a,e = split(l,'=',1)
self.Attributes.append(a,Expression(e))
else:
self.Attributes.append(l,Name(l,0))
# Class Metadata
meta_type='Generic Attribute Provider'
fromexpr=''
attrsexprs=[]
_properties=(
{'id':'title', 'type': 'string', 'mode': 'w'},
{'id':'fromexpr', 'type': 'string', 'mode': 'w'},
{'id':'attrsexprs', 'type': 'lines', 'mode': 'w'},
)
ExprGetterAttributeProvider = GenericAttributeProvider
def initialize(context):
context.registerPlugInClass(
AttributeProvider,
permission = 'Add Attribute Providers',
constructors = defaultConstructors(AttributeProvider,globals()),
icon = 'www/attrprov.gif'
)
context.registerPlugInClass(
ClassExtender,
permission = 'Add DataSkin Class Extenders',
constructors = defaultConstructors(ClassExtender,globals()),
icon = 'www/ClassExtender.gif'
)
context.registerPlugInClass(
ExternalAttributeProvider,
permission = 'Add Attribute Providers',
constructors = defaultConstructors(ExternalAttributeProvider,globals()),
icon = 'www/PEAP.gif'
)
return
# disabled
context.registerPlugInClass(
GenericAttributeProvider,
permission = 'Add Generic Attribute Providers',
constructors = defaultConstructors(GenericAttributeProvider,
globals()),
icon = 'www/exprgetter.gif'
)
|