Version 24.8.3.2, tag libreoffice-24.8.3.2
[LibreOffice.git] / bin / lint-ui.py
blobc04eba63bb76b442de1f33ddd9b6d2d8f4d60282
1 #!/usr/bin/env python3
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, you can obtain one at http://mozilla.org/MPL/2.0/.
9 # Takes a LibreOffice .ui file and provides linting tips for maintaining
10 # a consistent look for dialogs
12 import sys
13 # Force python XML parser not faster C accelerators
14 # because we can't hook the C implementation
15 sys.modules['_elementtree'] = None
16 import xml.etree.ElementTree as ET
17 import re
19 DEFAULT_WARNING_STR = 'Lint assertion failed'
21 POSSIBLE_TOP_LEVEL_WIDGETS = ['GtkDialog', 'GtkMessageDialog', 'GtkBox', 'GtkFrame', 'GtkGrid',
22 'GtkAssistant', 'GtkToolbar', 'GtkNotebook', 'GtkPopover', 'GtkWindow', 'GtkPaned', 'GtkScrolledWindow']
23 IGNORED_TOP_LEVEL_WIDGETS = ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer', 'GtkTreeStore']
24 BORDER_WIDTH = '6'
25 BUTTON_BOX_SPACING = '12'
26 ALIGNMENT_TOP_PADDING = '6'
27 #https://developer.gnome.org/hig-book/3.0/windows-alert.html.en#alert-spacing
28 MESSAGE_BOX_SPACING = '24'
29 MESSAGE_BORDER_WIDTH = '12'
31 IGNORED_WORDS = ['the', 'of', 'to', 'for', 'a', 'and', 'as', 'from', 'on', 'into', 'by', 'at', 'or', 'do', 'in', 'when', 'no']
33 # Hook the XML parser and add line number attributes
34 class LineNumberingParser(ET.XMLParser):
35 def _start(self, *args, **kwargs):
36 # Here we assume the default XML parser which is expat
37 # and copy its element position attributes into output Elements
38 element = super(self.__class__, self)._start(*args, **kwargs)
39 element._start_line_number = self.parser.CurrentLineNumber
40 element._start_column_number = self.parser.CurrentColumnNumber
41 element._start_byte_index = self.parser.CurrentByteIndex
42 return element
44 def _end(self, *args, **kwargs):
45 element = super(self.__class__, self)._end(*args, **kwargs)
46 element._end_line_number = self.parser.CurrentLineNumber
47 element._end_column_number = self.parser.CurrentColumnNumber
48 element._end_byte_index = self.parser.CurrentByteIndex
49 return element
52 def lint_assert(predicate, warning=DEFAULT_WARNING_STR, node=None):
53 if not predicate:
54 if not(node is None):
55 print(sys.argv[1] + ":" + str(node._start_line_number) + ": " + warning)
56 else:
57 print(sys.argv[1] + ": " + warning)
59 def check_top_level_widget(element):
60 # check widget type
61 widget_type = element.attrib['class']
62 if not(widget_type in POSSIBLE_TOP_LEVEL_WIDGETS):
63 return
65 # check border_width property
66 border_width_properties = element.findall("property[@name='border_width']")
67 # TODO reenable when we are ready to fix
68 #if len(border_width_properties) < 1:
69 # lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH)
70 #if len(border_width_properties) == 1:
71 # border_width = border_width_properties[0]
72 # if widget_type == "GtkMessageDialog":
73 # lint_assert(border_width.text == MESSAGE_BORDER_WIDTH,
74 # "Top level 'border_width' property should be " + MESSAGE_BORDER_WIDTH, border_width)
75 # else:
76 # lint_assert(border_width.text == BORDER_WIDTH,
77 # "Top level 'border_width' property should be " + BORDER_WIDTH, border_width)
79 # check that any widget which has 'has-default' also has 'can-default'
80 for widget in element.findall('.//object'):
81 if not widget.attrib['class']:
82 continue
83 widget_type = widget.attrib['class']
84 has_defaults = widget.findall("./property[@name='has_default']")
85 if len(has_defaults) > 0 and has_defaults[0].text == "True":
86 can_defaults = widget.findall("./property[@name='can_default']")
87 lint_assert(len(can_defaults)>0 and can_defaults[0].text == "True",
88 "has_default without can_default in " + widget_type + " with id = '" + widget.attrib['id'] + "'", widget)
90 def check_button_box_spacing(element):
91 spacing = element.findall("property[@name='spacing']")
92 lint_assert(len(spacing) > 0 and spacing[0].text == BUTTON_BOX_SPACING,
93 "Button box 'spacing' should be " + BUTTON_BOX_SPACING,
94 element)
96 def check_message_box_spacing(element):
97 spacing = element.findall("property[@name='spacing']")
98 lint_assert(len(spacing) > 0 and spacing[0].text == MESSAGE_BOX_SPACING,
99 "Message box 'spacing' should be " + MESSAGE_BOX_SPACING,
100 element)
102 def check_radio_buttons(root):
103 radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkRadioButton']
104 for radio in radios:
105 radio_underlines = radio.findall("./property[@name='use_underline']")
106 assert len(radio_underlines) <= 1
107 if len(radio_underlines) < 1:
108 lint_assert(False, "No use_underline in GtkRadioButton with id = '" + radio.attrib['id'] + "'", radio)
110 def check_adjustments(root):
111 adjustments = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkAdjustment']
112 for adjustment in adjustments:
113 uppers = adjustment.findall("./property[@name='upper']")
114 assert len(uppers) <= 1
115 if len(uppers) < 1:
116 lint_assert(False, "No upper in GtkAdjustment with id = '" + adjustment.attrib['id'] + "'", adjustment)
118 def check_menu_buttons(root):
119 buttons = [element for element in root.findall('.//object') if element.attrib['class'] == "GtkMenuButton"]
120 for button in buttons:
121 labels = button.findall("./property[@name='label']")
122 images = button.findall("./property[@name='image']")
123 assert(len(labels) <= 1)
124 if len(labels) < 1 and len(images) < 1:
125 if sys.argv[1] == "vcl/uiconfig/ui/combobox.ui" and button.attrib['id'] == "overlaybutton":
126 pass
127 else:
128 lint_assert(False, "No label in GtkMenuButton with id = '" + button.attrib['id'] + "'", button)
130 def check_check_buttons(root):
131 radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkCheckButton']
132 for radio in radios:
133 radio_underlines = radio.findall("./property[@name='use_underline']")
134 assert len(radio_underlines) <= 1
135 if len(radio_underlines) < 1:
136 lint_assert(False, "No use_underline in GtkCheckButton with id = '" + radio.attrib['id'] + "'", radio)
138 def check_frames(root):
139 frames = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkFrame']
140 for frame in frames:
141 frame_alignments = frame.findall("./child/object[@class='GtkAlignment']")
142 if len(frame_alignments) > 0:
143 lint_assert(False, "Deprecated GtkAlignment in GtkFrame with id = '" + frame.attrib['id'] + "'", frame)
145 def check_title_labels(root):
146 labels = root.findall(".//child[@type='label']")
147 for label in labels:
148 title = label.find(".//property[@name='label']")
149 if title is None:
150 continue
151 words = re.split(r'[^a-zA-Z0-9:_-]', title.text)
152 first = True
153 for word in words:
154 if len(word) and word[0].islower() and (word not in IGNORED_WORDS) and not first:
155 context = title.attrib['context']
156 # exclude a couple of whole sentences
157 if sys.argv[1] == "cui/uiconfig/ui/optpathspage.ui" and context == "optpathspage|label1":
158 pass
159 elif sys.argv[1] == "dbaccess/uiconfig/ui/password.ui" and context == "password|label1":
160 pass
161 elif sys.argv[1] == "sc/uiconfig/scalc/ui/datastreams.ui" and context == "datastreams|label4":
162 pass
163 elif sys.argv[1] == "sc/uiconfig/scalc/ui/scgeneralpage.ui" and context == "scgeneralpage|label6":
164 pass
165 elif sys.argv[1] == "sfx2/uiconfig/ui/documentfontspage.ui" and context == "documentfontspage|fontScriptFrameLabel":
166 pass
167 elif sys.argv[1] == "sw/uiconfig/swriter/ui/testmailsettings.ui" and context == "testmailsettings|label8":
168 pass
169 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcomparison.ui" and context == "optcomparison|setting":
170 pass
171 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcompatpage.ui" and context == "optcompatpage|label11":
172 pass
173 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcaptionpage.ui" and context == "optcaptionpage|label1":
174 pass
175 elif sys.argv[1] == "sw/uiconfig/swriter/ui/mmresultemaildialog.ui" and context == "mmresultemaildialog|attachft":
176 pass
177 elif sys.argv[1] == "sw/uiconfig/swriter/ui/mailmerge.ui" and context == "mailmerge|singledocument":
178 pass
179 elif sys.argv[1] == "cui/uiconfig/ui/acorexceptpage.ui" and context == "acorexceptpage|label2":
180 pass
181 elif sys.argv[1] == "dbaccess/uiconfig/ui/dbwizmysqlintropage.ui" and context == "dbwizmysqlintropage|label1":
182 pass
183 else:
184 lint_assert(False, "The word '" + word + "' should be capitalized", label)
185 first = False
187 def main():
188 tree = ET.parse(sys.argv[1], parser=LineNumberingParser())
189 root = tree.getroot()
191 if sys.argv[1] != "libreofficekit/qa/gtktiledviewer/gtv.ui":
192 lint_assert('domain' in root.attrib, "interface needs to specify translation domain")
194 top_level_widgets = [element for element in root.findall('object') if element.attrib['class'] not in IGNORED_TOP_LEVEL_WIDGETS]
195 # eg. one file contains only a Menu, which we don't check
196 if len(top_level_widgets) == 0:
197 return
199 for top_level_widget in top_level_widgets:
200 check_top_level_widget(top_level_widget)
202 # TODO - only do this if we have a GtkDialog?
203 # check button box spacing
204 button_box = top_level_widget.findall("./child/object[@id='dialog-vbox1']")
205 if len(button_box) > 0:
206 element = button_box[0]
207 # TODO reenable when we are ready to fix
208 #check_button_box_spacing(element)
210 message_box = top_level_widget.findall("./child/object[@id='messagedialog-vbox']")
211 if len(message_box) > 0:
212 element = message_box[0]
213 # TODO reenable when we are ready to fix
214 #check_message_box_spacing(element)
216 check_frames(root)
218 # TODO reenable when we are ready to fix
219 #check_radio_buttons(root)
221 check_menu_buttons(root)
223 # TODO reenable when we are ready to fix
224 #check_check_buttons(root)
226 check_title_labels(root)
228 if __name__ == "__main__":
229 main()