test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BasicDataAccessor.py
Go to the documentation of this file.
1 class BasicDataAccessor(object):
2  """ This class provides access to the underlying data model.
3  """
4 
5  def children(self, object):
6  """ Return the children of a container object.
7  """
8  raise NotImplementedError
9 
10  def isContainer(self, object):
11  """ Return if the object is a container object.
12  """
13  raise NotImplementedError
14 
15  def label(self, object):
16  """ Return a string that is used as caption of an object.
17  """
18  raise NotImplementedError
19 
20  def properties(self, object):
21  """ Return the list of the properties of an object.
22 
23  Each property is represented by a tuple containing its
24  type, name, value, description, readonly(True/False), deletable(True/False).
25  Possible types are: 'Category','String','MultilineString','File','FileVector','Boolean','Integer','Double'.
26  """
27  raise NotImplementedError
28 
29  def setProperty(self, object, name, value, categoryName):
30  """ Change the property 'name' of an object to a new value.
31  """
32  raise NotImplementedError
33 
34  def addProperty(self, object, name, value, type):
35  """ Add the property 'name' to an object.
36  """
37  raise NotImplementedError
38 
39  def removeProperty(self, object, name):
40  """ Remove the property 'name' from an object.
41  """
42  raise NotImplementedError
43 
44  def property(self, object, name):
45  """ Returns property with given name.
46  """
47  propertiesDict = {}
48  for p in self.properties(object):
49  propertiesDict[p[1]] = p
50  if name in propertiesDict.keys():
51  return propertiesDict[name]
52  else:
53  return None
54 
55  def propertyValue(self, object, name):
56  """ Returns value of property with given name.
57  """
58  property=self.property(object,name)
59  if property!=None:
60  return property[2]
61  else:
62  return None
63 
64  def allChildren(self, object):
65  """ Collect all children of children of an object.
66  """
67  children = []
68  for child in self.children(object):
69  children += [child]+self.allChildren(child)
70  return children
71 
72  def topLevelObjects(self):
73  """ Return top level objects, e.g. the event.
74  """
75  raise NotImplementedError
76 
78  """ This class gives a comfortable Interface to objects accessible via an accessor.
79 
80  Given the object and the accessor all properties and attributes of the object and
81  the accessor are accessible via __getattr__. A script in which all attributes
82  of the objects can be accessed can be run.
83  """
84  def __init__(self, object, accessor, throwAttributeErrors=True):
85  self._object = object
86  self._accessor = accessor
87  self._throwAttributeErrors=throwAttributeErrors
88 
89  def __getattr__(self, attr):
90  if attr in [p[1] for p in self._accessor.properties(self._object)]:
91  return self._accessor.propertyValue(self._object, attr)
92  elif hasattr(self._object, attr):
93  return getattr(self._object, attr)
94  elif hasattr(self._accessor, attr):
95  return getattr(self._accessor, attr)(self._object)
96  else:
97  if self._throwAttributeErrors:
98  raise AttributeError("object has no property '" + attr + "'")
99  else:
100  return "???"
101 
102  def runScript(self, script):
103  object = self
104  exec "result=" + str(script)
105  return result