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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14
15import string
16
17class _ELessList(list):
18        def __getitem__(self, index):
19                try:
20                        return list.__getitem__(self, index)
21                except IndexError:
22                        return None
23
24class EventType(str):
25        """
26        Wraps the AT-SPI event type string so its components can be accessed
27        individually as klass (can't use the keyword class), major, minor, and detail
28        (klass_major_minor_detail).
29
30        @note: All attributes of an instance of this class should be considered
31                public readable as it is acting a a struct.
32        @ivar klass: Most general event type identifier (object, window, mouse, etc.)
33        @type klass: string
34        @ivar major: Second level event type description
35        @type major: string
36        @ivar minor: Third level event type description
37        @type minor: string
38        @ivar detail: Lowest level event type description
39        @type detail: string
40        @ivar name: Full, unparsed event name as received from AT-SPI
41        @type name: string
42        @cvar format: Names of the event string components
43        @type format: 4-tuple of string
44        """
45
46        _SEPARATOR = ':'
47
48        def __init__(self, name):
49                """
50                Parses the full AT-SPI event name into its components
51                (klass:major:minor:detail). If the provided event name is an integer
52                instead of a string, then the event is really a device event.
53
54                @param name: Full AT-SPI event name
55                @type name: string
56                @raise AttributeError: When the given event name is not a valid string
57                """
58                stripped = name.strip(self._SEPARATOR)
59                separated = stripped.split(self._SEPARATOR, 3)
60                self._separated = _ELessList(separated)
61
62                self.klass = self._separated[0]
63                self.major = self._separated[1]
64                self.minor = self._separated[2]
65                self.detail = self._separated[3]
66
67        def is_subtype(self, event_type, excludeSelf = False):
68                """
69                Determines if the passed event type is a subtype
70                of this event.
71                """
72                if event_type.klass and event_type.klass !=  self.klass:
73                        return False
74                else:
75                        if event_type.major and event_type.major != self.major:
76                                return False
77                        else:
78                                if event_type.minor and event_type.minor != self.minor:
79                                        return False
80                if (excludeSelf and event_type.klass == self.klass
81                    and event_type.major == self.major and event_type.minor == self.minor):
82                        return False
83                return True
84
85        @property
86        def name(self):
87                return str(self)
88
89        @property
90        def value(self):
91                return str(self)
92