From: Christopher Lenz Date: Fri, 22 Jun 2007 00:33:22 +0000 (+0000) Subject: Added preliminary catalog updating/merging functionality. X-Git-Tag: 1.0~478 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a64dfdacaa28f69258ec6d2f17fc5027d2fbd0e2;p=thirdparty%2Fbabel.git Added preliminary catalog updating/merging functionality. --- diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 23606a22..e6e1cb3d 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -471,6 +471,66 @@ class Catalog(object): self[id] = Message(id, string, list(locations), flags, auto_comments, user_comments) + def update(self, template): + """Update the catalog based on the given template catalog. + + >>> from babel.messages import Catalog + >>> template = Catalog() + >>> template.add('blue', locations=[('main.py', 100)]) + >>> template.add(('salad', 'salads'), locations=[('util.py', 42)]) + >>> catalog = Catalog(locale='de_DE') + >>> catalog.add('blue', u'blau', locations=[('main.py', 98)]) + >>> catalog.add('head', u'Kopf', locations=[('util.py', 33)]) + >>> catalog.add(('salad', 'salads'), (u'Salat', u'Salate'), + ... locations=[('util.py', 38)]) + + >>> rest = catalog.update(template) + >>> len(catalog) + 2 + + >>> msg1 = catalog['blue'] + >>> msg1.string + u'blau' + >>> msg1.locations + [('main.py', 100)] + + >>> msg2 = catalog['salad'] + >>> msg2.string + (u'Salat', u'Salate') + >>> msg2.locations + [('util.py', 42)] + + >>> 'head' in catalog + False + >>> rest + [] + + :param template: the reference catalog, usually read from a POT file + :return: a list of `Message` objects that the catalog contained before + the updated, but couldn't be found in the template + """ + rest = odict([(message.id, message) for message in self if message.id]) + messages = self._messages + self._messages = odict() + + for message in template: + if message.id: + key = self._key_for(message.id) + if key in messages: + oldmsg = messages.pop(key) + message.string = oldmsg.string + message.flags |= oldmsg.flags + self[message.id] = message + del rest[message.id] + else: + for oldmsg in messages: + # TODO: fuzzy matching + pass + else: + self[message.id] = message + + return rest.values() + def _key_for(self, id): """The key for a message is just the singular ID even for pluralizable messages. diff --git a/babel/util.py b/babel/util.py index 5c94c042..55bdddbc 100644 --- a/babel/util.py +++ b/babel/util.py @@ -76,7 +76,7 @@ class odict(dict): """ def __init__(self, data=None): dict.__init__(self, data or {}) - self._keys = [] + self._keys = dict.keys(self) def __delitem__(self, key): dict.__delitem__(self, key)