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   
 26  iszprop = re.compile("^z[A-Z]").search 
 27   
28 -class ZenPropertyManager(PropertyManager):
29 """ 30 ZenPropertyManager adds keyedselection type to PropertyManager. 31 A keyedselection displayes a different name in the popup then 32 the actual value the popup will have. 33 34 It also has management for zenProperties which are properties that can be 35 inherited long the acquision chain. All properties are for a branch are 36 defined on a "root node" specified by the function which must be returned 37 by the function getZenRootNode that should be over ridden in a sub class. 38 Prperties can then be added further "down" the aq_chain by calling 39 setZenProperty on any contained node. 40 41 ZenProperties all have the same prefix which is defined by iszprop 42 this can be overridden in a subclass. 43 """ 44 security = ClassSecurityInfo() 45 46 manage_propertiesForm=DTMLFile('dtml/properties', globals(), 47 property_extensible_schema__=1) 48
49 - def _setPropValue(self, id, value):
50 """override from PerpertyManager to handle checks and ip creation""" 51 self._wrapperCheck(value) 52 if self.getPropertyType(id) == 'keyedselection': 53 value = int(value) 54 if not getattr(self,'_v_propdict',False): 55 self._v_propdict = self.propdict() 56 if self._v_propdict.has_key('setter'): 57 settername = self._v_propdict['setter'] 58 setter = getattr(aq_base(obj), settername, None) 59 if not setter: 60 raise ValueError("setter %s for property %s doesn't exist" 61 % (settername, id)) 62 if not callable(setter): 63 raise TypeError("setter %s for property %s not callable" 64 % (settername, id)) 65 setter(value) 66 else: 67 setattr(self,id,value)
68 69
70 - def _setProperty(self, id, value, type='string', label=None, 71 visible=True, setter=None):
72 """for selection and multiple selection properties 73 the value argument indicates the select variable 74 of the property 75 """ 76 self._wrapperCheck(value) 77 if not self.valid_property_id(id): 78 raise BadRequest, 'Id %s is invalid or duplicate' % id 79 80 def setprops(**pschema): 81 self._properties=self._properties+(pschema,) 82 if setter: pschema['setter'] = setter 83 if label: pschema['label'] = label
84 85 if type in ('selection', 'multiple selection'): 86 if not hasattr(self, value): 87 raise BadRequest, 'No select variable %s' % value 88 setprops(id=id,type=type, visible=visible, 89 select_variable=value) 90 if type=='selection': 91 self._setPropValue(id, '') 92 else: 93 self._setPropValue(id, []) 94 else: 95 setprops(id=id, type=type, visible=visible) 96 self._setPropValue(id, value)
97 98 99 _onlystars = re.compile("^\*+$").search 100 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'manage_editProperties')
101 - def manage_editProperties(self, REQUEST):
102 """ 103 Edit object properties via the web. 104 The purpose of this method is to change all property values, 105 even those not listed in REQUEST; otherwise checkboxes that 106 get turned off will be ignored. Use manage_changeProperties() 107 instead for most situations. 108 """ 109 for prop in self._propertyMap(): 110 name=prop['id'] 111 if 'w' in prop.get('mode', 'wd'): 112 value=REQUEST.get(name, '') 113 if self.zenPropIsPassword(name) and self._onlystars(value): 114 continue 115 self._updateProperty(name, value) 116 if getattr(self, "index_object", False): 117 self.index_object() 118 if REQUEST: 119 message="Saved changes." 120 return self.manage_propertiesForm(self,REQUEST, 121 manage_tabs_message=message)
122 123
124 - def getZenRootNode(self):
125 """sub class must implement to use zenProperties.""" 126 raise NotImplementedError
127 128 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyIds')
129 - def zenPropertyIds(self, all=True, pfilt=iszprop):
130 """ 131 Return list of device tree property names. 132 If all use list from property root node. 133 """ 134 if all: 135 rootnode = self.getZenRootNode() 136 else: 137 if self.id == self.dmdRootName: return [] 138 rootnode = aq_base(self) 139 props = [] 140 for prop in rootnode.propertyIds(): 141 if not pfilt(prop): continue 142 props.append(prop) 143 props.sort() 144 return props
145 146 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyItems')
147 - def zenPropertyItems(self):
148 """Return list of (id, value) tuples of zenProperties. 149 """ 150 return map(lambda x: (x, getattr(self, x)), self.zenPropertyIds())
151 152 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyMap')
153 - def zenPropertyMap(self, pfilt=iszprop):
154 """Return property mapping of device tree properties.""" 155 rootnode = self.getZenRootNode() 156 pmap = [] 157 for pdict in rootnode.propertyMap(): 158 if pfilt(pdict['id']): pmap.append(pdict) 159 pmap.sort(lambda x, y: cmp(x['id'], y['id'])) 160 return pmap
161 162 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyString')
163 - def zenPropertyString(self, id):
164 """Return the value of a device tree property as a string""" 165 value = getattr(self, id, "") 166 rootnode = self.getZenRootNode() 167 type = rootnode.getPropertyType(id) 168 if type == "lines": 169 value = "\n".join(map(str, value)) 170 elif self.zenPropIsPassword(id): 171 value = "*"*len(value) 172 return value
173 174 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropIsPassword')
175 - def zenPropIsPassword(self, id):
176 """Is this field a password field. 177 """ 178 return id.endswith("assword")
179 180 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyPath')
181 - def zenPropertyPath(self, id):
182 """Return the primaryId of where a device tree property is found.""" 183 for obj in aq_chain(self): 184 if getattr(aq_base(obj), id, zenmarker) != zenmarker: 185 return obj.getPrimaryId(self.getZenRootNode().getId())
186 187 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyType')
188 - def zenPropertyType(self, id):
189 """Return the type of this property.""" 190 ptype = self.getZenRootNode().getPropertyType(id) 191 if not ptype: ptype = "string" 192 return ptype
193 194 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'setZenProperty')
195 - def setZenProperty(self, propname, propvalue, REQUEST=None):
196 """ 197 Add or set the propvalue of the property propname on this node of 198 the device Class tree. 199 """ 200 rootnode = self.getZenRootNode() 201 ptype = rootnode.getPropertyType(propname) 202 if ptype == 'lines': 203 dedupedList = [] 204 for x in propvalue: 205 if x not in dedupedList: 206 dedupedList.append(x) 207 propvalue = dedupedList 208 if getattr(aq_base(self), propname, zenmarker) != zenmarker: 209 self._updateProperty(propname, propvalue) 210 else: 211 if ptype in ("selection", 'multiple selection'): ptype="string" 212 if type_converters.has_key(ptype): 213 propvalue=type_converters[ptype](propvalue) 214 self._setProperty(propname, propvalue, type=ptype) 215 if REQUEST: return self.callZenScreen(REQUEST)
216 217 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'saveZenProperties')
218 - def saveZenProperties(self, pfilt=iszprop, REQUEST=None):
219 """Save all ZenProperties found in the REQUEST.form object. 220 """ 221 for name, value in REQUEST.form.items(): 222 if pfilt(name): 223 if self.zenPropIsPassword(name) and self._onlystars(value): 224 continue 225 if name == 'zCollectorPlugins': 226 if tuple(getattr(self, name, ())) != tuple(value): 227 self.setZenProperty(name, value) 228 elif getattr(self, name, None) != value: 229 self.setZenProperty(name, value) 230 231 return self.callZenScreen(REQUEST)
232 233 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'deleteZenProperty')
234 - def deleteZenProperty(self, propname=None, REQUEST=None):
235 """ 236 Delete device tree properties from the this DeviceClass object. 237 """ 238 if propname: 239 self._delProperty(propname) 240 if REQUEST: return self.callZenScreen(REQUEST)
241 242 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyOptions')
243 - def zenPropertyOptions(self, propname):
244 "Provide a set of default options for a ZProperty" 245 return []
246 247 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'isLocal')
248 - def isLocal(self, propname):
249 """Check to see if a name is local to our current context. 250 """ 251 v = getattr(aq_base(self), propname, zenmarker) 252 return v != zenmarker
253 254 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
255 - def getOverriddenObjects(self, propname, showDevices=False):
256 """ Get the objects that override a property somewhere below in the tree 257 """ 258 if showDevices: 259 objects = [] 260 for inst in self.getSubInstances('devices'): 261 if inst.isLocal(propname) and inst not in objects: 262 objects.append(inst) 263 for suborg in self.children(): 264 if suborg.isLocal(propname): 265 objects.append(suborg) 266 for inst in suborg.getOverriddenObjects(propname, showDevices): 267 if inst not in objects: 268 objects.append(inst) 269 return objects 270 271 return [ org for org in self.getSubOrganizers() 272 if org.isLocal(propname) ]
273 274 275 InitializeClass(ZenPropertyManager) 276