1
# Copyright (C) 2012, Daniel Narvaez
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
from gi.repository import Atspi
25
from gi.repository import GLib
29
return Node(Atspi.get_desktop(0))
32
def _retry_find(func):
33
def wrapped(*args, **kwargs):
37
while n_retries <= 50:
38
logging.info("Try %d, name=%s role_name=%s" %
40
kwargs.get("name", None),
41
kwargs.get("role_name", None)))
44
result = func(*args, **kwargs)
45
except GLib.GError, e:
46
# The application is not responding, try again
47
if e.code == Atspi.Error.IPC:
50
logging.error("GError code %d" % e.code)
53
expect_none = kwargs.get("expect_none", False)
54
if (not expect_none and result) or \
55
(expect_none and not result):
59
n_retries = n_retries + 1
67
def __init__(self, accessible):
68
self._accessible = accessible
72
self._crawl_accessible(self, 0, lines)
73
return "\n".join(lines)
75
def do_action(self, name):
76
for i in range(self._accessible.get_n_actions()):
77
# New, incompatible API
78
if hasattr(self._accessible, "get_action_name"):
79
action_name = self._accessible.get_action_name(i)
81
action_name = Atspi.Action.get_name(self._accessible, i)
83
if action_name == name:
84
self._accessible.do_action(i)
86
def click(self, button=1):
87
point = self._accessible.get_position(Atspi.CoordType.SCREEN)
88
Atspi.generate_mouse_event(point.x, point.y, "b%sc" % button)
92
return self._accessible.get_name()
96
return self._accessible.get_role_name()
100
return Atspi.Text.get_text(self._accessible, 0, -1)
102
def get_children(self):
105
for i in range(self._accessible.get_child_count()):
106
child = self._accessible.get_child_at_index(i)
108
# We sometimes get none children from atspi
109
if child is not None:
110
children.append(Node(child))
115
def find_children(self, name=None, role_name=None):
117
return self._predicate(node, name, role_name)
120
self._find_all_descendants(self, predicate, descendants)
127
def find_child(self, name=None, role_name=None, expect_none=False):
129
return self._predicate(node, name, role_name)
131
node = self._find_descendant(self, predicate)
138
return "[%s | %s]" % (self.name, self.role_name)
140
def _predicate(self, node, name, role_name):
141
if name is not None and name != node.name:
144
if role_name is not None and role_name != node.role_name:
149
def _find_descendant(self, node, predicate):
153
for child in node.get_children():
154
descendant = self._find_descendant(child, predicate)
155
if descendant is not None:
160
def _find_all_descendants(self, node, predicate, matches):
164
for child in node.get_children():
165
self._find_all_descendants(child, predicate, matches)
167
def _crawl_accessible(self, node, depth, lines):
168
lines.append(" " * depth + str(node))
170
for child in node.get_children():
171
self._crawl_accessible(child, depth + 1, lines)