Package ZenRelations :: Module ZenPropertyManager
[hide private]
[frames] | no frames]

Source Code for Module ZenRelations.ZenPropertyManager

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import re 
 15   
 16  from OFS.PropertyManager import PropertyManager 
 17  from zExceptions import BadRequest 
 18  from Globals import DTMLFile 
 19  from Globals import InitializeClass 
 20  from Acquisition import aq_base, aq_chain 
 21  from ZPublisher.Converters import type_converters 
 22  from Products.ZenModel.ZenossSecurity import * 
 23  from AccessControl import ClassSecurityInfo 
 24  from Exceptions import zenmarker 
 25  iszprop = re.compile("^z[A-Z]").search 
 26   
 27  from Products.ZenUtils.Utils import unused 
 28   
29 -class ZenPropertyManager(PropertyManager):
30 """ 31 ZenPropertyManager adds keyedselection type to PropertyManager. 32 A keyedselection displayes a different name in the popup then 33 the actual value the popup will have. 34 35 It also has management for zenProperties which are properties that can be 36 inherited long the acquision chain. All properties are for a branch are 37 defined on a "root node" specified by the function which must be returned 38 by the function getZenRootNode that should be over ridden in a sub class. 39 Prperties can then be added further "down" the aq_chain by calling 40 setZenProperty on any contained node. 41 42 ZenProperties all have the same prefix which is defined by iszprop 43 this can be overridden in a subclass. 44 """ 45 __pychecker__='no-override' 46 47 security = ClassSecurityInfo() 48 49 manage_propertiesForm=DTMLFile('dtml/properties', globals(), 50 property_extensible_schema__=1) 51
52 - def _setPropValue(self, id, value):
53 """override from PerpertyManager to handle checks and ip creation""" 54 self._wrapperCheck(value) 55 if self.getPropertyType(id) == 'keyedselection': 56 value = int(value) 57 if not getattr(self,'_v_propdict',False): 58 self._v_propdict = self.propdict() 59 if self._v_propdict.has_key('setter'): 60 settername = self._v_propdict['setter'] 61 setter = getattr(aq_base(self), settername, None) 62 if not setter: 63 raise ValueError("setter %s for property %s doesn't exist" 64 % (settername, id)) 65 if not callable(setter): 66 raise TypeError("setter %s for property %s not callable" 67 % (settername, id)) 68 setter(value) 69 else: 70 setattr(self,id,value)
71 72
73 - def _setProperty(self, id, value, type='string', label=None, 74 visible=True, setter=None):
75 """for selection and multiple selection properties 76 the value argument indicates the select variable 77 of the property 78 """ 79 self._wrapperCheck(value) 80 if not self.valid_property_id(id): 81 raise BadRequest, 'Id %s is invalid or duplicate' % id 82 83 def setprops(**pschema): 84 self._properties=self._properties+(pschema,) 85 if setter: pschema['setter'] = setter 86 if label: pschema['label'] = label
87 88 if type in ('selection', 'multiple selection'): 89 if not hasattr(self, value): 90 raise BadRequest, 'No select variable %s' % value 91 setprops(id=id,type=type, visible=visible, 92 select_variable=value) 93 if type=='selection': 94 self._setPropValue(id, '') 95 else: 96 self._setPropValue(id, []) 97 else: 98 setprops(id=id, type=type, visible=visible) 99 self._setPropValue(id, value)
100 101
102 - def _updateProperty(self, id, value):
103 """ This method sets a property on a zope object. It overrides the 104 method in PropertyManager. If Zope is upgraded you will need to check 105 that this method has not changed! It is overridden so that we can catch 106 the ValueError returned from the field2* converters in the class 107 Converters.py 108 """ 109 from Products.ZenWidgets import messaging 110 try: 111 super(ZenPropertyManager, self)._updateProperty(id, value) 112 except ValueError: 113 proptype = self.getPropertyType(id) 114 messaging.IMessageSender(self).sendToBrowser( 115 'Error Saving Property %s' % id, 116 ("New value '%s' is of invalid type. " 117 "It should be type '%s'") % (value, proptype), 118 priority=messaging.CRITICAL 119 )
120 121 122 _onlystars = re.compile("^\*+$").search 123 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'manage_editProperties')
124 - def manage_editProperties(self, REQUEST):
125 """ 126 Edit object properties via the web. 127 The purpose of this method is to change all property values, 128 even those not listed in REQUEST; otherwise checkboxes that 129 get turned off will be ignored. Use manage_changeProperties() 130 instead for most situations. 131 """ 132 for prop in self._propertyMap(): 133 name=prop['id'] 134 if 'w' in prop.get('mode', 'wd'): 135 value=REQUEST.get(name, '') 136 if self.zenPropIsPassword(name) and self._onlystars(value): 137 continue 138 self._updateProperty(name, value) 139 if getattr(self, "index_object", False): 140 self.index_object() 141 if REQUEST: 142 message="Saved changes." 143 return self.manage_propertiesForm(self,REQUEST, 144 manage_tabs_message=message)
145 146
147 - def getZenRootNode(self):
148 """sub class must implement to use zenProperties.""" 149 raise NotImplementedError
150 151 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyIds')
152 - def zenPropertyIds(self, all=True, pfilt=iszprop):
153 """ 154 Return list of device tree property names. 155 If all use list from property root node. 156 """ 157 if all: 158 rootnode = self.getZenRootNode() 159 else: 160 if self.id == self.dmdRootName: return [] 161 rootnode = aq_base(self) 162 props = [] 163 for prop in rootnode.propertyIds(): 164 if not pfilt(prop): continue 165 props.append(prop) 166 props.sort() 167 return props
168 169 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyItems')
170 - def zenPropertyItems(self):
171 """Return list of (id, value) tuples of zenProperties. 172 """ 173 return map(lambda x: (x, getattr(self, x)), self.zenPropertyIds())
174 175 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyMap')
176 - def zenPropertyMap(self, pfilt=iszprop):
177 """Return property mapping of device tree properties.""" 178 rootnode = self.getZenRootNode() 179 pmap = [] 180 for pdict in rootnode.propertyMap(): 181 if pfilt(pdict['id']): pmap.append(pdict) 182 pmap.sort(lambda x, y: cmp(x['id'], y['id'])) 183 return pmap
184 185 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyString')
186 - def zenPropertyString(self, id):
187 """Return the value of a device tree property as a string""" 188 value = getattr(self, id, "") 189 rootnode = self.getZenRootNode() 190 type = rootnode.getPropertyType(id) 191 if type == "lines": 192 value = "\n".join(map(str, value)) 193 elif self.zenPropIsPassword(id): 194 value = "*"*len(value) 195 return value
196 197 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropIsPassword')
198 - def zenPropIsPassword(self, id):
199 """Is this field a password field. 200 """ 201 return id.endswith("assword")
202 203 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyPath')
204 - def zenPropertyPath(self, id):
205 """Return the primaryId of where a device tree property is found.""" 206 for obj in aq_chain(self): 207 if getattr(aq_base(obj), id, zenmarker) != zenmarker: 208 return obj.getPrimaryId(self.getZenRootNode().getId())
209 210 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyType')
211 - def zenPropertyType(self, id):
212 """Return the type of this property.""" 213 ptype = self.getZenRootNode().getPropertyType(id) 214 if not ptype: ptype = "string" 215 return ptype
216 217 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'setZenProperty')
218 - def setZenProperty(self, propname, propvalue, REQUEST=None):
219 """ 220 Add or set the propvalue of the property propname on this node of 221 the device Class tree. 222 """ 223 rootnode = self.getZenRootNode() 224 ptype = rootnode.getPropertyType(propname) 225 if ptype == 'lines': 226 dedupedList = [] 227 for x in propvalue: 228 if x not in dedupedList: 229 dedupedList.append(x) 230 propvalue = dedupedList 231 if getattr(aq_base(self), propname, zenmarker) != zenmarker: 232 self._updateProperty(propname, propvalue) 233 else: 234 if ptype in ("selection", 'multiple selection'): ptype="string" 235 if type_converters.has_key(ptype): 236 propvalue=type_converters[ptype](propvalue) 237 if getattr(self, propname, None) != propvalue: 238 self._setProperty(propname, propvalue, type=ptype) 239 if REQUEST: return self.callZenScreen(REQUEST)
240 241 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'saveZenProperties')
242 - def saveZenProperties(self, pfilt=iszprop, REQUEST=None):
243 """Save all ZenProperties found in the REQUEST.form object. 244 """ 245 for name, value in REQUEST.form.items(): 246 if pfilt(name): 247 if self.zenPropIsPassword(name) and self._onlystars(value): 248 continue 249 if name == 'zCollectorPlugins': 250 if tuple(getattr(self, name, ())) != tuple(value): 251 self.setZenProperty(name, value) 252 else: 253 self.setZenProperty(name, value) 254 255 return self.callZenScreen(REQUEST)
256 257 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'deleteZenProperty')
258 - def deleteZenProperty(self, propname=None, REQUEST=None):
259 """ 260 Delete device tree properties from the this DeviceClass object. 261 """ 262 if propname: 263 self._delProperty(propname) 264 if REQUEST: return self.callZenScreen(REQUEST)
265 266 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyOptions')
267 - def zenPropertyOptions(self, propname):
268 "Provide a set of default options for a ZProperty" 269 unused(propname) 270 return []
271 272 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'isLocal')
273 - def isLocal(self, propname):
274 """Check to see if a name is local to our current context. 275 """ 276 v = getattr(aq_base(self), propname, zenmarker) 277 return v != zenmarker
278 279 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
280 - def getOverriddenObjects(self, propname, showDevices=False):
281 """ Get the objects that override a property somewhere below in the tree 282 """ 283 if showDevices: 284 objects = [] 285 for inst in self.getSubInstances('devices'): 286 if inst.isLocal(propname) and inst not in objects: 287 objects.append(inst) 288 for suborg in self.children(): 289 if suborg.isLocal(propname): 290 objects.append(suborg) 291 for inst in suborg.getOverriddenObjects(propname, showDevices): 292 if inst not in objects: 293 objects.append(inst) 294 return objects 295 296 return [ org for org in self.getSubOrganizers() 297 if org.isLocal(propname) ]
298 299 300 InitializeClass(ZenPropertyManager) 301