annotate babel/messages/checkers.py @ 353:0791b3bf42cc trunk

Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
author aronacher
date Tue, 17 Jun 2008 19:59:02 +0000
parents 8860097a9765
children 9a2618ab9bbc
rev   line source
220
97b4b289e792 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 -*-
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
2 #
97b4b289e792 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
97b4b289e792 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.
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
5 #
97b4b289e792 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
97b4b289e792 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
97b4b289e792 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.
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
9 #
97b4b289e792 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
97b4b289e792 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
97b4b289e792 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/.
97b4b289e792 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
38053412171b Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
14 """Various routines that help with validation of translations.
38053412171b Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
15
38053412171b Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
16 :since: version 0.9
38053412171b Add more `since` tags to stuff added in trunk.
cmlenz
parents: 220
diff changeset
17 """
220
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
18
352
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
19 from itertools import izip
220
97b4b289e792 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
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
21
352
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
22 #: list of format chars that are compatible to each other
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
23 _string_format_compatibilities = [
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
24 set(['i', 'd', 'u']),
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
25 set(['x', 'X']),
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
26 set(['f', 'F', 'g', 'G'])
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
27 ]
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
28
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
29
220
97b4b289e792 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):
97b4b289e792 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."""
97b4b289e792 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:
97b4b289e792 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):
97b4b289e792 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 "
97b4b289e792 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")
97b4b289e792 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
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
37
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
38 msgstrs = message.string
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
39 if not isinstance(msgstrs, (list, tuple)):
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
40 msgstrs = (msgstrs,)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
41 if len(msgstrs) != catalog.num_plurals:
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
42 raise TranslationError("Wrong number of plural forms (expected %d)" %
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
43 catalog.num_plurals)
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
44
352
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
45
220
97b4b289e792 Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff changeset
46 def python_format(catalog, message):
353
0791b3bf42cc Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
47 """Verify the format string placeholders in the translation."""
352
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
48 if 'python-format' not in message.flags:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
49 return
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
50 msgids = message.id
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
51 if not isinstance(msgids, (list, tuple)):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
52 msgids = (msgids,)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
53 msgstrs = message.string
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
54 if not isinstance(msgstrs, (list, tuple)):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
55 msgstrs = (msgstrs,)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
56
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
57 for msgid, msgstr in izip(msgids, msgstrs):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
58 if msgstr:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
59 _validate_format(msgid, msgstr)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
60
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
61
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
62 def _validate_format(format, alternative):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
63 """Test format string `alternative` against `format`. `format` can be the
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
64 msgid of a message and `alternative` one of the `msgstr`\s. The two
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
65 arguments are not interchangeable as `alternative` may contain less
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
66 placeholders if `format` uses named placeholders.
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
67
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
68 If `format` does not use string formatting a `ValueError` is raised.
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
69
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
70 If the string formatting of `alternative` is compatible to `format` the
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
71 function returns `None`, otherwise a `TranslationError` is raised.
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
72
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
73 Examples for compatible format strings:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
74
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
75 >>> _validate_format('Hello %s!', 'Hallo %s!')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
76 >>> _validate_format('Hello %i!', 'Hallo %d!')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
77
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
78 Example for an incompatible format strings:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
79
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
80 >>> _validate_format('Hello %(name)s!', 'Hallo %s!')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
81 Traceback (most recent call last):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
82 ...
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
83 TranslationError: the format strings are of different kinds
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
84
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
85 This function is used by the `python_format` checker.
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
86
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
87 :param format: The original format string
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
88 :param alternative: The alternative format string that should be checked
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
89 against format
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
90 :return: None on success
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
91 :raises TranslationError: on formatting errors
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
92 """
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
93
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
94 def _parse(string):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
95 result = []
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
96 for match in PYTHON_FORMAT.finditer(string):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
97 name, format, typechar = match.groups()
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
98 if typechar == '%' and name is not None:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
99 continue
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
100 result.append((name, typechar))
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
101 return result
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
102
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
103 def _compatible(a, b):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
104 if a == b:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
105 return True
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
106 for set in _string_format_compatibilities:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
107 if a in set and b in set:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
108 return True
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
109 return False
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
110
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
111 def _check_positional(results):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
112 positional = None
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
113 for name, char in results:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
114 if positional is None:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
115 positional = name is None
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
116 else:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
117 if (name is None) != positional:
353
0791b3bf42cc Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
118 raise TranslationError('format string mixes positional '
0791b3bf42cc Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents: 352
diff changeset
119 'and named placeholders')
352
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
120 return bool(positional)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
121
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
122 a, b = map(_parse, (format, alternative))
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
123
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
124 # if a does not use string formattings, we are dealing with invalid
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
125 # input data. This function only works if the first string provided
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
126 # does contain string format chars
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
127 if not a:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
128 raise ValueError('original string provided does not use string '
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
129 'formatting.')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
130
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
131 # now check if both strings are positional or named
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
132 a_positional, b_positional = map(_check_positional, (a, b))
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
133 if a_positional and not b_positional and not b:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
134 raise TranslationError('placeholders are incompatible')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
135 elif a_positional != b_positional:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
136 raise TranslationError('the format strings are of different kinds')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
137
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
138 # if we are operating on positional strings both must have the
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
139 # same number of format chars and those must be compatible
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
140 if a_positional:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
141 if len(a) != len(b):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
142 raise TranslationError('positional format placeholders are '
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
143 'unbalanced')
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
144 for idx, ((_, first), (_, second)) in enumerate(izip(a, b)):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
145 if not _compatible(first, second):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
146 raise TranslationError('incompatible format for placeholder '
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
147 '%d: %r and %r are not compatible' %
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
148 (idx + 1, first, second))
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
149
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
150 # otherwise the second string must not have names the first one
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
151 # doesn't have and the types of those included must be compatible
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
152 else:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
153 type_map = dict(a)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
154 for name, typechar in b:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
155 if name not in type_map:
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
156 raise TranslationError('unknown named placeholder %r' % name)
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
157 elif not _compatible(typechar, type_map[name]):
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
158 raise TranslationErrorError('incompatible format for '
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
159 'placeholder %r: '
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
160 '%r and %r are not compatible' %
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
161 (name, typechar, type_map[name]))
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
162
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
163
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
164 #: list of builtin checkers for babel installations without setuptools.
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
165 #: Keep this in sync with the mapping in the setup.py
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
166 #: :see: babel.messages.catalog.Catalog.check
8860097a9765 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 234
diff changeset
167 builtin_checkers = [num_plurals, python_format]
Copyright (C) 2012-2017 Edgewall Software