summaryrefslogtreecommitdiff
path: root/bin/gla11y
blob: 04d5d83ccd1aa8166db13922b78bf2a249c2bc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Copyright (c) 2018 Martin Pieuchot
#   Copyright (c) 2018 Samuel Thibault <sthibault@hypra.fr>
#
#   Permission to use, copy, modify, and distribute this software for any
#   purpose with or without fee is hereby granted, provided that the above
#   copyright notice and this permission notice appear in all copies.
#
#   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Take LibreOffice (glade) .ui files and check for non accessible widgets

from __future__ import print_function

import os
import sys
import getopt
import lxml.etree as ET

progname = os.path.basename(sys.argv[0])
Werror = False
Wnone = False
errors = 0
warnings = 0


def errstr(elm):
    """
    Print the line number of the element
    """

    return str(elm.sourceline)

def err(filename, elm, msg):
    global errors

    if elm == None:
        prefix = "%s:" % filename
    else:
        prefix = "%s:%s" % (filename, errstr(elm))

    errors += 1
    msg = "%s ERROR: %s" % (prefix, msg)
    print(msg.encode('ascii', 'ignore'))


def warn(filename, elm, msg):
    global Werror, Wnone, errors, warnings

    if Wnone:
        return

    prefix = "%s:%s" % (filename, errstr(elm))

    if Werror:
        errors += 1
    else:
        warnings += 1

    msg = "%s WARNING: %s" % (prefix,  msg)
    print(msg.encode('ascii', 'ignore'))


def check_objects(filename, elm, objects, target):
    """
    Check that objects contains exactly one object
    """
    length = len(list(objects))
    if length == 0:
        err(filename, elm, "use of undeclared target '%s'" % target)
    elif length > 1:
        err(filename, elm, "several targets are named '%s'" % target)

def check_props(filename, root, props):
    """
    Check the given list of relation properties
    """
    for prop in props:
        objects = root.iterfind(".//object[@id='%s']" % prop.text)
        check_objects(filename, prop, objects, prop.text)

def check_rels(filename, root, rels):
    """
    Check the given list of relations
    """
    for rel in rels:
        target = rel.attrib['target']
        targets = root.iterfind(".//object[@id='%s']" % target)
        check_objects(filename, rel, targets, target)

def check_a11y_relation(filename, root):
    """
    Emit an error message if any of the 'object' elements of the XML
    document represented by `root' doesn't comply with Accessibility
    rules.
    """

    for obj in root.iter('object'):

        label_for = obj.findall("accessibility/relation[@type='label-for']")
        check_rels(filename, root, label_for)

        labelled_by = obj.findall("accessibility/relation[@type='labelled-by']")
        check_rels(filename, root, labelled_by)

        member_of = obj.findall("accessibility/relation[@type='member-of']")
        check_rels(filename, root, member_of)

        if obj.attrib['class'] == 'GtkLabel':
            # Case 0: A 'GtkLabel' must contain one or more "label-for"
            # pointing to existing elements or...
            if len(label_for) > 0:
                continue

            # ...a single "mnemonic_widget"
            properties = obj.findall("property[@name='mnemonic_widget']")
            check_props(filename, root, properties)
            if len(properties) > 1:
                # It does not make sense for a label to be a mnemonic for
                # several actions.
                lines = ', '.join([str(p.sourceline) for p in properties])
                err(filename, obj, "too many sub-elements"
                    ", expected single <property name='mnemonic_widgets'>"
                    ": lines %s" % lines)
                continue
            if len(properties) == 1:
                continue
            # TODO: warn that it is a label for nothing
            continue

        # Case 1: has a <child internal-child="accessible"> sub-element
        children = obj.findall("child[@internal-child='accessible']")
        if children:
            if len(children) > 1:
                lines = ', '.join([str(c.sourceline) for c in children])
                err(filename, obj, "too many sub-elements"
                    ", expected single <child internal-child='accessible'>"
                    ": lines %s" % lines)
            continue

        # Case 2: has an <accessibility> sub-element with a "labelled-by"
        # <relation> pointing to an existing element.
        if len(labelled_by) > 0:
            continue

        # TODO: after a few more checks and false-positives filtering, warn
        # that this does not have a label


def usage():
    print("%s [-W error|none] [file ...]" % progname,
          file=sys.stderr)
    sys.exit(2)


def main():
    global Werror, Wnone, errors

    try:
        opts, args = getopt.getopt(sys.argv[1:], "pW:")
    except getopt.GetoptError:
        usage()

    for o, a in opts:
        if o == "-W":
            if a == "error":
                Werror = True
            elif a == "none":
                Wnone = True

    for filename in args:
        try:
            tree = ET.parse(filename)
        except ET.ParseError:
            err(filename, None, "malformatted xml file")
        except IOError:
            err(filename, None, "unable to read file")

        try:
            check_a11y_relation(filename, tree.getroot())
        except Exception as error:
            import traceback
            traceback.print_exc()
            err(filename, None, "error parsing file")


    if errors > 0:
        sys.exit(1)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass

# vim: set shiftwidth=4 softtabstop=4 expandtab: