summaryrefslogtreecommitdiff
path: root/bin/lint-ui.py
blob: fd578f72694d63d419a2730cb908ab4b417f6ca6 (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
#!/usr/bin/env python3
#
# 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/.
#
# Takes a LibreOffice .ui file and provides linting tips for maintaining
# a consistent look for dialogs

import sys
# Force python XML parser not faster C accelerators
# because we can't hook the C implementation
sys.modules['_elementtree'] = None
import xml.etree.ElementTree as ET
import re

DEFAULT_WARNING_STR = 'Lint assertion failed'

POSSIBLE_TOP_LEVEL_WIDGETS = ['GtkDialog', 'GtkMessageDialog', 'GtkBox', 'GtkFrame', 'GtkGrid', 'GtkAssistant']
IGNORED_TOP_LEVEL_WIDGETS = ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer', 'GtkTreeStore']
BORDER_WIDTH = '6'
BUTTON_BOX_SPACING = '12'
ALIGNMENT_TOP_PADDING = '6'
#https://developer.gnome.org/hig-book/3.0/windows-alert.html.en#alert-spacing
MESSAGE_BOX_SPACING = '24'
MESSAGE_BORDER_WIDTH = '12'

IGNORED_WORDS = ['the', 'of', 'to', 'for', 'a', 'and', 'as', 'from', 'on', 'into', 'by', 'at', 'or', 'do', 'in', 'when']

# Hook the XML parser and add line number attributes
class LineNumberingParser(ET.XMLParser):
    def _start(self, *args, **kwargs):
        # Here we assume the default XML parser which is expat
        # and copy its element position attributes into output Elements
        element = super(self.__class__, self)._start(*args, **kwargs)
        element._start_line_number = self.parser.CurrentLineNumber
        element._start_column_number = self.parser.CurrentColumnNumber
        element._start_byte_index = self.parser.CurrentByteIndex
        return element

    def _end(self, *args, **kwargs):
        element = super(self.__class__, self)._end(*args, **kwargs)
        element._end_line_number = self.parser.CurrentLineNumber
        element._end_column_number = self.parser.CurrentColumnNumber
        element._end_byte_index = self.parser.CurrentByteIndex
        return element


def lint_assert(predicate, warning=DEFAULT_WARNING_STR, node=None):
    if not predicate:
        if not(node is None):
            print(sys.argv[1] + ":" + str(node._start_line_number) + ": " + warning)
        else:
            print(sys.argv[1] + ": " + warning)

def check_top_level_widget(element):
    # check widget type
    widget_type = element.attrib['class']
    lint_assert(widget_type in POSSIBLE_TOP_LEVEL_WIDGETS,
                "Top level widget should be 'GtkDialog', 'GtkFrame', 'GtkBox', or 'GtkGrid', but is " + widget_type)

    # check border_width property
    border_width_properties = element.findall("property[@name='border_width']")
    # This one fires so often I don't think it's useful
    #if len(border_width_properties) < 1:
    #    lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH)
    if len(border_width_properties) == 1:
        border_width = border_width_properties[0]
        if widget_type == "GtkMessageDialog":
            lint_assert(border_width.text == MESSAGE_BORDER_WIDTH,
                        "Top level 'border_width' property should be " + MESSAGE_BORDER_WIDTH, border_width)
        else:
            lint_assert(border_width.text == BORDER_WIDTH,
                        "Top level 'border_width' property should be " + BORDER_WIDTH, border_width)

def check_button_box_spacing(element):
    spacing = element.findall("property[@name='spacing']")
    lint_assert(len(spacing) > 0 and spacing[0].text == BUTTON_BOX_SPACING,
                "Button box 'spacing' should be " + BUTTON_BOX_SPACING,
                element)

def check_message_box_spacing(element):
    spacing = element.findall("property[@name='spacing']")
    lint_assert(len(spacing) > 0 and spacing[0].text == MESSAGE_BOX_SPACING,
                "Button box 'spacing' should be " + MESSAGE_BOX_SPACING,
                element)

def check_radio_buttons(root):
    radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkRadioButton']
    for radio in radios:
        radio_underlines = radio.findall("./property[@name='use_underline']")
        assert len(radio_underlines) <= 1
        if len(radio_underlines) < 1:
            lint_assert(False, "No use_underline in GtkRadioButton with id = '" + radio.attrib['id'] + "'", radio)

def check_menu_buttons(root):
    buttons = [element for element in root.findall('.//object') if element.attrib['class'] == "GtkMenuButton"]
    for button in buttons:
        labels = button.findall("./property[@name='label']")
        images = button.findall("./property[@name='image']")
        assert(len(labels) <= 1)
        if len(labels) < 1 and len(images) < 1:
            lint_assert(False, "No label in GtkMenuButton with id = '" + button.attrib['id'] + "'", button)

def check_check_buttons(root):
    radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkCheckButton']
    for radio in radios:
        radio_underlines = radio.findall("./property[@name='use_underline']")
        assert len(radio_underlines) <= 1
        if len(radio_underlines) < 1:
            lint_assert(False, "No use_underline in GtkCheckButton with id = '" + radio.attrib['id'] + "'", radio)


def check_frames(root):
    frames = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkFrame']
    for frame in frames:
        frame_alignments = frame.findall("./child/object[@class='GtkAlignment']")
        assert len(frame_alignments) <= 1
        if len(frame_alignments) < 1:
            lint_assert(False, "No GtkAlignment in GtkFrame with id = '" + frame.attrib['id'] + "'", frame)
        if len(frame_alignments) == 1:
            alignment = frame_alignments[0]
            check_alignment_top_padding(alignment)

def check_alignment_top_padding(alignment):
    top_padding_properties = alignment.findall("./property[@name='top_padding']")
    assert len(top_padding_properties) <= 1
    if len(top_padding_properties) < 1:
        lint_assert(False, "No GtkAlignment 'top_padding' set. Should probably be " + ALIGNMENT_TOP_PADDING, alignment)
    if len(top_padding_properties) == 1:
        top_padding = top_padding_properties[0]
        lint_assert(top_padding.text == ALIGNMENT_TOP_PADDING,
                    "GtkAlignment 'top_padding' should be " + ALIGNMENT_TOP_PADDING, alignment)

def check_title_labels(root):
    labels = root.findall(".//child[@type='label']")
    for label in labels:
        title = label.find(".//property[@name='label']")
        if title is None:
            continue
        words = re.split(r'[^a-zA-Z0-9:_-]', title.text)
        first = True
        for word in words:
            if len(word) and word[0].islower() and (word not in IGNORED_WORDS or first):
                lint_assert(False, "The word '" + word + "' should be capitalized", label)
            first = False

def main():
    tree = ET.parse(sys.argv[1], parser=LineNumberingParser())
    root = tree.getroot()

    lint_assert('domain' in root.attrib, "interface needs to specific translation domain")

    top_level_widgets = [element for element in root.findall('object') if element.attrib['class'] not in IGNORED_TOP_LEVEL_WIDGETS]
    lint_assert( len(top_level_widgets) <= 1, "should be only one top-level widget for us to analyze, found " + str(len(top_level_widgets)))
    if len(top_level_widgets) > 1:
        return
    # eg. one file contains only a Menu, which we don't check
    if len(top_level_widgets) == 0:
        return

    top_level_widget = top_level_widgets[0]
    check_top_level_widget(top_level_widget)

    # TODO - only do this if we have a GtkDialog?
    # check button box spacing
    button_box = top_level_widget.findall("./child/object[@id='dialog-vbox1']")
    if len(button_box) > 0:
        element = button_box[0]
        check_button_box_spacing(element)

    message_box = top_level_widget.findall("./child/object[@id='messagedialog-vbox']")
    if len(message_box) > 0:
        element = message_box[0]
        check_message_box_spacing(element)

    check_frames(root)

    check_radio_buttons(root)

    check_menu_buttons(root)

    check_check_buttons(root)

    check_title_labels(root)

if __name__ == "__main__":
    main()