7231e8a9e0c4b91a21c1dccc3137088306f5ffae
[platform/core/uifw/at-spi2-atk.git] / pyatspi / base.py
1 #Copyright (C) 2008 Codethink Ltd
2
3 #This library is free software; you can redistribute it and/or
4 #modify it under the terms of the GNU Lesser General Public
5 #License version 2 as published by the Free Software Foundation.
6
7 #This program is distributed in the hope that it will be useful,
8 #but WITHOUT ANY WARRANTY; without even the implied warranty of
9 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 #GNU General Public License for more details.
11 #You should have received a copy of the GNU Lesser General Public License
12 #along with this program; if not, write to the Free Software
13 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14
15 import dbus
16 from dbus.proxies import Interface
17 from dbus.exceptions import *
18
19 import interfaces
20 from factory import create_accessible
21
22 __all__ = [
23            "AccessibleObjectNoLongerExists",
24            "Enum",
25            "BaseProxy",
26           ]
27
28 class AccessibleObjectNoLongerExists(Exception):
29         pass
30
31 #------------------------------------------------------------------------------
32
33 class Enum(int):
34         def __str__(self):
35                 return self._enum_lookup(int(self))
36
37 #------------------------------------------------------------------------------
38
39
40 class BaseProxyMeta(type):
41         def __init__(cls, *args, **kwargs):
42                 type.__init__(cls, *args, **kwargs)
43
44                 queryable_interfaces = { 
45                         'Accessible':interfaces.ATSPI_ACCESSIBLE,
46                         'Action':interfaces.ATSPI_ACTION,
47                         'Application':interfaces.ATSPI_APPLICATION,
48                         'Collection':interfaces.ATSPI_COLLECTION,
49                         'Component':interfaces.ATSPI_COMPONENT,
50                         'Desktop':interfaces.ATSPI_DESKTOP,
51                         'Document':interfaces.ATSPI_DOCUMENT,
52                         'EditableText':interfaces.ATSPI_EDITABLE_TEXT,
53                         'Hypertext':interfaces.ATSPI_HYPERTEXT,
54                         'Hyperlink':interfaces.ATSPI_HYPERLINK,
55                         'Image':interfaces.ATSPI_IMAGE,
56                         'Selection':interfaces.ATSPI_SELECTION,
57                         'StreamableContent':interfaces.ATSPI_STREAMABLE_CONTENT,
58                         'Table':interfaces.ATSPI_TABLE,
59                         'Text':interfaces.ATSPI_TEXT,
60                         'Value':interfaces.ATSPI_VALUE,
61                 }
62
63                 for interface in queryable_interfaces.keys():
64                         name = 'query%s' % interface
65                         def new_query(self, object):
66                                 return self.queryInterface(object, queryable_interfaces[interface])
67                         setattr(cls, name, new_query) 
68
69 #------------------------------------------------------------------------------
70
71 class BaseProxy(Interface):
72         """
73         The base D-Bus proxy for a remote object that implements one or more
74         of the AT-SPI interfaces.
75         """
76
77         __metaclass__ = BaseProxyMeta
78
79         def __init__(self, cache, app_name, acc_path, interface, dbus_object=None, connection=None, parent=None):
80                 """
81                 Create a D-Bus Proxy for an ATSPI interface.
82
83                 cache - ApplicationCache, where the cached data for the accessible can be obtained.
84                 app_name - D-Bus bus name of the application this accessible belongs to.
85                 acc_path - D-Bus object path of the server side accessible object.
86                 parent - Parent accessible.
87                 interface - D-Bus interface of the object. Used to decide which accessible class to instanciate.
88                 dbus_object(kwarg) - The D-Bus proxy object used by the accessible for D-Bus method calls.
89                 """
90                 self._cache = cache
91                 self._app_name = app_name
92                 self._acc_path = acc_path
93                 self._parent = parent
94
95                 if not dbus_object:
96                         dbus_object = connection.get_object(self._app_name, self._acc_path, introspect=False)
97                 self._dbus_object = dbus_object
98
99                 Interface.__init__(self, self._dbus_object, interface)
100
101                 self._pgetter = self.get_dbus_method("Get", dbus_interface="org.freedesktop.DBus.Properties")
102                 self._psetter = self.get_dbus_method("Set", dbus_interface="org.freedesktop.DBus.Properties")
103
104         def get_dbus_method(self, *args, **kwargs):
105                 method =  Interface.get_dbus_method(self, *args, **kwargs)
106
107                 def dbus_method_func(*args, **kwargs):
108                         # TODO Need to throw an AccessibleObjectNoLongerExists exception
109                         # on D-Bus error of the same type.
110                         try:
111                                 return method(*args, **kwargs)
112                         except UnknownMethodException, e:
113                                 raise NotImplementedError(e)
114                         except DBusException, e:
115                                 raise LookupError(e)
116
117                 return dbus_method_func
118
119         @property
120         def cached_data(self):
121                 try:
122                         return self._cache[self._app_name][self._acc_path]
123                 except KeyError:
124                         raise AccessibleObjectNoLongerExists, \
125                                 'Cache data cannot be found for path %s in app %s' % (self._acc_path, self._app_name)
126
127         @property
128         def interfaces(self):
129                 return self._data.interfaces
130
131         def queryInterface(self, interface):
132                 """
133                 Gets a different accessible interface for this object
134                 or raises a NotImplemented error if the given interface
135                 is not supported.
136                 """
137                 if interface in self._data.interfaces:
138                         return create_accessible(self._cache,
139                                                  self._app_name,
140                                                  self._acc_path,
141                                                  self._parent,
142                                                  interface,
143                                                  dbus_object=self._dbus_object)
144                 else:
145                         raise NotImplementedError(
146                                 "%s not supported by accessible object at path %s"
147                                 % (interface, self.path))
148
149 #END----------------------------------------------------------------------------