Qc@sdZddlZddlZddlmZddlmZmZmZm Z ddl m Z m Z m Z mZmZddlmZmZmZdefdYZd efd YZd ejefd YZd eejfdYZdS(s( babel.support ~~~~~~~~~~~~~ Several classes and functions that help with integrating and using Babel in applications. .. note: the code in this module is not used by Babel itself :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. iN(tLocale(t format_datetformat_datetimet format_timetformat_timedelta(t format_numbertformat_decimaltformat_currencytformat_percenttformat_scientific(tPY2t text_typettext_to_nativetFormatcBseZdZddZdddZdddZdddZdddedZ d Z dd Z d Z dd Z d ZRS(sCWrapper class providing the various date and number formatting functions bound to a specific locale and time-zone. >>> from babel.util import UTC >>> from datetime import date >>> fmt = Format('en_US', UTC) >>> fmt.date(date(2007, 4, 1)) u'Apr 1, 2007' >>> fmt.decimal(1.2345) u'1.234' cCstj||_||_dS(sInitialize the formatter. :param locale: the locale identifier or `Locale` instance :param tzinfo: the time-zone info (a `tzinfo` instance or `None`) N(Rtparsetlocalettzinfo(tselfRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__init__'stmediumcCst||d|jS(sReturn a date formatted according to the given pattern. >>> from datetime import date >>> fmt = Format('en_US') >>> fmt.date(date(2007, 4, 1)) u'Apr 1, 2007' R(RR(Rtdatetformat((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR0scCst||d|jd|jS(s6Return a date and time formatted according to the given pattern. >>> from datetime import datetime >>> from pytz import timezone >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) u'Apr 1, 2007, 11:30:00 AM' RR(RRR(RtdatetimeR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR:s cCst||d|jd|jS(sReturn a time formatted according to the given pattern. >>> from datetime import datetime >>> from pytz import timezone >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) >>> fmt.time(datetime(2007, 4, 1, 15, 30)) u'11:30:00 AM' RR(RRR(RttimeR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRFs tsecondg333333?c Cs+t|d|d|d|d|d|jS(sReturn a time delta according to the rules of the given locale. >>> from datetime import timedelta >>> fmt = Format('en_US') >>> fmt.timedelta(timedelta(weeks=11)) u'3 months' t granularityt thresholdRt add_directionR(RR(RtdeltaRRRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt timedeltaQs  cCst|d|jS(sReturn an integer number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.number(1099) u'1,099' R(RR(Rtnumber((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR_scCst||d|jS(sReturn a decimal number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.decimal(1.2345) u'1.234' R(RR(RRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pytdecimalhscCst||d|jS(sHReturn a number in the given currency formatted for the locale. R(RR(RRtcurrency((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR qscCst||d|jS(sReturn a number formatted as percentage for the locale. >>> fmt = Format('en_US') >>> fmt.percent(0.34) u'34%' R(RR(RRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pytpercentvscCst|d|jS(sLReturn a number formatted using scientific notation for the locale. R(R R(RR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt scientificsN(t__name__t __module__t__doc__tNoneRRRRtFalseRRRR R!R"(((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR s       t LazyProxycBs%eZdZdddddgZdZedZdZd Zd Z d Z d Z d Z dZ dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd Zd!Z RS("sClass for proxy objects that delegate to a specified function to evaluate the actual object. >>> def greeting(name='world'): ... return 'Hello, %s!' % name >>> lazy_greeting = LazyProxy(greeting, name='Joe') >>> print lazy_greeting Hello, Joe! >>> u' ' + lazy_greeting u' Hello, Joe!' >>> u'(%s)' % lazy_greeting u'(Hello, Joe!)' This can be used, for example, to implement lazy translation functions that delay the actual translation until the string is actually used. The rationale for such behavior is that the locale of the user may not always be available. In web applications, you only know the locale when processing a request. The proxy implementation attempts to be as complete as possible, so that the lazy objects should mostly work as expected, for example for sorting: >>> greetings = [ ... LazyProxy(greeting, 'world'), ... LazyProxy(greeting, 'Joe'), ... LazyProxy(greeting, 'universe'), ... ] >>> greetings.sort() >>> for greeting in greetings: ... print greeting Hello, Joe! Hello, universe! Hello, world! t_funct_argst_kwargst_valuet_is_cache_enabledcOsu|jdt}tj|d|tj|d|tj|d|tj|d|tj|dddS(Nt enable_cacheR)R*R+R-R,(tpoptTruetobjectt __setattr__R&(Rtfunctargstkwargstis_cache_enabled((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs cCsQ|jdkrJ|j|j|j}|js4|Stj|d|n|jS(NR,(R,R&R)R*R+R-R1R2(Rtvalue((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR7s  cCs ||jkS(N(R7(Rtkey((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __contains__scCs t|jS(N(tboolR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __nonzero__scCs t|jS(N(tdirR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__dir__scCs t|jS(N(titerR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__iter__scCs t|jS(N(tlenR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__len__scCs t|jS(N(tstrR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__str__scCs t|jS(N(tunicodeR7(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __unicode__scCs |j|S(N(R7(Rtother((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__add__scCs ||jS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__radd__scCs |j|S(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__mod__scCs ||jS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__rmod__scCs |j|S(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__mul__scCs ||jS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__rmul__scOs|j||S(N(R7(RR4R5((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__call__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__lt__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__le__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__eq__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__ne__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__gt__scCs |j|kS(N(R7(RRF((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__ge__scCst|j|dS(N(tdelattrR7(Rtname((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __delattr__scCst|j|S(N(tgetattrR7(RRU((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __getattr__scCst|j||dS(N(tsetattrR7(RRUR7((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR2scCs|j|=dS(N(R7(RR8((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __delitem__scCs |j|S(N(R7(RR8((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt __getitem__scCs||j|stfpRUN( t_catalogtpluraltsuperR_RtfilterR&RWtfilestDEFAULT_DOMAINtdomaint_domains(RRc((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs  ! cCs|jj||j|S(sULike ``gettext()``, but look the message up in the specified domain. (Rktgettgettext(RRjtmessage((s7/usr/local/lib/python2.7/site-packages/babel/support.pytdgettext scCs|jj||j|S(sVLike ``lgettext()``, but look the message up in the specified domain. (RkRltlgettext(RRjRn((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt ldgettext&scCs|jj||j|S(sVLike ``ugettext()``, but look the message up in the specified domain. (RkRltugettext(RRjRn((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt udgettext,scCs"|jj||j|||S(sVLike ``ngettext()``, but look the message up in the specified domain. (RkRltngettext(RRjtsingularRetnum((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt dngettext4scCs"|jj||j|||S(sWLike ``lngettext()``, but look the message up in the specified domain. (RkRlt lngettext(RRjRuReRv((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt ldngettext:scCs"|jj||j|||S(sVLike ``ungettext()`` but look the message up in the specified domain. (RkRlt ungettext(RRjRuReRv((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt udngettext@ss%s%scCs|j||f}t}|jj||}||kr]|jrY|jj||S|S|jrvt||jS|jrt||jS|S(sLook up the `context` and `message` id in the catalog and return the corresponding message string, as an 8-bit string encoded with the catalog's charset encoding, if known. If there is no entry in the catalog for the `message` id and `context` , and a fallback has been set, the look up is forwarded to the fallback's ``pgettext()`` method. Otherwise, the `message` id is returned. ( tCONTEXT_ENCODINGR1RdRlt _fallbacktpgettextt_output_charsetR t_charset(RtcontextRnt ctxt_msg_idtmissingttmsg((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR~Os     cCs|j||f}t}|jj||}||kr]|jrY|jj||S|S|jrv|j|jS|jtj S(sEquivalent to ``pgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. ( R|R1RdRlR}t lpgettextRtencodeRtgetpreferredencoding(RRRnRRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRes    cCs|j||f}yV|j||j|f}|jrKt||jS|jrdt||jS|SWnGtk r|jr|jj||||S|dkr|S|SnXdS(s^Do a plural-forms lookup of a message id. `singular` is used as the message id for purposes of lookup in the catalog, while `num` is used to determine which plural form to use. The returned message string is an 8-bit string encoded with the catalog's charset encoding, if known. If the message id for `context` is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's ``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is returned, and ``plural`` is returned in all other cases. iN( R|RdReRR RtKeyErrorR}t npgettext(RRRuReRvRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRus      cCs|j||f}yL|j||j|f}|jrK|j|jS|jtjSWnGtk r|jr|jj ||||S|dkr|S|SnXdS(sEquivalent to ``npgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. iN( R|RdReRRRRRR}t lnpgettext(RRRuReRvRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs    cCsg|j||f}t}|jj||}||krc|jrY|jj||St|S|S(ssLook up the `context` and `message` id in the catalog and return the corresponding message string, as a Unicode string. If there is no entry in the catalog for the `message` id and `context`, and a fallback has been set, the look up is forwarded to the fallback's ``upgettext()`` method. Otherwise, the `message` id is returned. (R|R1RdRlR}t upgettextR (RRRntctxt_message_idRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs    cCs|j||f}y |j||j|f}WnZtk r|jre|jj||||S|dkrt|}qt|}nX|S(s$Do a plural-forms lookup of a message id. `singular` is used as the message id for purposes of lookup in the catalog, while `num` is used to determine which plural form to use. The returned message string is a Unicode string. If the message id for `context` is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's ``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is returned, and `plural` is returned in all other cases. i(R|RdReRR}t unpgettextR (RRRuReRvRR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs     cCs|jj||j||S(sVLike `pgettext()`, but look the message up in the specified `domain`. (RkRlR~(RRjRRn((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt dpgettextscCs|jj||j||S(sWLike `upgettext()`, but look the message up in the specified `domain`. (RkRlR(RRjRRn((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt udpgettextscCs|jj||j||S(sEquivalent to ``dpgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. (RkRlR(RRjRRn((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt ldpgettextscCs%|jj||j||||S(sWLike ``npgettext``, but look the message up in the specified `domain`. (RkRlR(RRjRRuReRv((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt dnpgettextscCs%|jj||j||||S(sXLike ``unpgettext``, but look the message up in the specified `domain`. (RkRlR(RRjRRuReRv((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt udnpgettextscCs%|jj||j||||S(sEquivalent to ``dnpgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. (RkRlR(RRjRRuReRv((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt ldnpgettextsN("R#R$R&RiRRoRqRst dugettextRwRyR{t dungettextR|R~RRRRRRRt dupgettextRRRt dunpgettextRR RmR_RrRtRz(((s7/usr/local/lib/python2.7/site-packages/babel/support.pyR_ s8                    t TranslationscBszeZdZdZdddZesBejjZ ejj Z ne ddddZ dZedZdZRS(s&An extended translation catalog class.tmessagescCs/tt|jd||p%|j|_dS(sInitialize the translations catalog. :param fp: the file-like object the translation should be read from :param domain: the message domain (default: 'messages') RcN(RfRRRiRj(RRcRj((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRsc Cs|dk rOt|ttfs-|g}ng|D]}t|^q4}n|sa|j}ntj|||}|stSt |d}|d|d|SWdQXdS(spLoad translations from the given directory. :param dirname: the directory containing the ``MO`` files :param locales: the list of locales in order of preference (items in this list can be either `Locale` objects or locale strings) :param domain: the message domain (default: 'messages') trbRcRjN( R&t isinstancetlistttupleRBRiRmtfindR_topen(tclstdirnametlocalesRjRtfilenameRc((s7/usr/local/lib/python2.7/site-packages/babel/support.pytloads  " cCs#dt|j|jjdfS(Ns <%s: "%s">sproject-id-version(ttypeR#t_infoRl(R((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt__repr__$scCst|d|j}|r7||jkr7|j|S|jj|}|rk|dk rk|j|n|j|||j|<|S(s!Add the given translations to the catalog. If the domain of the translations is different than that of the current catalog, they are added as a catalog that is only accessible by the various ``d*gettext`` functions. :param translations: the `Translations` instance with the messages to add :param merge: whether translations for message domains that have already been added should be merged with the existing translations RjN(RWRiRjtmergeRkRlR&t add_fallback(Rt translationsRRjtexisting((s7/usr/local/lib/python2.7/site-packages/babel/support.pytadd(s    cCsQt|tjrM|jj|jt|trM|jj|jqMn|S(s0Merge the given translations into the catalog. Message translations in the specified catalog override any messages with the same identifier in the existing catalog. :param translations: the `Translations` instance with the messages to merge (RRmtGNUTranslationsRdtupdateRRhtextend(RR((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRBs N(R#R$R%RiR&RR RmRRrRtRzt classmethodRRR0RR(((s7/usr/local/lib/python2.7/site-packages/babel/support.pyRs    (R%RmRt babel.coreRt babel.datesRRRRt babel.numbersRRRRR t babel._compatR R R R1R R(R_RR(((s7/usr/local/lib/python2.7/site-packages/babel/support.pyt s  "(k