]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/tests/check_properties.py
Executable scripts
[working/Evergreen.git] / build / i18n / tests / check_properties.py
1 #!/usr/bin/env python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008  Laurentian University
4 # Dan Scott <dscott@laurentian.ca>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
16
17 # vim:et:sw=4:ts=4: set fileencoding=utf-8 :
18
19 """
20 Parse i18n properties files and XUL / JavaScript files looking for trouble
21     * Invalid strings
22     * Unused strings
23     * Missing strings
24 """
25
26 import os
27 import re
28
29 DEBUG = False
30
31 PROP_DIRS = (
32         '../../../Open-ILS/xul/staff_client/server/locale/en-US/',
33         '../../../Open-ILS/xul/staff_client/chrome/locale/en-US/'
34         )
35
36 XUL_DIRS = (
37         '../../../Open-ILS/xul/staff_client/server/',
38         '../../../Open-ILS/xul/staff_client/chrome/',
39         )
40
41 def parse_properties():
42     """
43     Parse the properties files in known places
44     """
45
46     basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
47
48     properties = {}
49
50     prop_files = []
51
52     for p_dir in PROP_DIRS:
53         p_dir = os.path.normpath(os.path.join(basedir, p_dir))
54         file_list = os.listdir(p_dir)
55         for p_file in file_list:
56             if os.path.splitext(p_file)[1] == '.properties':
57                 prop_files.append(os.path.join(p_dir, p_file))
58
59     prefix = os.path.commonprefix(prop_files)
60
61     for p_file in prop_files:
62
63         # Get the shortest unique address for this file
64         short_pf = p_file[len(prefix):]
65
66         prop_file = open(p_file, 'r')
67
68         line_num = 1
69
70         for line in prop_file:
71             line_num += 1
72
73             # Get rid of trailing linefeed
74             line = line[0:-1]
75
76             # Skip comments
77             if not line or line[0] == '#':
78                 continue
79
80             # Split property/value on first = sign
81             unpack = re.split('=', line, 1)
82
83             # If a line doesn't have an = sign, is that okay (run-on from previous?) or illegal?
84             # I think it's illegal
85             if len(unpack) != 2:
86                 print("%s:%d: No property in line [%s]" % (short_pf, line_num, line))
87                 continue
88
89             prop_key, value = unpack
90
91             if not properties.has_key(prop_key):
92                 properties[prop_key] = [{'value': value, 'file': short_pf}]
93                 continue
94
95             for entry in properties[prop_key]:
96                 if ['file'] == short_pf:
97                     print("%s:%d: Duplicate key '%s' in line [%s]" % (short_pf, line_num, prop_key, line[0:-1]))
98                     continue
99
100             properties[prop_key].append({'value': value, 'file': short_pf})
101
102         prop_file.close()
103
104     return properties
105
106 def check_xul_files(props):
107     """
108     Finds all the XUL and JavaScript files
109     """
110
111     basedir = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
112
113     xul_files = []
114
115     for x_dir in XUL_DIRS:
116         for root, dirs, files in os.walk(x_dir):
117             for x_file in files:
118                 if os.path.splitext(x_file)[1] == '.xul' or os.path.splitext(x_file)[1] == '.js':
119                     check_xul(root, x_file, props)
120
121 def check_xul(root, filename, props):
122     """
123     Parse all getString() and getFormattedString() calls in XUL and JavaScript
124     files to ensure:
125       * that the requested property exists
126       * that every property is actually required
127     """
128
129     num_strings = 0
130
131     # Typical example of a getString request:
132     # document.getElementById('catStrings').getString('staff.cat.bib_brief.deleted')
133     strings = re.compile(r'''\(\s*?(['"])([^'"]+?)Strings\1\s*?\)\.getString\(\s*?(['"])([^'"]+?)\3\s*?\)''')
134
135     # Typical example of a getFormattedString request:
136     # document.getElementById('catStrings').getFormattedString('staff.cat.bib_brief.record_id', [docid])
137     formed_strings = re.compile(r'''\(\s*?(['"])([^'"]+?)Strings\1\s*?\)\.getFormattedString\(\s*?(['"])([^'"]+?)\3\s*?,\s*\[(.+?)\]\s*\)\)''')
138
139     xul = open(os.path.join(root, filename), 'r')
140     content = xul.read()
141     xul.close()
142
143     if DEBUG:
144         print "File: %s" % (os.path.normpath(os.path.join(root, filename)))
145
146     for s_match in strings.finditer(content):
147         num_strings += 1
148         #print "\tStringset: %s ID: %s" % (s_match.group(2), s_match.group(4))
149         if not props.has_key(s_match.group(4)):
150             print "File: %s" % (os.path.normpath(os.path.join(root, filename)))
151             print "\tID %s not found, expected in %sStrings" % (s_match.group(4), s_match.group(2))
152
153     for s_match in formed_strings.finditer(content):
154         num_strings += 1
155         #print "\tStringset: %s ID: %s, data: %s" % (s_match.group(2), s_match.group(4), s_match.group(5))
156         if not props.has_key(s_match.group(4)):
157             print "File: %s" % (os.path.normpath(os.path.join(root, filename)))
158             print "\tID %s not found, expected in %sStrings" % (s_match.group(4), s_match.group(2))
159
160     if DEBUG:
161         print "\t%d i18n calls found" % (num_strings)
162
163 if __name__ == '__main__':
164     props = parse_properties() 
165     check_xul_files(props)