comparison babel/messages/catalog.py @ 167:533baef258bb

Implement fuzzy matching to catalog updates. No frontend yet.
author cmlenz
date Fri, 22 Jun 2007 08:39:04 +0000
parents eafaa302dde1
children 47f6c31e9a24
comparison
equal deleted inserted replaced
166:0eccbe635dba 167:533baef258bb
13 13
14 """Data structures for message catalogs.""" 14 """Data structures for message catalogs."""
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 email import message_from_string 19 from email import message_from_string
19 import re 20 import re
20 try: 21 try:
21 set 22 set
22 except NameError: 23 except NameError:
469 :param user_comments: a sequence of user comments 470 :param user_comments: a sequence of user comments
470 """ 471 """
471 self[id] = Message(id, string, list(locations), flags, auto_comments, 472 self[id] = Message(id, string, list(locations), flags, auto_comments,
472 user_comments) 473 user_comments)
473 474
474 def update(self, template): 475 def update(self, template, fuzzy_matching=True):
475 """Update the catalog based on the given template catalog. 476 """Update the catalog based on the given template catalog.
476 477
477 >>> from babel.messages import Catalog 478 >>> from babel.messages import Catalog
478 >>> template = Catalog() 479 >>> template = Catalog()
479 >>> template.add('blue', locations=[('main.py', 100)]) 480 >>> template.add('blue', locations=[('main.py', 100)])
504 False 505 False
505 >>> rest 506 >>> rest
506 [<Message 'head'>] 507 [<Message 'head'>]
507 508
508 :param template: the reference catalog, usually read from a POT file 509 :param template: the reference catalog, usually read from a POT file
510 :param fuzzy_matching: whether to use fuzzy matching of message IDs
509 :return: a list of `Message` objects that the catalog contained before 511 :return: a list of `Message` objects that the catalog contained before
510 the updated, but couldn't be found in the template 512 the updated, but couldn't be found in the template
511 """ 513 """
512 rest = odict([(message.id, message) for message in self if message.id])
513 messages = self._messages 514 messages = self._messages
514 self._messages = odict() 515 self._messages = odict()
515 516
516 for message in template: 517 for message in template:
517 if message.id: 518 if message.id:
519 if key in messages: 520 if key in messages:
520 oldmsg = messages.pop(key) 521 oldmsg = messages.pop(key)
521 message.string = oldmsg.string 522 message.string = oldmsg.string
522 message.flags |= oldmsg.flags 523 message.flags |= oldmsg.flags
523 self[message.id] = message 524 self[message.id] = message
524 del rest[message.id] 525
525 else: 526 else:
526 for oldmsg in messages: 527 if fuzzy_matching:
527 # TODO: fuzzy matching 528 # do some fuzzy matching with difflib
528 pass 529 matches = get_close_matches(key.lower().strip(),
529 else: 530 [self._key_for(msgid) for msgid in messages], 1)
530 self[message.id] = message 531 if matches:
531 532 oldmsg = messages.pop(matches[0])
532 return rest.values() 533 message.string = oldmsg.string
534 message.flags |= oldmsg.flags | set([u'fuzzy'])
535 self[message.id] = message
536 continue
537
538 self[message.id] = message
539
540 return messages.values()
533 541
534 def _key_for(self, id): 542 def _key_for(self, id):
535 """The key for a message is just the singular ID even for pluralizable 543 """The key for a message is just the singular ID even for pluralizable
536 messages. 544 messages.
537 """ 545 """
Copyright (C) 2012-2017 Edgewall Software