Mercurial > babel > old > babel-test
annotate babel/messages/checkers.py @ 488:07a27065102a stable-0.9.x
Merged revisions 458 via svnmerge from
http://svn.edgewall.org/repos/babel/trunk
........
r458 | aronacher | 2008-10-31 17:13:59 +0100 (vr, 31 okt 2008) | 3 lines
Removed `ValueError` raising for string formatting message checkers if the string does not contain any string formattings. The new behavior is undefined. This fixes at least one of the problems of the #150 ticket.
........
author | jruigrok |
---|---|
date | Sun, 11 Apr 2010 09:10:49 +0000 |
parents | c2ae38340540 |
children | b29ff192b610 |
rev | line source |
---|---|
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
2 # |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
5 # |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://babel.edgewall.org/wiki/License. |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
9 # |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://babel.edgewall.org/log/. |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
13 |
234 | 14 """Various routines that help with validation of translations. |
15 | |
16 :since: version 0.9 | |
17 """ | |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
18 |
369 | 19 from itertools import izip |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
20 from babel.messages.catalog import TranslationError, PYTHON_FORMAT |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
21 |
369 | 22 #: list of format chars that are compatible to each other |
23 _string_format_compatibilities = [ | |
24 set(['i', 'd', 'u']), | |
25 set(['x', 'X']), | |
26 set(['f', 'F', 'g', 'G']) | |
27 ] | |
28 | |
29 | |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
30 def num_plurals(catalog, message): |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
31 """Verify the number of plurals in the translation.""" |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
32 if not message.pluralizable: |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
33 if not isinstance(message.string, basestring): |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
34 raise TranslationError("Found plural forms for non-pluralizable " |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
35 "message") |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
36 return |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
37 |
369 | 38 # skip further tests if no catalog is provided. |
39 elif catalog is None: | |
40 return | |
41 | |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
42 msgstrs = message.string |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
43 if not isinstance(msgstrs, (list, tuple)): |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
44 msgstrs = (msgstrs,) |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
45 if len(msgstrs) != catalog.num_plurals: |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
46 raise TranslationError("Wrong number of plural forms (expected %d)" % |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
47 catalog.num_plurals) |
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
48 |
369 | 49 |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
50 def python_format(catalog, message): |
369 | 51 """Verify the format string placeholders in the translation.""" |
52 if 'python-format' not in message.flags: | |
53 return | |
54 msgids = message.id | |
55 if not isinstance(msgids, (list, tuple)): | |
56 msgids = (msgids,) | |
57 msgstrs = message.string | |
58 if not isinstance(msgstrs, (list, tuple)): | |
59 msgstrs = (msgstrs,) | |
60 | |
61 for msgid, msgstr in izip(msgids, msgstrs): | |
62 if msgstr: | |
63 _validate_format(msgid, msgstr) | |
64 | |
65 | |
66 def _validate_format(format, alternative): | |
67 """Test format string `alternative` against `format`. `format` can be the | |
68 msgid of a message and `alternative` one of the `msgstr`\s. The two | |
69 arguments are not interchangeable as `alternative` may contain less | |
70 placeholders if `format` uses named placeholders. | |
71 | |
488 | 72 The behavior of this function is undefined if the string does not use |
73 string formattings. | |
369 | 74 |
75 If the string formatting of `alternative` is compatible to `format` the | |
76 function returns `None`, otherwise a `TranslationError` is raised. | |
77 | |
78 Examples for compatible format strings: | |
79 | |
80 >>> _validate_format('Hello %s!', 'Hallo %s!') | |
81 >>> _validate_format('Hello %i!', 'Hallo %d!') | |
82 | |
83 Example for an incompatible format strings: | |
84 | |
85 >>> _validate_format('Hello %(name)s!', 'Hallo %s!') | |
86 Traceback (most recent call last): | |
87 ... | |
88 TranslationError: the format strings are of different kinds | |
89 | |
90 This function is used by the `python_format` checker. | |
91 | |
92 :param format: The original format string | |
93 :param alternative: The alternative format string that should be checked | |
94 against format | |
95 :return: None on success | |
96 :raises TranslationError: on formatting errors | |
97 """ | |
98 | |
99 def _parse(string): | |
100 result = [] | |
101 for match in PYTHON_FORMAT.finditer(string): | |
102 name, format, typechar = match.groups() | |
103 if typechar == '%' and name is None: | |
104 continue | |
105 result.append((name, str(typechar))) | |
106 return result | |
107 | |
108 def _compatible(a, b): | |
109 if a == b: | |
110 return True | |
111 for set in _string_format_compatibilities: | |
112 if a in set and b in set: | |
113 return True | |
114 return False | |
115 | |
116 def _check_positional(results): | |
117 positional = None | |
118 for name, char in results: | |
119 if positional is None: | |
120 positional = name is None | |
121 else: | |
122 if (name is None) != positional: | |
123 raise TranslationError('format string mixes positional ' | |
124 'and named placeholders') | |
125 return bool(positional) | |
126 | |
127 a, b = map(_parse, (format, alternative)) | |
128 | |
129 # now check if both strings are positional or named | |
130 a_positional, b_positional = map(_check_positional, (a, b)) | |
131 if a_positional and not b_positional and not b: | |
132 raise TranslationError('placeholders are incompatible') | |
133 elif a_positional != b_positional: | |
134 raise TranslationError('the format strings are of different kinds') | |
135 | |
136 # if we are operating on positional strings both must have the | |
137 # same number of format chars and those must be compatible | |
138 if a_positional: | |
139 if len(a) != len(b): | |
140 raise TranslationError('positional format placeholders are ' | |
141 'unbalanced') | |
142 for idx, ((_, first), (_, second)) in enumerate(izip(a, b)): | |
143 if not _compatible(first, second): | |
144 raise TranslationError('incompatible format for placeholder ' | |
145 '%d: %r and %r are not compatible' % | |
146 (idx + 1, first, second)) | |
147 | |
148 # otherwise the second string must not have names the first one | |
149 # doesn't have and the types of those included must be compatible | |
150 else: | |
151 type_map = dict(a) | |
152 for name, typechar in b: | |
153 if name not in type_map: | |
154 raise TranslationError('unknown named placeholder %r' % name) | |
155 elif not _compatible(typechar, type_map[name]): | |
156 raise TranslationError('incompatible format for ' | |
157 'placeholder %r: ' | |
158 '%r and %r are not compatible' % | |
159 (name, typechar, type_map[name])) | |
160 | |
161 | |
162 def _find_checkers(): | |
163 try: | |
164 from pkg_resources import working_set | |
165 except ImportError: | |
166 return [num_plurals, python_format] | |
167 checkers = [] | |
168 for entry_point in working_set.iter_entry_points('babel.checkers'): | |
169 checkers.append(entry_point.load()) | |
170 return checkers | |
171 | |
172 | |
173 checkers = _find_checkers() |