Mercurial > babel > old > mirror
comparison 0.9.x/babel/messages/catalog.py @ 371:4b63ce84c6ae stable
Ported [388:405/trunk] to 0.9.x branch.
author | cmlenz |
---|---|
date | Fri, 27 Jun 2008 15:22:12 +0000 |
parents | 05975a0e7021 |
children | 6a0e7205790f |
comparison
equal
deleted
inserted
replaced
370:d6ea495ef8c9 | 371:4b63ce84c6ae |
---|---|
15 | 15 |
16 from cgi import parse_header | 16 from cgi import parse_header |
17 from datetime import datetime | 17 from datetime import datetime |
18 from difflib import get_close_matches | 18 from difflib import get_close_matches |
19 from email import message_from_string | 19 from email import message_from_string |
20 from copy import copy | |
20 import re | 21 import re |
21 try: | 22 try: |
22 set | 23 set |
23 except NameError: | 24 except NameError: |
24 from sets import Set as set | 25 from sets import Set as set |
31 from babel.util import odict, distinct, LOCALTZ, UTC, FixedOffsetTimezone | 32 from babel.util import odict, distinct, LOCALTZ, UTC, FixedOffsetTimezone |
32 | 33 |
33 __all__ = ['Message', 'Catalog', 'TranslationError'] | 34 __all__ = ['Message', 'Catalog', 'TranslationError'] |
34 __docformat__ = 'restructuredtext en' | 35 __docformat__ = 'restructuredtext en' |
35 | 36 |
36 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?([-#0\ +])?(\*|[\d]+)?' | 37 |
37 r'(\.(\*|[\d]+))?([hlL])?[diouxXeEfFgGcrs]') | 38 PYTHON_FORMAT = re.compile(r'''(?x) |
39 \% | |
40 (?:\(([\w]*)\))? | |
41 ( | |
42 [-#0\ +]?(?:\*|[\d]+)? | |
43 (?:\.(?:\*|[\d]+))? | |
44 [hlL]? | |
45 ) | |
46 ([diouxXeEfFgGcrs%]) | |
47 ''') | |
38 | 48 |
39 | 49 |
40 class Message(object): | 50 class Message(object): |
41 """Representation of a single message in a catalog.""" | 51 """Representation of a single message in a catalog.""" |
42 | 52 |
91 elif obj_plural: | 101 elif obj_plural: |
92 return cmp(self.id, obj.id[0]) | 102 return cmp(self.id, obj.id[0]) |
93 return cmp(self.id, obj.id) | 103 return cmp(self.id, obj.id) |
94 | 104 |
95 def clone(self): | 105 def clone(self): |
96 return Message(self.id, self.string, self.locations, self.flags, | 106 return Message(*map(copy, (self.id, self.string, self.locations, |
97 self.auto_comments, self.user_comments, | 107 self.flags, self.auto_comments, |
98 self.previous_id, self.lineno) | 108 self.user_comments, self.previous_id, |
109 self.lineno))) | |
110 | |
111 def check(self, catalog=None): | |
112 """Run various validation checks on the message. Some validations | |
113 are only performed if the catalog is provided. This method returns | |
114 a sequence of `TranslationError` objects. | |
115 | |
116 :rtype: ``iterator`` | |
117 :param catalog: A catalog instance that is passed to the checkers | |
118 :see: `Catalog.check` for a way to perform checks for all messages | |
119 in a catalog. | |
120 """ | |
121 from babel.messages.checkers import checkers | |
122 errors = [] | |
123 for checker in checkers: | |
124 try: | |
125 checker(catalog, self) | |
126 except TranslationError, e: | |
127 errors.append(e) | |
128 return errors | |
99 | 129 |
100 def fuzzy(self): | 130 def fuzzy(self): |
101 return 'fuzzy' in self.flags | 131 return 'fuzzy' in self.flags |
102 fuzzy = property(fuzzy, doc="""\ | 132 fuzzy = property(fuzzy, doc="""\ |
103 Whether the translation is fuzzy. | 133 Whether the translation is fuzzy. |
566 | 596 |
567 For every message which fails validation, this method yield a | 597 For every message which fails validation, this method yield a |
568 ``(message, errors)`` tuple, where ``message`` is the `Message` object | 598 ``(message, errors)`` tuple, where ``message`` is the `Message` object |
569 and ``errors`` is a sequence of `TranslationError` objects. | 599 and ``errors`` is a sequence of `TranslationError` objects. |
570 | 600 |
571 :note: this feature requires ``setuptools``/``pkg_resources`` to be | |
572 installed; if it is not, this method will simply return an empty | |
573 iterator | |
574 :rtype: ``iterator`` | 601 :rtype: ``iterator`` |
575 """ | 602 """ |
576 checkers = [] | 603 for message in self._messages.values(): |
577 try: | 604 errors = message.check(catalog=self) |
578 from pkg_resources import working_set | 605 if errors: |
579 except ImportError: | 606 yield message, errors |
580 return | |
581 else: | |
582 for entry_point in working_set.iter_entry_points('babel.checkers'): | |
583 checkers.append(entry_point.load()) | |
584 for message in self._messages.values(): | |
585 errors = [] | |
586 for checker in checkers: | |
587 try: | |
588 checker(self, message) | |
589 except TranslationError, e: | |
590 errors.append(e) | |
591 if errors: | |
592 yield message, errors | |
593 | 607 |
594 def update(self, template, no_fuzzy_matching=False): | 608 def update(self, template, no_fuzzy_matching=False): |
595 """Update the catalog based on the given template catalog. | 609 """Update the catalog based on the given template catalog. |
596 | 610 |
597 >>> from babel.messages import Catalog | 611 >>> from babel.messages import Catalog |