Updated core
[LibreOffice.git] / bin / lint-ui.py
blob0c8a9ac15ae1c456d32675169a140579fd0028c5
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 import xml.etree.ElementTree as ET
15 DEFAULT_WARNING_STR = 'Lint assertion failed'
17 POSSIBLE_TOP_LEVEL_WIDGETS = ['GtkDialog', 'GtkBox', 'GtkFrame', 'GtkGrid']
18 IGNORED_TOP_LEVEL_WIDGETS = ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer']
19 BORDER_WIDTH = '6'
20 BUTTON_BOX_SPACING = '12'
21 ALIGNMENT_TOP_PADDING = '6'
23 def lint_assert(predicate, warning=DEFAULT_WARNING_STR):
24 if not predicate:
25 print(" * " + warning)
27 def check_top_level_widget(element):
28 # check widget type
29 widget_type = element.attrib['class']
30 lint_assert(widget_type in POSSIBLE_TOP_LEVEL_WIDGETS,
31 "Top level widget should be 'GtkDialog', 'GtkFrame', 'GtkBox', or 'GtkGrid'")
33 # check border_width property
34 border_width_properties = element.findall("property[@name='border_width']")
35 if len(border_width_properties) < 1:
36 lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH)
37 if len(border_width_properties) == 1:
38 border_width = border_width_properties[0]
39 lint_assert(border_width.text == BORDER_WIDTH,
40 "Top level 'border_width' property should be " + BORDER_WIDTH)
42 def check_button_box_spacing(element):
43 spacing = element.findall("property[@name='spacing']")[0]
44 lint_assert(spacing.text == BUTTON_BOX_SPACING,
45 "Button box 'spacing' should be " + BUTTON_BOX_SPACING)
47 def check_frames(root):
48 frames = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkFrame']
49 for frame in frames:
50 frame_alignments = frame.findall("./child/object[@class='GtkAlignment']")
51 assert len(frame_alignments) <= 1
52 if len(frame_alignments) < 1:
53 lint_assert(False, "No GtkAlignment in GtkFrame with id = '" + frame.attrib['id'] + "'")
54 if len(frame_alignments) == 1:
55 alignment = frame_alignments[0]
56 check_alignment_top_padding(alignment)
58 def check_alignment_top_padding(alignment):
59 top_padding_properties = alignment.findall("./property[@name='top_padding']")
60 assert len(top_padding_properties) <= 1
61 if len(top_padding_properties) < 1:
62 lint_assert(False, "No GtkAlignment 'top_padding' set. Should probably be " + ALIGNMENT_TOP_PADDING)
63 if len(top_padding_properties) == 1:
64 top_padding = top_padding_properties[0]
65 lint_assert(top_padding.text == ALIGNMENT_TOP_PADDING,
66 "GtkAlignment 'top_padding' should be " + ALIGNMENT_TOP_PADDING)
68 def main():
69 print(" == " + sys.argv[1] + " ==")
70 tree = ET.parse(sys.argv[1])
71 root = tree.getroot()
73 top_level_widgets = [element for element in root.findall('object') if element.attrib['class'] not in IGNORED_TOP_LEVEL_WIDGETS]
74 assert len(top_level_widgets) == 1
76 top_level_widget = top_level_widgets[0]
77 check_top_level_widget(top_level_widget)
79 # TODO - only do this if we have a GtkDialog?
80 # check button box spacing
81 button_box = top_level_widget.findall("child[@id='dialog-vbox1']")
82 if len(button_box) > 0:
83 element = button_box[0]
84 check_button_box_spacing(element)
86 check_frames(root)
88 if __name__ == "__main__":
89 main()