comparison babel/messages/catalog.py @ 579:99d51589c822 trunk

use decorators (as we require Python 2.4+ anyway)
author fschwarz
date Tue, 31 Jul 2012 08:46:19 +0000
parents b0e80df660ab
children c5dd3752bf2a
comparison
equal deleted inserted replaced
578:b167d06df1d6 579:99d51589c822
144 checker(catalog, self) 144 checker(catalog, self)
145 except TranslationError, e: 145 except TranslationError, e:
146 errors.append(e) 146 errors.append(e)
147 return errors 147 return errors
148 148
149 @property
149 def fuzzy(self): 150 def fuzzy(self):
150 return 'fuzzy' in self.flags 151 """Whether the translation is fuzzy.
151 fuzzy = property(fuzzy, doc="""\
152 Whether the translation is fuzzy.
153 152
154 >>> Message('foo').fuzzy 153 >>> Message('foo').fuzzy
155 False 154 False
156 >>> msg = Message('foo', 'foo', flags=['fuzzy']) 155 >>> msg = Message('foo', 'foo', flags=['fuzzy'])
157 >>> msg.fuzzy 156 >>> msg.fuzzy
158 True 157 True
159 >>> msg 158 >>> msg
160 <Message 'foo' (flags: ['fuzzy'])> 159 <Message 'foo' (flags: ['fuzzy'])>
161 160
162 :type: `bool` 161 :type: `bool`"""
163 """) 162 return 'fuzzy' in self.flags
164 163
164 @property
165 def pluralizable(self): 165 def pluralizable(self):
166 return isinstance(self.id, (list, tuple)) 166 """Whether the message is plurizable.
167 pluralizable = property(pluralizable, doc="""\
168 Whether the message is plurizable.
169 167
170 >>> Message('foo').pluralizable 168 >>> Message('foo').pluralizable
171 False 169 False
172 >>> Message(('foo', 'bar')).pluralizable 170 >>> Message(('foo', 'bar')).pluralizable
173 True 171 True
174 172
175 :type: `bool` 173 :type: `bool`"""
176 """) 174 return isinstance(self.id, (list, tuple))
177 175
176 @property
178 def python_format(self): 177 def python_format(self):
178 """Whether the message contains Python-style parameters.
179
180 >>> Message('foo %(name)s bar').python_format
181 True
182 >>> Message(('foo %(name)s', 'foo %(name)s')).python_format
183 True
184
185 :type: `bool`"""
179 ids = self.id 186 ids = self.id
180 if not isinstance(ids, (list, tuple)): 187 if not isinstance(ids, (list, tuple)):
181 ids = [ids] 188 ids = [ids]
182 return bool(filter(None, [PYTHON_FORMAT.search(id) for id in ids])) 189 return bool(filter(None, [PYTHON_FORMAT.search(id) for id in ids]))
183 python_format = property(python_format, doc="""\
184 Whether the message contains Python-style parameters.
185
186 >>> Message('foo %(name)s bar').python_format
187 True
188 >>> Message(('foo %(name)s', 'foo %(name)s')).python_format
189 True
190
191 :type: `bool`
192 """)
193 190
194 191
195 class TranslationError(Exception): 192 class TranslationError(Exception):
196 """Exception thrown by translation checkers when invalid message 193 """Exception thrown by translation checkers when invalid message
197 translations are encountered.""" 194 translations are encountered."""
470 Generated-By: Babel ... 467 Generated-By: Babel ...
471 468
472 :type: `list` 469 :type: `list`
473 """) 470 """)
474 471
472 @property
475 def num_plurals(self): 473 def num_plurals(self):
474 """The number of plurals used by the catalog or locale.
475
476 >>> Catalog(locale='en').num_plurals
477 2
478 >>> Catalog(locale='ga').num_plurals
479 3
480
481 :type: `int`"""
476 if self._num_plurals is None: 482 if self._num_plurals is None:
477 num = 2 483 num = 2
478 if self.locale: 484 if self.locale:
479 num = get_plural(self.locale)[0] 485 num = get_plural(self.locale)[0]
480 self._num_plurals = num 486 self._num_plurals = num
481 return self._num_plurals 487 return self._num_plurals
482 num_plurals = property(num_plurals, doc="""\ 488
483 The number of plurals used by the catalog or locale. 489 @property
484
485 >>> Catalog(locale='en').num_plurals
486 2
487 >>> Catalog(locale='ga').num_plurals
488 3
489
490 :type: `int`
491 """)
492
493 def plural_expr(self): 490 def plural_expr(self):
491 """The plural expression used by the catalog or locale.
492
493 >>> Catalog(locale='en').plural_expr
494 '(n != 1)'
495 >>> Catalog(locale='ga').plural_expr
496 '(n==1 ? 0 : n==2 ? 1 : 2)'
497
498 :type: `basestring`"""
494 if self._plural_expr is None: 499 if self._plural_expr is None:
495 expr = '(n != 1)' 500 expr = '(n != 1)'
496 if self.locale: 501 if self.locale:
497 expr = get_plural(self.locale)[1] 502 expr = get_plural(self.locale)[1]
498 self._plural_expr = expr 503 self._plural_expr = expr
499 return self._plural_expr 504 return self._plural_expr
500 plural_expr = property(plural_expr, doc="""\ 505
501 The plural expression used by the catalog or locale. 506 @property
502
503 >>> Catalog(locale='en').plural_expr
504 '(n != 1)'
505 >>> Catalog(locale='ga').plural_expr
506 '(n==1 ? 0 : n==2 ? 1 : 2)'
507
508 :type: `basestring`
509 """)
510
511 def plural_forms(self): 507 def plural_forms(self):
508 """Return the plural forms declaration for the locale.
509
510 >>> Catalog(locale='en').plural_forms
511 'nplurals=2; plural=(n != 1)'
512 >>> Catalog(locale='pt_BR').plural_forms
513 'nplurals=2; plural=(n > 1)'
514
515 :type: `str`"""
512 return 'nplurals=%s; plural=%s' % (self.num_plurals, self.plural_expr) 516 return 'nplurals=%s; plural=%s' % (self.num_plurals, self.plural_expr)
513 plural_forms = property(plural_forms, doc="""\
514 Return the plural forms declaration for the locale.
515
516 >>> Catalog(locale='en').plural_forms
517 'nplurals=2; plural=(n != 1)'
518 >>> Catalog(locale='pt_BR').plural_forms
519 'nplurals=2; plural=(n > 1)'
520
521 :type: `str`
522 """)
523 517
524 def __contains__(self, id): 518 def __contains__(self, id):
525 """Return whether the catalog has a message with the specified ID.""" 519 """Return whether the catalog has a message with the specified ID."""
526 return self._key_for(id) in self._messages 520 return self._key_for(id) in self._messages
527 521
528 def __len__(self): 522 def __len__(self):
529 """The number of messages in the catalog. 523 """The number of messages in the catalog.
530 524
531 This does not include the special ``msgid ""`` entry. 525 This does not include the special ``msgid ""`` entry."""
532 """
533 return len(self._messages) 526 return len(self._messages)
534 527
535 def __iter__(self): 528 def __iter__(self):
536 """Iterates through all the entries in the catalog, in the order they 529 """Iterates through all the entries in the catalog, in the order they
537 were added, yielding a `Message` object for every entry. 530 were added, yielding a `Message` object for every entry.
538 531
539 :rtype: ``iterator`` 532 :rtype: ``iterator``"""
540 """
541 buf = [] 533 buf = []
542 for name, value in self.mime_headers: 534 for name, value in self.mime_headers:
543 buf.append('%s: %s' % (name, value)) 535 buf.append('%s: %s' % (name, value))
544 flags = set() 536 flags = set()
545 if self.fuzzy: 537 if self.fuzzy:
Copyright (C) 2012-2017 Edgewall Software