Mercurial > babel > old > mirror
annotate babel/messages/checkers.py @ 416:f03cc3bed4e1
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
usage. still has a few, unimportant failing tests
author | pjenvey |
---|---|
date | Wed, 08 Oct 2008 22:42:55 +0000 |
parents | 1cc8aba0363e |
children | 36beb7f9b692 |
rev | line source |
---|---|
222
bd8b1301b27e
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 -*- |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
2 # |
bd8b1301b27e
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 |
bd8b1301b27e
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. |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
5 # |
bd8b1301b27e
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 |
bd8b1301b27e
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 |
bd8b1301b27e
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. |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
9 # |
bd8b1301b27e
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 |
bd8b1301b27e
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 |
bd8b1301b27e
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/. |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
13 |
236 | 14 """Various routines that help with validation of translations. |
15 | |
16 :since: version 0.9 | |
17 """ | |
222
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
18 |
354
249aab27c4b3
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:
236
diff
changeset
|
19 from itertools import izip |
222
bd8b1301b27e
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 |
416
f03cc3bed4e1
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
366
diff
changeset
|
21 from babel.util import set |
222
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
22 |
354
249aab27c4b3
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:
236
diff
changeset
|
23 #: list of format chars that are compatible to each other |
249aab27c4b3
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:
236
diff
changeset
|
24 _string_format_compatibilities = [ |
249aab27c4b3
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:
236
diff
changeset
|
25 set(['i', 'd', 'u']), |
249aab27c4b3
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:
236
diff
changeset
|
26 set(['x', 'X']), |
249aab27c4b3
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:
236
diff
changeset
|
27 set(['f', 'F', 'g', 'G']) |
249aab27c4b3
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:
236
diff
changeset
|
28 ] |
249aab27c4b3
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:
236
diff
changeset
|
29 |
249aab27c4b3
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:
236
diff
changeset
|
30 |
222
bd8b1301b27e
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): |
bd8b1301b27e
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.""" |
bd8b1301b27e
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: |
bd8b1301b27e
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): |
bd8b1301b27e
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 " |
bd8b1301b27e
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") |
bd8b1301b27e
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 |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
38 |
357
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
39 # skip further tests if no catalog is provided. |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
40 elif catalog is None: |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
41 return |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
42 |
222
bd8b1301b27e
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 |
bd8b1301b27e
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)): |
bd8b1301b27e
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,) |
bd8b1301b27e
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: |
bd8b1301b27e
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)" % |
bd8b1301b27e
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) |
bd8b1301b27e
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
diff
changeset
|
49 |
354
249aab27c4b3
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:
236
diff
changeset
|
50 |
222
bd8b1301b27e
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): |
355
2b74cff3c612
Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents:
354
diff
changeset
|
52 """Verify the format string placeholders in the translation.""" |
354
249aab27c4b3
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:
236
diff
changeset
|
53 if 'python-format' not in message.flags: |
249aab27c4b3
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:
236
diff
changeset
|
54 return |
249aab27c4b3
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:
236
diff
changeset
|
55 msgids = message.id |
249aab27c4b3
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:
236
diff
changeset
|
56 if not isinstance(msgids, (list, tuple)): |
249aab27c4b3
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:
236
diff
changeset
|
57 msgids = (msgids,) |
249aab27c4b3
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:
236
diff
changeset
|
58 msgstrs = message.string |
249aab27c4b3
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:
236
diff
changeset
|
59 if not isinstance(msgstrs, (list, tuple)): |
249aab27c4b3
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:
236
diff
changeset
|
60 msgstrs = (msgstrs,) |
249aab27c4b3
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:
236
diff
changeset
|
61 |
249aab27c4b3
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:
236
diff
changeset
|
62 for msgid, msgstr in izip(msgids, msgstrs): |
249aab27c4b3
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:
236
diff
changeset
|
63 if msgstr: |
249aab27c4b3
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:
236
diff
changeset
|
64 _validate_format(msgid, msgstr) |
249aab27c4b3
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:
236
diff
changeset
|
65 |
249aab27c4b3
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:
236
diff
changeset
|
66 |
249aab27c4b3
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:
236
diff
changeset
|
67 def _validate_format(format, alternative): |
249aab27c4b3
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:
236
diff
changeset
|
68 """Test format string `alternative` against `format`. `format` can be the |
249aab27c4b3
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:
236
diff
changeset
|
69 msgid of a message and `alternative` one of the `msgstr`\s. The two |
249aab27c4b3
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:
236
diff
changeset
|
70 arguments are not interchangeable as `alternative` may contain less |
249aab27c4b3
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:
236
diff
changeset
|
71 placeholders if `format` uses named placeholders. |
249aab27c4b3
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:
236
diff
changeset
|
72 |
249aab27c4b3
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:
236
diff
changeset
|
73 If `format` does not use string formatting a `ValueError` is raised. |
249aab27c4b3
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:
236
diff
changeset
|
74 |
249aab27c4b3
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:
236
diff
changeset
|
75 If the string formatting of `alternative` is compatible to `format` the |
249aab27c4b3
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:
236
diff
changeset
|
76 function returns `None`, otherwise a `TranslationError` is raised. |
249aab27c4b3
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:
236
diff
changeset
|
77 |
249aab27c4b3
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:
236
diff
changeset
|
78 Examples for compatible format strings: |
249aab27c4b3
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:
236
diff
changeset
|
79 |
249aab27c4b3
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:
236
diff
changeset
|
80 >>> _validate_format('Hello %s!', 'Hallo %s!') |
249aab27c4b3
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:
236
diff
changeset
|
81 >>> _validate_format('Hello %i!', 'Hallo %d!') |
249aab27c4b3
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:
236
diff
changeset
|
82 |
249aab27c4b3
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:
236
diff
changeset
|
83 Example for an incompatible format strings: |
249aab27c4b3
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:
236
diff
changeset
|
84 |
249aab27c4b3
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:
236
diff
changeset
|
85 >>> _validate_format('Hello %(name)s!', 'Hallo %s!') |
249aab27c4b3
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:
236
diff
changeset
|
86 Traceback (most recent call last): |
249aab27c4b3
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:
236
diff
changeset
|
87 ... |
249aab27c4b3
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:
236
diff
changeset
|
88 TranslationError: the format strings are of different kinds |
249aab27c4b3
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:
236
diff
changeset
|
89 |
249aab27c4b3
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:
236
diff
changeset
|
90 This function is used by the `python_format` checker. |
249aab27c4b3
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:
236
diff
changeset
|
91 |
249aab27c4b3
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:
236
diff
changeset
|
92 :param format: The original format string |
249aab27c4b3
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:
236
diff
changeset
|
93 :param alternative: The alternative format string that should be checked |
249aab27c4b3
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:
236
diff
changeset
|
94 against format |
249aab27c4b3
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:
236
diff
changeset
|
95 :return: None on success |
249aab27c4b3
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:
236
diff
changeset
|
96 :raises TranslationError: on formatting errors |
249aab27c4b3
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:
236
diff
changeset
|
97 """ |
249aab27c4b3
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:
236
diff
changeset
|
98 |
249aab27c4b3
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:
236
diff
changeset
|
99 def _parse(string): |
249aab27c4b3
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:
236
diff
changeset
|
100 result = [] |
249aab27c4b3
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:
236
diff
changeset
|
101 for match in PYTHON_FORMAT.finditer(string): |
249aab27c4b3
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:
236
diff
changeset
|
102 name, format, typechar = match.groups() |
363 | 103 if typechar == '%' and name is None: |
354
249aab27c4b3
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:
236
diff
changeset
|
104 continue |
362
928e3021c552
Convert format strings to str for nicer error messages (no u prefix) in the python_format checker.
aronacher
parents:
357
diff
changeset
|
105 result.append((name, str(typechar))) |
354
249aab27c4b3
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:
236
diff
changeset
|
106 return result |
249aab27c4b3
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:
236
diff
changeset
|
107 |
249aab27c4b3
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:
236
diff
changeset
|
108 def _compatible(a, b): |
249aab27c4b3
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:
236
diff
changeset
|
109 if a == b: |
249aab27c4b3
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:
236
diff
changeset
|
110 return True |
249aab27c4b3
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:
236
diff
changeset
|
111 for set in _string_format_compatibilities: |
249aab27c4b3
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:
236
diff
changeset
|
112 if a in set and b in set: |
249aab27c4b3
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:
236
diff
changeset
|
113 return True |
249aab27c4b3
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:
236
diff
changeset
|
114 return False |
249aab27c4b3
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:
236
diff
changeset
|
115 |
249aab27c4b3
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:
236
diff
changeset
|
116 def _check_positional(results): |
249aab27c4b3
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:
236
diff
changeset
|
117 positional = None |
249aab27c4b3
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:
236
diff
changeset
|
118 for name, char in results: |
249aab27c4b3
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:
236
diff
changeset
|
119 if positional is None: |
249aab27c4b3
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:
236
diff
changeset
|
120 positional = name is None |
249aab27c4b3
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:
236
diff
changeset
|
121 else: |
249aab27c4b3
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:
236
diff
changeset
|
122 if (name is None) != positional: |
355
2b74cff3c612
Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents:
354
diff
changeset
|
123 raise TranslationError('format string mixes positional ' |
2b74cff3c612
Fixed a small bug in the python format string checker that caused the wrong exception to be thrown.
aronacher
parents:
354
diff
changeset
|
124 'and named placeholders') |
354
249aab27c4b3
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:
236
diff
changeset
|
125 return bool(positional) |
249aab27c4b3
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:
236
diff
changeset
|
126 |
249aab27c4b3
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:
236
diff
changeset
|
127 a, b = map(_parse, (format, alternative)) |
249aab27c4b3
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:
236
diff
changeset
|
128 |
249aab27c4b3
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:
236
diff
changeset
|
129 # if a does not use string formattings, we are dealing with invalid |
249aab27c4b3
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:
236
diff
changeset
|
130 # input data. This function only works if the first string provided |
249aab27c4b3
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:
236
diff
changeset
|
131 # does contain string format chars |
249aab27c4b3
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:
236
diff
changeset
|
132 if not a: |
249aab27c4b3
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:
236
diff
changeset
|
133 raise ValueError('original string provided does not use string ' |
249aab27c4b3
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:
236
diff
changeset
|
134 'formatting.') |
249aab27c4b3
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:
236
diff
changeset
|
135 |
249aab27c4b3
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:
236
diff
changeset
|
136 # now check if both strings are positional or named |
249aab27c4b3
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:
236
diff
changeset
|
137 a_positional, b_positional = map(_check_positional, (a, b)) |
249aab27c4b3
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:
236
diff
changeset
|
138 if a_positional and not b_positional and not b: |
249aab27c4b3
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:
236
diff
changeset
|
139 raise TranslationError('placeholders are incompatible') |
249aab27c4b3
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:
236
diff
changeset
|
140 elif a_positional != b_positional: |
249aab27c4b3
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:
236
diff
changeset
|
141 raise TranslationError('the format strings are of different kinds') |
249aab27c4b3
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:
236
diff
changeset
|
142 |
249aab27c4b3
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:
236
diff
changeset
|
143 # if we are operating on positional strings both must have the |
249aab27c4b3
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:
236
diff
changeset
|
144 # same number of format chars and those must be compatible |
249aab27c4b3
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:
236
diff
changeset
|
145 if a_positional: |
249aab27c4b3
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:
236
diff
changeset
|
146 if len(a) != len(b): |
249aab27c4b3
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:
236
diff
changeset
|
147 raise TranslationError('positional format placeholders are ' |
249aab27c4b3
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:
236
diff
changeset
|
148 'unbalanced') |
249aab27c4b3
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:
236
diff
changeset
|
149 for idx, ((_, first), (_, second)) in enumerate(izip(a, b)): |
249aab27c4b3
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:
236
diff
changeset
|
150 if not _compatible(first, second): |
249aab27c4b3
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:
236
diff
changeset
|
151 raise TranslationError('incompatible format for placeholder ' |
249aab27c4b3
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:
236
diff
changeset
|
152 '%d: %r and %r are not compatible' % |
249aab27c4b3
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:
236
diff
changeset
|
153 (idx + 1, first, second)) |
249aab27c4b3
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:
236
diff
changeset
|
154 |
249aab27c4b3
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:
236
diff
changeset
|
155 # otherwise the second string must not have names the first one |
249aab27c4b3
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:
236
diff
changeset
|
156 # doesn't have and the types of those included must be compatible |
249aab27c4b3
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:
236
diff
changeset
|
157 else: |
249aab27c4b3
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:
236
diff
changeset
|
158 type_map = dict(a) |
249aab27c4b3
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:
236
diff
changeset
|
159 for name, typechar in b: |
249aab27c4b3
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:
236
diff
changeset
|
160 if name not in type_map: |
249aab27c4b3
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:
236
diff
changeset
|
161 raise TranslationError('unknown named placeholder %r' % name) |
249aab27c4b3
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:
236
diff
changeset
|
162 elif not _compatible(typechar, type_map[name]): |
366
1cc8aba0363e
Fixed a typo in the message checkers (tests will follow)
aronacher
parents:
363
diff
changeset
|
163 raise TranslationError('incompatible format for ' |
1cc8aba0363e
Fixed a typo in the message checkers (tests will follow)
aronacher
parents:
363
diff
changeset
|
164 'placeholder %r: ' |
1cc8aba0363e
Fixed a typo in the message checkers (tests will follow)
aronacher
parents:
363
diff
changeset
|
165 '%r and %r are not compatible' % |
1cc8aba0363e
Fixed a typo in the message checkers (tests will follow)
aronacher
parents:
363
diff
changeset
|
166 (name, typechar, type_map[name])) |
354
249aab27c4b3
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:
236
diff
changeset
|
167 |
249aab27c4b3
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:
236
diff
changeset
|
168 |
357
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
169 def _find_checkers(): |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
170 try: |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
171 from pkg_resources import working_set |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
172 except ImportError: |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
173 return [num_plurals, python_format] |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
174 checkers = [] |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
175 for entry_point in working_set.iter_entry_points('babel.checkers'): |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
176 checkers.append(entry_point.load()) |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
177 return checkers |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
178 |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
179 |
9acf6b5baa22
Refactored the checker system. It's now possible to partially validate translations on a per-message level.
aronacher
parents:
355
diff
changeset
|
180 checkers = _find_checkers() |