o V\W@sdZddlZddlZddlmZddlmZmZmZm Z ddl m Z m Z m Z mZmZddlmZmZmZGdddeZGd d d eZGd d d ejeZGd ddeejZdS)a- 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-2019 by the Babel Team. :license: BSD, see LICENSE for more details. N)Locale) format_dateformat_datetime format_timeformat_timedelta) format_numberformat_decimalformat_currencyformat_percentformat_scientific)PY2 text_typetext_to_nativec@sreZdZdZdddZdddZddd Zdd d Z dddZddZ dddZ ddZ dddZ ddZ dS)FormataCWrapper 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' NcCst||_||_dS)zInitialize the formatter. :param locale: the locale identifier or `Locale` instance :param tzinfo: the time-zone info (a `tzinfo` instance or `None`) N)rparselocaletzinfo)selfrrr//usr/lib/python3/dist-packages/babel/support.py__init__'s  zFormat.__init__mediumcCt|||jdS)zReturn 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)rdateformatrrrr0sz Format.datecCt|||j|jdS)a6Return 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)rdatetimerrrrr:s zFormat.datetimecCr)aReturn 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' r)rrr)rtimerrrrrFs z Format.timesecond333333?FcCst||||||jdS)zReturn 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' ) granularity thresholdr add_directionr)rr)rdeltar"r#rr$rrr timedeltaQs  zFormat.timedeltacCt||jdS)zReturn an integer number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.number(1099) u'1,099' r)rrrnumberrrrr)_sz Format.numbercCr)zReturn a decimal number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.decimal(1.2345) u'1.234' r)rrrr)rrrrdecimalhzFormat.decimalcCr)zHReturn a number in the given currency formatted for the locale. r)r r)rr)currencyrrrr-qszFormat.currencycCr)zReturn a number formatted as percentage for the locale. >>> fmt = Format('en_US') >>> fmt.percent(0.34) u'34%' r)r rr*rrrpercentvr,zFormat.percentcCr')zLReturn a number formatted using scientific notation for the locale. r)r rr(rrr scientificszFormat.scientificN)Nr)r r!rF)__name__ __module__ __qualname____doc__rrrrr&r)r+r-r.r/rrrrrs      rc@s eZdZdZgdZddZeddZddZd d Z d d Z d dZ ddZ ddZ ddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Z d9d:Z!d;d<Z"d=d>Z#d?S)@ LazyProxyaClass 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! )_func_args_kwargs_value_is_cache_enabledcOsV|dd}t|d|t|d|t|d|t|d|t|dddS)N enable_cacheTr6r7r8r:r9)popobject __setattr__)rfuncargskwargsis_cache_enabledrrrrs zLazyProxy.__init__cCs<|jdur|j|ji|j}|js|St|d||jS)Nr9)r9r6r7r8r:r=r>)rvaluerrrrCs zLazyProxy.valuecCs ||jvSr0rCrkeyrrr __contains__ zLazyProxy.__contains__cC t|jSr0)boolrCrrrr __nonzero__rHzLazyProxy.__nonzero__cCrIr0)dirrCrKrrr__dir__rHzLazyProxy.__dir__cCrIr0)iterrCrKrrr__iter__rHzLazyProxy.__iter__cCrIr0)lenrCrKrrr__len__rHzLazyProxy.__len__cCrIr0)strrCrKrrr__str__rHzLazyProxy.__str__cCrIr0)unicoderCrKrrr __unicode__rHzLazyProxy.__unicode__cCs |j|Sr0rDrotherrrr__add__rHzLazyProxy.__add__cCs ||jSr0rDrWrrr__radd__rHzLazyProxy.__radd__cCs |j|Sr0rDrWrrr__mod__rHzLazyProxy.__mod__cCs ||jSr0rDrWrrr__rmod__rHzLazyProxy.__rmod__cCs |j|Sr0rDrWrrr__mul__rHzLazyProxy.__mul__cCs ||jSr0rDrWrrr__rmul__rHzLazyProxy.__rmul__cOs|j|i|Sr0rD)rr@rArrr__call__zLazyProxy.__call__cCs |j|kSr0rDrWrrr__lt__rHzLazyProxy.__lt__cCs |j|kSr0rDrWrrr__le__rHzLazyProxy.__le__cCs |j|kSr0rDrWrrr__eq__rHzLazyProxy.__eq__cCs |j|kSr0rDrWrrr__ne__rHzLazyProxy.__ne__cCs |j|kSr0rDrWrrr__gt__rHzLazyProxy.__gt__cCs |j|kSr0rDrWrrr__ge__rHzLazyProxy.__ge__cCst|j|dSr0)delattrrCrnamerrr __delattr__r`zLazyProxy.__delattr__cCs t|j|Sr0)getattrrCrhrrr __getattr__ zLazyProxy.__getattr__cCst|j||dSr0)setattrrC)rrirCrrrr>szLazyProxy.__setattr__cCs |j|=dSr0rDrErrr __delitem__rmzLazyProxy.__delitem__cCs |j|Sr0rDrErrr __getitem__rHzLazyProxy.__getitem__cCs||j|<dSr0rD)rrFrCrrr __setitem__szLazyProxy.__setitem__cCs"t|jg|jRd|ji|jS)Nr;)r5r6r7r:r8rKrrr__copy__ szLazyProxy.__copy__cCsFddlm}t||j|g||j|Rd||j|i||j|S)Nr)deepcopyr;)copyrsr5r6r7r:r8)rmemorsrrr __deepcopy__s     zLazyProxy.__deepcopy__N)$r1r2r3r4 __slots__rpropertyrCrGrLrNrPrRrTrVrYrZr[r\r]r^r_rarbrcrdrerfrjrlr>rorprqrrrvrrrrr5sD"  r5cseZdZdZd)fdd ZddZddZdd ZeZd d Z d d Z ddZ e Z dZ ddZddZddZddZddZddZddZdd ZeZd!d"Zd#d$Zd%d&ZeZd'd(ZesmejjZejj Z!Z"SZ"S)*NullTranslationsNcsNi|_dd|_tt|j|dttdt|ddg|_|j |_ i|_ dS)aInitialize a simple translations class which is not backed by a real catalog. Behaves similar to gettext.NullTranslations but also offers Babel's on *gettext methods (e.g. 'dgettext()'). :param fp: a file-like object (ignored in this class) cSs t|dkS)N)int)nrrr+s z+NullTranslations.__init__..fpNri) _catalogpluralsuperryrlistfilterrkfilesDEFAULT_DOMAINdomain_domains)rr __class__rrr s   zNullTranslations.__init__cC|j|||S)zULike ``gettext()``, but look the message up in the specified domain. )rgetgettextrrmessagerrrdgettext1zNullTranslations.dgettextcCr)zVLike ``lgettext()``, but look the message up in the specified domain. )rrlgettextrrrr ldgettext7rzNullTranslations.ldgettextcCr)zVLike ``ugettext()``, but look the message up in the specified domain. )rrugettextrrrr udgettext=rzNullTranslations.udgettextcC|j|||||S)zVLike ``ngettext()``, but look the message up in the specified domain. )rrngettextrrsingularrnumrrr dngettextEzNullTranslations.dngettextcCr)zWLike ``lngettext()``, but look the message up in the specified domain. )rr lngettextrrrr ldngettextKrzNullTranslations.ldngettextcCr)zVLike ``ungettext()`` but look the message up in the specified domain. )rr ungettextrrrr udngettextQrzNullTranslations.udngettextz%s%scCsj|j||f}t}|j||}||ur!|jr|j||S|S|jr*t||jS|jr3t||jS|S)aLook 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. ) CONTEXT_ENCODINGr=rr _fallbackpgettext_output_charsetr_charsetrcontextr ctxt_msg_idmissingtmsgrrrr`s  zNullTranslations.pgettextcCsb|j||f}t}|j||}||ur!|jr|j||S|S|jr*||jS|t S)zEquivalent to ``pgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. ) rr=rrr lpgettextrencodergetpreferredencodingrrrrrvs zNullTranslations.lpgettextc Cs|j||f}z!|j|||f}|jrt||jWS|jr&t||jWS|WStyI|jr=|j||||YS|dkrE|YS|YSw)a^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. rz) rrrrrrKeyErrorr npgettextrrrrrrrrrrrs  zNullTranslations.npgettextc Cs|j||f}z|j|||f}|jr||jWS|tWStyD|jr8|j ||||YS|dkr@|YS|YSw)zEquivalent to ``npgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. rz) rrrrrrrrr lnpgettextrrrrrs zNullTranslations.lnpgettextcCsJ|j||f}t}|j||}||ur#|jr|j||St|S|S)asLook 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. )rr=rrr upgettextr )rrrctxt_message_idrrrrrrszNullTranslations.upgettextc Csx|j||f}z |j|||f}W|Sty;|jr)|j||||YS|dkr4t|}Y|St|}Y|Sw)a$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. rz)rrrrr unpgettextr )rrrrrrrrrrrs    zNullTranslations.unpgettextcC|j||||S)zVLike `pgettext()`, but look the message up in the specified `domain`. )rrrrrrrrrr dpgettextzNullTranslations.dpgettextcCr)zWLike `upgettext()`, but look the message up in the specified `domain`. )rrrrrrr udpgettextrzNullTranslations.udpgettextcCr)zEquivalent to ``dpgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. )rrrrrrr ldpgettextszNullTranslations.ldpgettextcC|j||||||S)zWLike ``npgettext``, but look the message up in the specified `domain`. )rrrrrrrrrrrr dnpgettextzNullTranslations.dnpgettextcCr)zXLike ``unpgettext``, but look the message up in the specified `domain`. )rrrrrrr udnpgettextrzNullTranslations.udnpgettextcCr)zEquivalent to ``dnpgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. )rrrrrrr ldnpgettextszNullTranslations.ldnpgettextr0)#r1r2r3rrrrr dugettextrrr dungettextrrrrrrrrr dupgettextrrr dunpgettextrr rryrrr __classcell__rrrrrys<rycsbeZdZdZdZdfdd ZesejjZ ejj Z e dddZ dd Zdd d Zd dZZS) Translationsz&An extended translation catalog class.messagesNcs"tt|j|d|p |j|_dS)zInitialize the translations catalog. :param fp: the file-like object the translation should be read from :param domain: the message domain (default: 'messages') r~N)rrrrr)rrrrrrrszTranslations.__init__cCs|durt|ttfs|g}dd|D}|s|j}t|||}|s&tSt|d}|||dWdS1s,sz%Translations.load..rb)rr) isinstancertuplerrfindryopen)clsdirnamelocalesrfilenamerrrrloads   $zTranslations.loadcCsdt|j|jdfS)Nz <%s: "%s">zproject-id-version)typer1_inforrKrrr__repr__5s  zTranslations.__repr__TcCsdt|d|j}|r||jkr||S|j|}|r&|dur&|||S||||j|<|S)a!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 rN)rkrrmergerr add_fallback)r translationsrrexistingrrradd9s      zTranslations.addcCs6t|tjr|j|jt|tr|j|j|S)a0Merge 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 )rrGNUTranslationsrupdaterrextend)rrrrrrSs  zTranslations.merge)NN)NNN)T)r1r2r3r4rrr rrrrr classmethodrrrrrrrrrr s   r)r4rr babel.corer babel.datesrrrr babel.numbersrrr r r babel._compatr r rr=rr5ryrrrrrrs  kr