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
13 import xml
.etree
.ElementTree
as ET
16 DEFAULT_WARNING_STR
= 'Lint assertion failed'
18 POSSIBLE_TOP_LEVEL_WIDGETS
= ['GtkDialog', 'GtkMessageDialog', 'GtkBox', 'GtkFrame', 'GtkGrid']
19 IGNORED_TOP_LEVEL_WIDGETS
= ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer']
21 BUTTON_BOX_SPACING
= '12'
22 ALIGNMENT_TOP_PADDING
= '6'
23 #https://developer.gnome.org/hig-book/3.0/windows-alert.html.en#alert-spacing
24 MESSAGE_BOX_SPACING
= '24'
25 MESSAGE_BORDER_WIDTH
= '12'
27 IGNORED_WORDS
= ['the', 'of', 'to', 'for', 'a', 'and', 'as', 'from', 'on', 'into', 'by', 'at', 'or', 'do', 'in', 'when']
29 def lint_assert(predicate
, warning
=DEFAULT_WARNING_STR
):
31 print(" * " + warning
)
33 def check_top_level_widget(element
):
35 widget_type
= element
.attrib
['class']
36 lint_assert(widget_type
in POSSIBLE_TOP_LEVEL_WIDGETS
,
37 "Top level widget should be 'GtkDialog', 'GtkFrame', 'GtkBox', or 'GtkGrid'")
39 # check border_width property
40 border_width_properties
= element
.findall("property[@name='border_width']")
41 if len(border_width_properties
) < 1:
42 lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH
)
43 if len(border_width_properties
) == 1:
44 border_width
= border_width_properties
[0]
45 if widget_type
== "GtkMessageDialog":
46 lint_assert(border_width
.text
== MESSAGE_BORDER_WIDTH
,
47 "Top level 'border_width' property should be " + MESSAGE_BORDER_WIDTH
)
49 lint_assert(border_width
.text
== BORDER_WIDTH
,
50 "Top level 'border_width' property should be " + BORDER_WIDTH
)
52 def check_button_box_spacing(element
):
53 spacing
= element
.findall("property[@name='spacing']")[0]
54 lint_assert(spacing
.text
== BUTTON_BOX_SPACING
,
55 "Button box 'spacing' should be " + BUTTON_BOX_SPACING
)
57 def check_message_box_spacing(element
):
58 spacing
= element
.findall("property[@name='spacing']")[0]
59 lint_assert(spacing
.text
== MESSAGE_BOX_SPACING
,
60 "Button box 'spacing' should be " + MESSAGE_BOX_SPACING
)
62 def check_radio_buttons(root
):
63 radios
= [element
for element
in root
.findall('.//object') if element
.attrib
['class'] == 'GtkRadioButton']
65 radio_underlines
= radio
.findall("./property[@name='use_underline']")
66 assert len(radio_underlines
) <= 1
67 if len(radio_underlines
) < 1:
68 lint_assert(False, "No use_underline in GtkRadioButton with id = '" + radio
.attrib
['id'] + "'")
70 def check_check_buttons(root
):
71 radios
= [element
for element
in root
.findall('.//object') if element
.attrib
['class'] == 'GtkCheckButton']
73 radio_underlines
= radio
.findall("./property[@name='use_underline']")
74 assert len(radio_underlines
) <= 1
75 if len(radio_underlines
) < 1:
76 lint_assert(False, "No use_underline in GtkCheckButton with id = '" + radio
.attrib
['id'] + "'")
79 def check_frames(root
):
80 frames
= [element
for element
in root
.findall('.//object') if element
.attrib
['class'] == 'GtkFrame']
82 frame_alignments
= frame
.findall("./child/object[@class='GtkAlignment']")
83 assert len(frame_alignments
) <= 1
84 if len(frame_alignments
) < 1:
85 lint_assert(False, "No GtkAlignment in GtkFrame with id = '" + frame
.attrib
['id'] + "'")
86 if len(frame_alignments
) == 1:
87 alignment
= frame_alignments
[0]
88 check_alignment_top_padding(alignment
)
90 def check_alignment_top_padding(alignment
):
91 top_padding_properties
= alignment
.findall("./property[@name='top_padding']")
92 assert len(top_padding_properties
) <= 1
93 if len(top_padding_properties
) < 1:
94 lint_assert(False, "No GtkAlignment 'top_padding' set. Should probably be " + ALIGNMENT_TOP_PADDING
)
95 if len(top_padding_properties
) == 1:
96 top_padding
= top_padding_properties
[0]
97 lint_assert(top_padding
.text
== ALIGNMENT_TOP_PADDING
,
98 "GtkAlignment 'top_padding' should be " + ALIGNMENT_TOP_PADDING
)
100 def check_title_labels(root
):
101 labels
= root
.findall(".//child[@type='label']")
102 titles
= [label
.find(".//property[@name='label']") for label
in labels
]
106 words
= re
.split(r
'[^a-zA-Z0-9:_-]', title
.text
)
109 if word
[0].islower() and (word
not in IGNORED_WORDS
or first
):
110 lint_assert(False, "The word '" + word
+ "' should be capitalized")
114 print(" == " + sys
.argv
[1] + " ==")
115 tree
= ET
.parse(sys
.argv
[1])
116 root
= tree
.getroot()
118 lint_assert('domain' in root
.attrib
, "interface needs to specific translation domain")
120 top_level_widgets
= [element
for element
in root
.findall('object') if element
.attrib
['class'] not in IGNORED_TOP_LEVEL_WIDGETS
]
121 assert len(top_level_widgets
) == 1
123 top_level_widget
= top_level_widgets
[0]
124 check_top_level_widget(top_level_widget
)
126 # TODO - only do this if we have a GtkDialog?
127 # check button box spacing
128 button_box
= top_level_widget
.findall("./child/object[@id='dialog-vbox1']")
129 if len(button_box
) > 0:
130 element
= button_box
[0]
131 check_button_box_spacing(element
)
133 message_box
= top_level_widget
.findall("./child/object[@id='messagedialog-vbox']")
134 if len(message_box
) > 0:
135 element
= message_box
[0]
136 check_message_box_spacing(element
)
140 check_radio_buttons(root
)
142 check_check_buttons(root
)
144 check_title_labels(root
)
146 if __name__
== "__main__":