comparison babel/messages/catalog.py @ 177:47f6c31e9a24

Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing). The `Catalog` class now also includes an extra attribute, '''`fuzzy`''', which is the fuzzy bit of the catalog header. The above change allows the `compile_catalog` frontends to only compile catalogs '''not''' marked as fuzzy, unless `--use-fuzzy` is passed. Added tests to check header fuzzy bit parsing.
author palgarvio
date Tue, 26 Jun 2007 16:46:56 +0000
parents 533baef258bb
children e927dffc9ab4
comparison
equal deleted inserted replaced
176:b29bb8629a68 177:47f6c31e9a24
64 self.flags.discard('python-format') 64 self.flags.discard('python-format')
65 self.auto_comments = list(auto_comments) 65 self.auto_comments = list(auto_comments)
66 self.user_comments = list(user_comments) 66 self.user_comments = list(user_comments)
67 67
68 def __repr__(self): 68 def __repr__(self):
69 return '<%s %r>' % (type(self).__name__, self.id) 69 return '<%s %r (Flags: %r)>' % (type(self).__name__, self.id,
70 ', '.join([flag for flag in self.flags]))
70 71
71 def fuzzy(self): 72 def fuzzy(self):
72 return 'fuzzy' in self.flags 73 return 'fuzzy' in self.flags
73 fuzzy = property(fuzzy, doc="""\ 74 fuzzy = property(fuzzy, doc="""\
74 Whether the translation is fuzzy. 75 Whether the translation is fuzzy.
75 76
76 >>> Message('foo').fuzzy 77 >>> Message('foo').fuzzy
77 False 78 False
78 >>> Message('foo', 'foo', flags=['fuzzy']).fuzzy 79 >>> msg = Message('foo', 'foo', flags=['fuzzy'])
80 >>> msg.fuzzy
79 True 81 True
82 >>> msg
83 <Message 'foo' (Flags: 'fuzzy')>
80 84
81 :type: `bool` 85 :type: `bool`
82 """) 86 """)
83 87
84 def pluralizable(self): 88 def pluralizable(self):
122 """Representation of a message catalog.""" 126 """Representation of a message catalog."""
123 127
124 def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER, 128 def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER,
125 project=None, version=None, copyright_holder=None, 129 project=None, version=None, copyright_holder=None,
126 msgid_bugs_address=None, creation_date=None, 130 msgid_bugs_address=None, creation_date=None,
127 revision_date=None, last_translator=None, charset='utf-8'): 131 revision_date=None, last_translator=None, charset='utf-8',
132 fuzzy=True):
128 """Initialize the catalog object. 133 """Initialize the catalog object.
129 134
130 :param locale: the locale identifier or `Locale` object, or `None` 135 :param locale: the locale identifier or `Locale` object, or `None`
131 if the catalog is not bound to a locale (which basically 136 if the catalog is not bound to a locale (which basically
132 means it's a template) 137 means it's a template)
140 reports to 145 reports to
141 :param creation_date: the date the catalog was created 146 :param creation_date: the date the catalog was created
142 :param revision_date: the date the catalog was revised 147 :param revision_date: the date the catalog was revised
143 :param last_translator: the name and email of the last translator 148 :param last_translator: the name and email of the last translator
144 :param charset: the encoding to use in the output 149 :param charset: the encoding to use in the output
150 :param fuzzy: the fuzzy bit on the catalog header
145 """ 151 """
146 self.domain = domain #: The message domain 152 self.domain = domain #: The message domain
147 if locale: 153 if locale:
148 locale = Locale.parse(locale) 154 locale = Locale.parse(locale)
149 self.locale = locale #: The locale or `None` 155 self.locale = locale #: The locale or `None`
168 if revision_date is None: 174 if revision_date is None:
169 revision_date = datetime.now(LOCALTZ) 175 revision_date = datetime.now(LOCALTZ)
170 elif isinstance(revision_date, datetime) and not revision_date.tzinfo: 176 elif isinstance(revision_date, datetime) and not revision_date.tzinfo:
171 revision_date = revision_date.replace(tzinfo=LOCALTZ) 177 revision_date = revision_date.replace(tzinfo=LOCALTZ)
172 self.revision_date = revision_date #: Last revision date of the catalog 178 self.revision_date = revision_date #: Last revision date of the catalog
179 self.fuzzy = fuzzy #: Catalog Header fuzzy bit(True or False)
173 180
174 def _get_header_comment(self): 181 def _get_header_comment(self):
175 comment = self._header_comment 182 comment = self._header_comment
176 comment = comment.replace('PROJECT', self.project) \ 183 comment = comment.replace('PROJECT', self.project) \
177 .replace('VERSION', self.version) \ 184 .replace('VERSION', self.version) \
374 :rtype: ``iterator`` 381 :rtype: ``iterator``
375 """ 382 """
376 buf = [] 383 buf = []
377 for name, value in self.mime_headers: 384 for name, value in self.mime_headers:
378 buf.append('%s: %s' % (name, value)) 385 buf.append('%s: %s' % (name, value))
379 yield Message('', '\n'.join(buf), flags=set(['fuzzy'])) 386 if self.fuzzy:
387 flags = set(['fuzzy'])
388 else:
389 flags = set()
390 yield Message('', '\n'.join(buf), flags=flags)
380 for key in self._messages: 391 for key in self._messages:
381 yield self._messages[key] 392 yield self._messages[key]
382 393
383 def __repr__(self): 394 def __repr__(self):
384 locale = '' 395 locale = ''
406 """Add or update the message with the specified ID. 417 """Add or update the message with the specified ID.
407 418
408 >>> catalog = Catalog() 419 >>> catalog = Catalog()
409 >>> catalog[u'foo'] = Message(u'foo') 420 >>> catalog[u'foo'] = Message(u'foo')
410 >>> catalog[u'foo'] 421 >>> catalog[u'foo']
411 <Message u'foo'> 422 <Message u'foo' (Flags: '')>
412 423
413 If a message with that ID is already in the catalog, it is updated 424 If a message with that ID is already in the catalog, it is updated
414 to include the locations and flags of the new message. 425 to include the locations and flags of the new message.
415 426
416 >>> catalog = Catalog() 427 >>> catalog = Catalog()
453 """Add or update the message with the specified ID. 464 """Add or update the message with the specified ID.
454 465
455 >>> catalog = Catalog() 466 >>> catalog = Catalog()
456 >>> catalog.add(u'foo') 467 >>> catalog.add(u'foo')
457 >>> catalog[u'foo'] 468 >>> catalog[u'foo']
458 <Message u'foo'> 469 <Message u'foo' (Flags: '')>
459 470
460 This method simply constructs a `Message` object with the given 471 This method simply constructs a `Message` object with the given
461 arguments and invokes `__setitem__` with that object. 472 arguments and invokes `__setitem__` with that object.
462 473
463 :param id: the message ID, or a ``(singular, plural)`` tuple for 474 :param id: the message ID, or a ``(singular, plural)`` tuple for
502 [('util.py', 42)] 513 [('util.py', 42)]
503 514
504 >>> 'head' in catalog 515 >>> 'head' in catalog
505 False 516 False
506 >>> rest 517 >>> rest
507 [<Message 'head'>] 518 [<Message 'head' (Flags: '')>]
508 519
509 :param template: the reference catalog, usually read from a POT file 520 :param template: the reference catalog, usually read from a POT file
510 :param fuzzy_matching: whether to use fuzzy matching of message IDs 521 :param fuzzy_matching: whether to use fuzzy matching of message IDs
511 :return: a list of `Message` objects that the catalog contained before 522 :return: a list of `Message` objects that the catalog contained before
512 the updated, but couldn't be found in the template 523 the updated, but couldn't be found in the template
Copyright (C) 2012-2017 Edgewall Software