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