Changes to introspection generation to remove DOCTYPE and XML
[platform/core/uifw/at-spi2-atk.git] / tests / AccessibleTreeCache.py
1 import dbus
2
3 class AccessibleObjectDoesNotExist(Exception):
4         def __init__(self, path):
5                 self.path = path
6         
7         def __str__(self):
8                 return "Object %s does not exist" % (self.path)
9
10 class AccessibleTreeCache():
11         """
12         Caches a collection of Accessible Objects.
13         """
14
15         _TREE_INTERFACE = 'org.freedesktop.atspi.Tree'
16         _UPDATE_SIGNAL = 'updateTree'
17
18         def __init__(self, connection, busName, objectStorePath):
19                 """
20                 Creates a cache for accessible objects.
21
22                 All accessible objects are created and accessed through this cache.
23
24                 Parameters:
25                 connection - DBus connection.
26                 busName - DBus bus name where accessible tree resides.
27                 objectStorePath - Path where the accessible tree can be accessed.
28                 """
29                 storeObject = connection.get_object(busName, objectStorePath)
30
31                 self._busName = busName
32                 self._accessibleStore = dbus.Interface(storeObject, self._TREE_INTERFACE)
33                 self._objects = {}
34                 self._root = self._accessibleStore.getRoot()
35
36                 self._updateObjects(self._accessibleStore.getTree())
37
38                 #Connect to update signal
39                 self._signalMatch = self._accessibleStore.connect_to_signal(self._UPDATE_SIGNAL,
40                                                                             self._updateHandler)
41
42         def getRootAccessible(self):
43                 """
44                 Gets the accessible object at the root of the tree.
45                 """
46                 return self.getAccessible(self._root)
47
48         def getAccessible(self, objectPath):
49                 """
50                 Gets the accessible object for the given object path.
51                 """
52                 if objectPath in self._objects:
53                         return self._objects[objectPath]
54                 else:
55                         raise AccessibleObjectDoesNotExist(objectPath)
56
57         def _updateObjects(self, objects):
58                 """
59                 Updates the object cache from an
60                 array of wire format Accessible objects.
61                 """
62                 for object in objects:
63                         (path,
64                          parent,
65                          children,
66                          interfaces,
67                          name, 
68                          role,
69                          description) = object
70                         if path in self._objects:
71                                 self._objects[path].update(path,
72                                                            parent,
73                                                            children, 
74                                                            interfaces, 
75                                                            name, 
76                                                            role, 
77                                                            description)
78                         else:
79                                 acc = AccessibleObjectProxy(self,
80                                                             self._busName,
81                                                             path,
82                                                             parent,
83                                                             children,
84                                                             interfaces,
85                                                             name,
86                                                             role,
87                                                             description)
88                                 self._objects[path] = acc;
89
90         def _removeObjects(self, paths):
91                 for path in paths:
92                         #Probably want to set object as invalid first
93                         del(self._objects[path])
94
95         def _updateHandler(self, updates):
96                 objects, paths = updates
97                 self._removeObjects(paths)
98                 self._updateObjects(objects)
99
100 class AccessibleObjectProxy():
101         """
102         A D-Bus proxy for an element that implements one or more of the AT-SPI
103         Accessibility interfaces.
104         """
105
106         def __init__(self, cache, bus,
107                      path, parent,
108                      children, interfaces, 
109                      name, role, description):
110                 """
111                 Create an accessible object.
112
113                 Parameters:
114
115                 cache - The accessible cache that this object is owned by.
116                 bus - Bus name where proxied object can be located.
117                 path - The D-Bus object path of object proxied by this object.
118                 parent - The parent accessible or '/' if the root object.
119                 children - List of child accessible objects.
120                 interfaces - List of interfaces supported by this object.
121                 name - Name of the accessible.
122                 role - The accessibles role.
123                 description - Description of the accessible
124                 """
125                 self._cache = cache
126                 self.bus = bus
127                 self.update(path, parent, children, interfaces, name, role, description)
128
129         def update(self, path, parent,
130                    children, interfaces, 
131                    name, role, description):
132                 """
133                 update an accessible object.
134
135                 Parameters:
136
137                 path - The D-Bus object path of object proxied by this object.
138                 parent - The parent accessible or '/' if the root object.
139                 children - List of child accessible objects.
140                 interfaces - List of interfaces supported by this object.
141                 name - Name of the accessible.
142                 role - The accessibles role.
143                 description - Description of the accessible
144                 """
145                 self.path = path
146                 self.parent = parent
147                 self.children = children
148                 self.interfaces = interfaces
149                 self.name = name
150                 self.role = role
151                 self.description = description
152
153         def getChildren(self):
154                 return [self._cache.getAccessible(path) for path in self.children]