Mercurial > babel > old > babel-test
annotate babel/messages/checkers.py @ 507:b29ff192b610 stable-0.9.x
Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
author | fschwarz |
---|---|
date | Fri, 04 Mar 2011 13:14:03 +0000 |
parents | 07a27065102a |
children |
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 |
507
b29ff192b610
Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
fschwarz
parents:
488
diff
changeset
|
21 from babel.util import set |
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
|
22 |
369 | 23 #: list of format chars that are compatible to each other |
24 _string_format_compatibilities = [ | |
25 set(['i', 'd', 'u']), | |
26 set(['x', 'X']), | |
27 set(['f', 'F', 'g', 'G']) | |
28 ] | |
29 | |
30 | |
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
|
31 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
|
32 """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
|
33 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
|
34 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
|
35 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
|
36 "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
|
37 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
|
38 |
369 | 39 # skip further tests if no catalog is provided. |
40 elif catalog is None: | |
41 return | |
42 | |
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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 |
369 | 50 |
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
|
51 def python_format(catalog, message): |
369 | 52 """Verify the format string placeholders in the translation.""" |
53 if 'python-format' not in message.flags: | |
54 return | |
55 msgids = message.id | |
56 if not isinstance(msgids, (list, tuple)): | |
57 msgids = (msgids,) | |
58 msgstrs = message.string | |
59 if not isinstance(msgstrs, (list, tuple)): | |
60 msgstrs = (msgstrs,) | |
61 | |
62 for msgid, msgstr in izip(msgids, msgstrs): | |
63 if msgstr: | |
64 _validate_format(msgid, msgstr) | |
65 | |
66 | |
67 def _validate_format(format, alternative): | |
68 """Test format string `alternative` against `format`. `format` can be the | |
69 msgid of a message and `alternative` one of the `msgstr`\s. The two | |
70 arguments are not interchangeable as `alternative` may contain less | |
71 placeholders if `format` uses named placeholders. | |
72 | |
488 | 73 The behavior of this function is undefined if the string does not use |
74 string formattings. | |
369 | 75 |
76 If the string formatting of `alternative` is compatible to `format` the | |
77 function returns `None`, otherwise a `TranslationError` is raised. | |
78 | |
79 Examples for compatible format strings: | |
80 | |
81 >>> _validate_format('Hello %s!', 'Hallo %s!') | |
82 >>> _validate_format('Hello %i!', 'Hallo %d!') | |
83 | |
84 Example for an incompatible format strings: | |
85 | |
86 >>> _validate_format('Hello %(name)s!', 'Hallo %s!') | |
87 Traceback (most recent call last): | |
88 ... | |
89 TranslationError: the format strings are of different kinds | |
90 | |
91 This function is used by the `python_format` checker. | |
92 | |
93 :param format: The original format string | |
94 :param alternative: The alternative format string that should be checked | |
95 against format | |
96 :return: None on success | |
97 :raises TranslationError: on formatting errors | |
98 """ | |
99 | |
100 def _parse(string): | |
101 result = [] | |
102 for match in PYTHON_FORMAT.finditer(string): | |
103 name, format, typechar = match.groups() | |
104 if typechar == '%' and name is None: | |
105 continue | |
106 result.append((name, str(typechar))) | |
107 return result | |
108 | |
109 def _compatible(a, b): | |
110 if a == b: | |
111 return True | |
112 for set in _string_format_compatibilities: | |
113 if a in set and b in set: | |
114 return True | |
115 return False | |
116 | |
117 def _check_positional(results): | |
118 positional = None | |
119 for name, char in results: | |
120 if positional is None: | |
121 positional = name is None | |
122 else: | |
123 if (name is None) != positional: | |
124 raise TranslationError('format string mixes positional ' | |
125 'and named placeholders') | |
126 return bool(positional) | |
127 | |
128 a, b = map(_parse, (format, alternative)) | |
129 | |
130 # now check if both strings are positional or named | |
131 a_positional, b_positional = map(_check_positional, (a, b)) | |
132 if a_positional and not b_positional and not b: | |
133 raise TranslationError('placeholders are incompatible') | |
134 elif a_positional != b_positional: | |
135 raise TranslationError('the format strings are of different kinds') | |
136 | |
137 # if we are operating on positional strings both must have the | |
138 # same number of format chars and those must be compatible | |
139 if a_positional: | |
140 if len(a) != len(b): | |
141 raise TranslationError('positional format placeholders are ' | |
142 'unbalanced') | |
143 for idx, ((_, first), (_, second)) in enumerate(izip(a, b)): | |
144 if not _compatible(first, second): | |
145 raise TranslationError('incompatible format for placeholder ' | |
146 '%d: %r and %r are not compatible' % | |
147 (idx + 1, first, second)) | |
148 | |
149 # otherwise the second string must not have names the first one | |
150 # doesn't have and the types of those included must be compatible | |
151 else: | |
152 type_map = dict(a) | |
153 for name, typechar in b: | |
154 if name not in type_map: | |
155 raise TranslationError('unknown named placeholder %r' % name) | |
156 elif not _compatible(typechar, type_map[name]): | |
157 raise TranslationError('incompatible format for ' | |
158 'placeholder %r: ' | |
159 '%r and %r are not compatible' % | |
160 (name, typechar, type_map[name])) | |
161 | |
162 | |
163 def _find_checkers(): | |
164 try: | |
165 from pkg_resources import working_set | |
166 except ImportError: | |
167 return [num_plurals, python_format] | |
168 checkers = [] | |
169 for entry_point in working_set.iter_entry_points('babel.checkers'): | |
170 checkers.append(entry_point.load()) | |
171 return checkers | |
172 | |
173 | |
174 checkers = _find_checkers() |