ó âÒðQc@szdZddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z ddl m Zdd lmZdd lmZdd lmZdd lmZmZmZmZdd lmZmZmZmZdddgZej dƒZ!de"fd„ƒYZ#de$fd„ƒYZ%dZ&erZd„Z'ne Z'de"fd„ƒYZ(dS(sÀ babel.messages.catalog ~~~~~~~~~~~~~~~~~~~~~~ Data structures for message catalogs. :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. iÿÿÿÿN(t parse_header(tdatetimettime(tget_close_matches(tmessage_from_string(tcopy(t __version__(tLocale(tformat_datetime(t get_plural(todicttdistincttLOCALTZtFixedOffsetTimezone(t string_typest number_typestPY2tcmptMessagetCatalogtTranslationErrors¬(?x) \% (?:\(([\w]*)\))? ( [-#0\ +]?(?:\*|[\d]+)? (?:\.(?:\*|[\d]+))? [hlL]? ) ([diouxXeEfFgGcrs%]) c Bs¹eZdZddddddddd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z dd „Zed „ƒZed„ƒZed„ƒZRS(s0Representation of a single message in a catalog.uc Csî||_| r"|jr"d}n||_tt|ƒƒ|_t|ƒ|_|rq|jrq|jj dƒn|jj dƒtt|ƒƒ|_ tt|ƒƒ|_ t |tƒrÉ|g|_nt|ƒ|_||_| |_dS(s`Create the message object. :param id: the message ID, or a ``(singular, plural)`` tuple for pluralizable messages :param string: the translated message string, or a ``(singular, plural)`` tuple for pluralizable messages :param locations: a sequence of ``(filenname, lineno)`` tuples :param flags: a set or sequence of flags :param auto_comments: a sequence of automatic comments for the message :param user_comments: a sequence of user comments for the message :param previous_id: the previous message ID, or a ``(singular, plural)`` tuple for pluralizable messages :param lineno: the line number on which the msgid line was found in the PO file, if any :param context: the message context us python-formatN(uu(tidt pluralizabletstringtlistR t locationstsettflagst python_formattaddtdiscardt auto_commentst user_commentst isinstanceRt previous_idtlinenotcontext( tselfRRRRRR R"R#R$((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__init__.s     cCs&dt|ƒj|jt|jƒfS(Ns<%s %r (flags: %r)>(ttypet__name__RRR(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__repr__Sscs.‡‡fd†}|ƒ\}}t||ƒS(s0Compare Messages, taking into account plural idscsŒtˆtƒr|ˆj}ˆj}|rE|rEˆjdˆjdfS|r_ˆjdˆjfS|r|ˆjˆjdfSnˆjˆjfS(Ni(R!RRR(tpluralt obj_plural(tobjR%(s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pytvalues_to_compareYs   (R(R%R,R-tthistother((R,R%s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__cmp__Ws cCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__gt__gscCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__lt__jscCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__ge__mscCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__le__pscCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__eq__sscCs|j|ƒdkS(Ni(R0(R%R/((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__ne__vsc CsIttt|j|j|j|j|j|j|j |j |j f ƒŒS(N( RtmapRRRRRRR R"R#R$(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pytcloneys  cCs_ddlm}g}xB|D]:}y|||ƒWqtk rV}|j|ƒqXqW|S(s•Run various validation checks on the message. Some validations are only performed if the catalog is provided. This method returns a sequence of `TranslationError` objects. :rtype: ``iterator`` :param catalog: A catalog instance that is passed to the checkers :see: `Catalog.check` for a way to perform checks for all messages in a catalog. iÿÿÿÿ(tcheckers(tbabel.messages.checkersR9Rtappend(R%tcatalogR9terrorstcheckerte((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pytchecks  cCs d|jkS(sWhether the translation is fuzzy. >>> Message('foo').fuzzy False >>> msg = Message('foo', 'foo', flags=['fuzzy']) >>> msg.fuzzy True >>> msg :type: `bool`tfuzzy(R(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyRA’s cCst|jttfƒS(s¯Whether the message is plurizable. >>> Message('foo').pluralizable False >>> Message(('foo', 'bar')).pluralizable True :type: `bool`(R!RRttuple(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR¡s cCs>|j}t|ttfƒs*|g}ntd„|DƒƒS(sâWhether the message contains Python-style parameters. >>> Message('foo %(name)s bar').python_format True >>> Message(('foo %(name)s', 'foo %(name)s')).python_format True :type: `bool`css|]}tj|ƒVqdS(N(t PYTHON_FORMATtsearch(t.0R((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pys ºs(RR!RRBtany(R%tids((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR­s  (((((N(R(t __module__t__doc__tNoneR&R)R0R1R2R3R4R5R6R8R@tpropertyRARR(((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR+s  $           cBseZdZRS(s_Exception thrown by translation checkers when invalid message translations are encountered.(R(RHRI(((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR½su¸# Translations template for PROJECT. # Copyright (C) YEAR ORGANIZATION # This file is distributed under the same license as the PROJECT project. # FIRST AUTHOR , YEAR. #cCsdt|jdƒƒ}i}xB|jƒD]4\}}|jdƒ}|jdƒ}|||sLANGUAGE sutf-8ttzinfosYEAR-MO-DA HO:MI+ZONEN(tdomainRtparsetlocalet_header_commentR t _messagestprojecttversiontcopyright_holdertmsgid_bugs_addresstlast_translatort language_teamtcharsetRJRtnowR R!RYtreplacet creation_datet revision_dateRAtobsoletet _num_pluralst _plural_expr(R%R\RZtheader_commentR_R`RaRbRhRiRcRdReRA((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR&Üs6            cCs³|j}tjtƒjdƒ}t|jdƒrH|jjdƒ}n|jd|jƒjd|j ƒjd|ƒjd|j ƒ}|j r¯|jdd|j j ƒ}n|S( Ns%YtstrftimeRVRWtYEARRXsTranslations templates%s translations( R]RRfR RnthasattrRiRgR_R`RaR\t english_name(R%tcommenttyear((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt_get_header_comments    cCs ||_dS(N(R](R%R((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt_set_header_comment&stdocsƒ The header comment for the catalog. >>> catalog = Catalog(project='Foobar', version='1.0', ... copyright_holder='Foo Company') >>> print catalog.header_comment #doctest: +ELLIPSIS # Translations template for Foobar. # Copyright (C) ... Foo Company # This file is distributed under the same license as the Foobar project. # FIRST AUTHOR , .... # The header can also be set from a string. Any known upper-case variables will be replaced when the header is retrieved again: >>> catalog = Catalog(project='Foobar', version='1.0', ... copyright_holder='Foo Company') >>> catalog.header_comment = '''\ ... # The POT for my really cool PROJECT project. ... # Copyright (C) 1990-2003 ORGANIZATION ... # This file is distributed under the same license as the PROJECT ... # project. ... #''' >>> print catalog.header_comment # The POT for my really cool Foobar project. # Copyright (C) 1990-2003 Foo Company # This file is distributed under the same license as the Foobar # project. # :type: `unicode` cCs­g}|jdd|j|jffƒ|jd|jfƒ|jdt|jdddƒfƒt|jtt ft ƒr¨|jdt|jdddƒfƒn|jd|jfƒ|jd |j fƒ|j dk r d |jkr |jd |jjd t|j ƒƒfƒn|jd |jfƒ|j dk r^|jd |jfƒn|jdƒ|jdd|jfƒ|jdƒ|jddtfƒ|S(NsProject-Id-Versions%s %ssReport-Msgid-Bugs-TosPOT-Creation-Datesyyyy-MM-dd HH:mmZR\tensPO-Revision-DatesLast-TranslatortLANGUAGEs Language-Teams Plural-Formss MIME-Versions1.0s Content-Typestext/plain; charset=%ssContent-Transfer-Encodingt8bits Generated-Bys Babel %s (s MIME-Versions1.0(sContent-Transfer-EncodingRy(R;R_R`RbRRhR!RiRttime_RRcR\RJRdRgtstrt plural_formsReRW(R%RQ((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt_get_mime_headersJs4          cCs3x,|D]$\}}|jƒ}|dkr`|jdƒ}dj|d ƒ|_|d|_q|dkrx||_q|dkr||_q|dkr¨||_q|dkrët|ƒ\}}d |kr+|d jƒ|_ q+q|d kr@td |ƒ\}}t |j d d ƒƒ|_ |j ddƒ|_ q|dkr.tjd|dƒ\}}}tj|dƒ} tj| ƒ} |d|d} } | d | d } }t | dƒ}t | ƒ}t |ƒ}|d}||7}||9}t|ƒ}tj| ƒ}|jd|ƒ|_q|dkrd|kr+tjd|dƒ\}}}tj|dƒ} tj| ƒ} |d|d} } | d | d } }t | dƒ}t | ƒ}t |ƒ}|d}||7}||9}t|ƒ}tj| ƒ}|jd|ƒ|_q+qqWdS(Nsproject-id-versiont u iÿÿÿÿsreport-msgid-bugs-toslast-translators language-teams content-typeRes plural-formss ;tnpluralsiR*s(n != 1)spot-creation-dates ([+-]\d{4})$is%Y-%m-%d %H:%Mit1i<RYspo-revision-dateRo(tlowertsplittjoinR_R`RbRcRdRRetinttgetRkRltreRtstrptimetmktimeR Rt fromtimestampRgRhRi(R%RQRSRTtpartstmimetypetparamst_ttzoffsettttttst plus_minus_strestthours_offset_st mins_offset_st plus_minust hours_offsett mins_offsettnet_mins_offsettdt((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt_set_mime_headershsf                          s­ The MIME headers of the catalog, used for the special ``msgid ""`` entry. The behavior of this property changes slightly depending on whether a locale is set or not, the latter indicating that the catalog is actually a template for actual translations. Here's an example of the output for such a catalog template: >>> from babel.dates import UTC >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC) >>> catalog = Catalog(project='Foobar', version='1.0', ... creation_date=created) >>> for name, value in catalog.mime_headers: ... print '%s: %s' % (name, value) Project-Id-Version: Foobar 1.0 Report-Msgid-Bugs-To: EMAIL@ADDRESS POT-Creation-Date: 1990-04-01 15:30+0000 PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE Last-Translator: FULL NAME Language-Team: LANGUAGE MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Generated-By: Babel ... And here's an example of the output when the locale is set: >>> revised = datetime(1990, 8, 3, 12, 0, tzinfo=UTC) >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0', ... creation_date=created, revision_date=revised, ... last_translator='John Doe ', ... language_team='de_DE ') >>> for name, value in catalog.mime_headers: ... print '%s: %s' % (name, value) Project-Id-Version: Foobar 1.0 Report-Msgid-Bugs-To: EMAIL@ADDRESS POT-Creation-Date: 1990-04-01 15:30+0000 PO-Revision-Date: 1990-08-03 12:00+0000 Last-Translator: John Doe Language-Team: de_DE Plural-Forms: nplurals=2; plural=(n != 1) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Generated-By: Babel ... :type: `list` cCsG|jdkr@d}|jr4t|jƒd}n||_n|jS(s¹The number of plurals used by the catalog or locale. >>> Catalog(locale='en').num_plurals 2 >>> Catalog(locale='ga').num_plurals 3 :type: `int`iiN(RkRJR\R (R%tnum((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt num_pluralsês   cCsG|jdkr@d}|jr4t|jƒd}n||_n|jS(såThe plural expression used by the catalog or locale. >>> Catalog(locale='en').plural_expr '(n != 1)' >>> Catalog(locale='ga').plural_expr '(n==1 ? 0 : n==2 ? 1 : 2)' :type: `string_types`s(n != 1)iN(RlRJR\R (R%texpr((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt plural_exprûs   cCsd|j|jfS(sôReturn the plural forms declaration for the locale. >>> Catalog(locale='en').plural_forms 'nplurals=2; plural=(n != 1)' >>> Catalog(locale='pt_BR').plural_forms 'nplurals=2; plural=(n > 1)' :type: `str`snplurals=%s; plural=%s(RœRž(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR| s cCs|j|ƒ|jkS(s?Return whether the catalog has a message with the specified ID.(t_key_forR^(R%R((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt __contains__scCs t|jƒS(seThe number of messages in the catalog. This does not include the special ``msgid ""`` entry.(tlenR^(R%((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__len__sccs g}x.|jD]#\}}|jd||fƒqWtƒ}|jr_|tdgƒO}ntddj|ƒd|ƒVx|jD]}|j|Vq†WdS(s¡Iterates through all the entries in the catalog, in the order they were added, yielding a `Message` object for every entry. :rtype: ``iterator``s%s: %sRAus RN(t mime_headersR;RRARRƒR^(R%tbufRSRTRtkey((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt__iter__"s  cCs<d}|jrd|j}ndt|ƒj|j|fS(Nts %ss <%s %r%s>(R\R'R(RZ(R%R\((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR)1s cCs|j|ƒdS(s)Delete the message with the specified ID.N(tdelete(R%R((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt __delitem__7scCs |j|ƒS(sUReturn the message with the specified ID. :param id: the message ID (R…(R%R((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt __getitem__;scCsa|j||jƒ}|jj|ƒ}|rÓ|jr[|j r[|j|_|j|_ntt|j |j ƒƒ|_ tt|j |j ƒƒ|_ tt|j |j ƒƒ|_ |j |j O_ |}nŠ|dkr8t |jƒjƒ|_djg|j D]}d|jƒ^qƒ|_|j|_n%t|ttfƒrPn||j|>> catalog = Catalog() >>> catalog[u'foo'] = Message(u'foo') >>> catalog[u'foo'] If a message with that ID is already in the catalog, it is updated to include the locations and flags of the new message. >>> catalog = Catalog() >>> catalog[u'foo'] = Message(u'foo', locations=[('main.py', 1)]) >>> catalog[u'foo'].locations [('main.py', 1)] >>> catalog[u'foo'] = Message(u'foo', locations=[('utils.py', 5)]) >>> catalog[u'foo'].locations [('main.py', 1), ('utils.py', 5)] :param id: the message ID :param message: the `Message` object R§s s# %sN(RŸR$R^R…RRRRR RRR RRURNR£RƒtrstripRmRAR!RB(R%RtmessageR¥tcurrenttc((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt __setitem__Bs,       )c Cs>t||t|ƒ||||d|d| ƒ} | ||<| S(suAdd or update the message with the specified ID. >>> catalog = Catalog() >>> catalog.add(u'foo') >>> catalog[u'foo'] This method simply constructs a `Message` object with the given arguments and invokes `__setitem__` with that object. :param id: the message ID, or a ``(singular, plural)`` tuple for pluralizable messages :param string: the translated message string, or a ``(singular, plural)`` tuple for pluralizable messages :param locations: a sequence of ``(filenname, lineno)`` tuples :param flags: a set or sequence of flags :param auto_comments: a sequence of automatic comments :param user_comments: a sequence of user comments :param previous_id: the previous message ID, or a ``(singular, plural)`` tuple for pluralizable messages :param lineno: the line number on which the msgid line was found in the PO file, if any :param context: the message context R#R$(RR( R%RRRRRR R"R#R$R¬((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyRts   ccsDx=|jjƒD],}|jd|ƒ}|r||fVqqWdS(sBRun various validation checks on the translations in the catalog. For every message which fails validation, this method yield a ``(message, errors)`` tuple, where ``message`` is the `Message` object and ``errors`` is a sequence of `TranslationError` objects. :rtype: ``iterator`` R<N(R^tvaluesR@(R%R¬R=((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR@•s cCs|jj|j||ƒƒS(s©Return the message with the specified ID and context. :param id: the message ID :param context: the message context, or ``None`` for no context (R^R…RŸ(R%RR$((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR…£scCs2|j||ƒ}||jkr.|j|=ndS(s©Delete the message with the specified ID and context. :param id: the message ID :param context: the message context, or ``None`` for no context N(RŸR^(R%RR$R¥((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyR¨«sc sáˆj‰ˆjƒ‰tƒˆ_g}|sxtgˆD]5}|r7ˆ|jr7ˆj|ƒˆ|jf^q7ƒ}ntƒ‰‡‡‡‡fd†}xþ|D]ö}|jr ˆj|j|jƒ}|ˆkræ||||ƒq–|t kr†t |t ƒr|d}n|}t |j ƒjƒ|jƒdƒ} | r†| d} || } | dk rm| | f} n||| |ƒq q†n|ˆ|j>> from babel.messages import Catalog >>> template = Catalog() >>> template.add('green', locations=[('main.py', 99)]) >>> 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)]) >>> catalog.update(template) >>> len(catalog) 3 >>> msg1 = catalog['green'] >>> msg1.string >>> msg1.locations [('main.py', 99)] >>> msg2 = catalog['blue'] >>> msg2.string u'blau' >>> msg2.locations [('main.py', 100)] >>> msg3 = catalog['salad'] >>> msg3.string (u'Salat', u'Salate') >>> msg3.locations [('util.py', 42)] Messages that are in the catalog but not in the template are removed from the main collection, but can still be accessed via the `obsolete` member: >>> 'head' in catalog False >>> catalog.obsolete.values() [] :param template: the reference catalog, usually read from a POT file :param no_fuzzy_matching: whether to use fuzzy matching of message IDs csµ|jƒ}t}||kryt}ˆj|ƒˆj|ƒ}t|jtƒrd|jg|_q‹t |jƒ|_nˆj |dƒ}|j |_ t|jt t fƒr@t|j t t fƒsýt}t |j gdgt|jƒdƒ|_ qqt|j ƒˆjkrqt}t |j t|j ƒ ƒ|_ qqn1t|j t t fƒrqt}|j d|_ n|j|jO_|r¤|jtdgƒO_n|ˆ|j   8      P 0        2    €()RIR†RtcgiRRRztdifflibRtemailRRtbabelRRWt babel.coreRt babel.datesRtbabel.messages.pluralsR t babel.utilR R R R t babel._compatRRRRt__all__tcompileRCtobjectRt ExceptionRRÆRUR(((s@/usr/local/lib/python2.7/site-packages/babel/messages/catalog.pyt s.  "" ’