a `^X@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-2021 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@sneZdZdZdddZdddZddd Zdd d Zd ddZddZ d!ddZ ddZ d"ddZ 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)selfrrr1/usr/lib/python3.9/site-packages/babel/support.py__init__'s zFormat.__init__mediumcCst|||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.datecCst|||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.datetimecCst|||j|jdS)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?longFcCst||||||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)rZdeltar!r"rr#rrr timedeltaQs zFormat.timedeltacCst||jdS)zReturn an integer number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.number(1099) u'1,099' r)rrrnumberrrrr&_sz Format.numbercCst|||jdS)zReturn a decimal number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.decimal(1.2345) u'1.234' r)rrrr&rrrrdecimalhszFormat.decimalcCst|||jdS)zHReturn a number in the given currency formatted for the locale. r)r r)rr&currencyrrrr)qszFormat.currencycCst|||jdS)zReturn a number formatted as percentage for the locale. >>> fmt = Format('en_US') >>> fmt.percent(0.34) u'34%' r)r rr'rrrpercentvszFormat.percentcCst||jdS)zLReturn a number formatted using scientific notation for the locale. r)r rr%rrr scientificszFormat.scientific)N)Nr)Nr)Nr)rrr F)N)N)__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_enabled_attribute_errorcOsd|dd}t|d|t|d|t|d|t|d|t|ddt|dddS) N enable_cacheTr1r2r3r5r4r6)popobject __setattr__)rfuncargskwargsZis_cache_enabledrrrrs zLazyProxy.__init__c Csv|jdurpz|j|ji|j}Wn4tyV}zt|d|WYd}~n d}~00|jsb|St|d||jS)Nr6r4)r4r1r2r3AttributeErrorr9r:r5)rvalueerrorrrrr?s zLazyProxy.valuecCs ||jvSNr?rkeyrrr __contains__szLazyProxy.__contains__cCs t|jSrA)boolr?rrrr __nonzero__szLazyProxy.__nonzero__cCs t|jSrA)dirr?rGrrr__dir__szLazyProxy.__dir__cCs t|jSrA)iterr?rGrrr__iter__szLazyProxy.__iter__cCs t|jSrA)lenr?rGrrr__len__szLazyProxy.__len__cCs t|jSrA)strr?rGrrr__str__szLazyProxy.__str__cCs t|jSrA)Zunicoder?rGrrr __unicode__szLazyProxy.__unicode__cCs |j|SrArBrotherrrr__add__szLazyProxy.__add__cCs ||jSrArBrRrrr__radd__szLazyProxy.__radd__cCs |j|SrArBrRrrr__mod__szLazyProxy.__mod__cCs ||jSrArBrRrrr__rmod__szLazyProxy.__rmod__cCs |j|SrArBrRrrr__mul__szLazyProxy.__mul__cCs ||jSrArBrRrrr__rmul__szLazyProxy.__rmul__cOs|j|i|SrArB)rr<r=rrr__call__szLazyProxy.__call__cCs |j|kSrArBrRrrr__lt__szLazyProxy.__lt__cCs |j|kSrArBrRrrr__le__szLazyProxy.__le__cCs |j|kSrArBrRrrr__eq__szLazyProxy.__eq__cCs |j|kSrArBrRrrr__ne__szLazyProxy.__ne__cCs |j|kSrArBrRrrr__gt__szLazyProxy.__gt__cCs |j|kSrArBrRrrr__ge__szLazyProxy.__ge__cCst|j|dSrA)delattrr?rnamerrr __delattr__szLazyProxy.__delattr__cCs|jdur|jt|j|SrA)r6getattrr?rbrrr __getattr__s zLazyProxy.__getattr__cCst|j||dSrA)setattrr?)rrcr?rrrr:szLazyProxy.__setattr__cCs |j|=dSrArBrCrrr __delitem__ szLazyProxy.__delitem__cCs |j|SrArBrCrrr __getitem__ szLazyProxy.__getitem__cCs||j|<dSrArB)rrDr?rrr __setitem__szLazyProxy.__setitem__cCs"t|jg|jRd|ji|jS)Nr7)r0r1r2r5r3rGrrr__copy__szLazyProxy.__copy__cCsFddlm}t||j|g||j|Rd||j|i||j|S)Nr)deepcopyr7)copyrlr0r1r2r5r3)rmemorlrrr __deepcopy__s     zLazyProxy.__deepcopy__N)$r,r-r.r/ __slots__rpropertyr?rErHrJrLrNrPrQrTrUrVrWrXrYrZr[r\r]r^r_r`rdrfr:rhrirjrkrorrrrr0sB"  r0cseZdZdZd)fdd ZddZddZdd ZeZd d Z d d Z ddZ e Z dZ ddZddZddZddZddZddZddZdd ZeZd!d"Zd#d$Zd%d&ZeZd'd(ZesejjZejj Z!Z"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)nrrr3z+NullTranslations.__init__..fpNrc) _catalogpluralsuperrrrlistfilterrefilesDEFAULT_DOMAINdomain_domains)rry __class__rrr(s  zNullTranslations.__init__cCs|j|||S)zULike ``gettext()``, but look the message up in the specified domain. )rgetgettextrrmessagerrrdgettext9szNullTranslations.dgettextcCs|j|||S)zVLike ``lgettext()``, but look the message up in the specified domain. )rrlgettextrrrr ldgettext?szNullTranslations.ldgettextcCs|j|||S)zVLike ``ugettext()``, but look the message up in the specified domain. )rrugettextrrrr udgettextEszNullTranslations.udgettextcCs|j|||||S)zVLike ``ngettext()``, but look the message up in the specified domain. )rrngettextrrsingularr{numrrr dngettextMszNullTranslations.dngettextcCs|j|||||S)zWLike ``lngettext()``, but look the message up in the specified domain. )rr lngettextrrrr ldngettextSszNullTranslations.ldngettextcCs|j|||||S)zVLike ``ungettext()`` but look the message up in the specified domain. )rr ungettextrrrr udngettextYszNullTranslations.udngettextz%s%scCsj|j||f}t}|j||}||urB|jr>|j||S|S|jrTt||jS|jrft||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_ENCODINGr9rzr _fallbackpgettext_output_charsetr_charsetrcontextr ctxt_msg_idmissingtmsgrrrrhs  zNullTranslations.pgettextcCsb|j||f}t}|j||}||urB|jr>|j||S|S|jrT||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()``. ) rr9rzrr lpgettextrencodergetpreferredencodingrrrrr~s zNullTranslations.lpgettextc Cs|j||f}zB|j|||f}|jr8t||jWS|jrLt||jWS|WSty|jrz|j||||YS|dkr|YS|YSYn0dS)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. rsN) rrzr{rrrKeyErrorr npgettextrrrr{rrrrrrrs  zNullTranslations.npgettextc Cs|j||f}z8|j|||f}|jr8||jWS|tWSty|jrp|j ||||YS|dkr|YS|YSYn0dS)zEquivalent to ``npgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. rsN) rrzr{rrrrrr lnpgettextrrrrrs zNullTranslations.lnpgettextcCsJ|j||f}t}|j||}||urF|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. )rr9rzrr upgettextr )rrrctxt_message_idrrrrrrszNullTranslations.upgettextc Cst|j||f}z|j|||f}WnHtyn|jrP|j||||YS|dkrbt|}nt|}Yn0|S)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. rs)rrzr{rr unpgettextr )rrrr{rrrrrrrs   zNullTranslations.unpgettextcCs|j||||S)zVLike `pgettext()`, but look the message up in the specified `domain`. )rrrrrrrrrr dpgettextszNullTranslations.dpgettextcCs|j||||S)zWLike `upgettext()`, but look the message up in the specified `domain`. )rrrrrrr udpgettextszNullTranslations.udpgettextcCs|j||||S)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.ldpgettextcCs|j||||||S)zWLike ``npgettext``, but look the message up in the specified `domain`. )rrrrrrrr{rrrr dnpgettextszNullTranslations.dnpgettextcCs|j||||||S)zXLike ``unpgettext``, but look the message up in the specified `domain`. )rrrrrrr udnpgettextszNullTranslations.udnpgettextcCs|j||||||S)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.ldnpgettext)N)#r,r-r.rrrrrZ dugettextrrrZ dungettextrrrrrrrrrZ dupgettextrrrZ dunpgettextrr rrrrrr __classcell__rrrrrr$s8rrcsbeZdZdZdZdfdd Zes2ejjZ 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') rxN)r|rrrr)rryrrrrrszTranslations.__init__cCs|dur*t|ttfs|g}dd|D}|s4|j}t|||}|sLtSt|d}|||dWdS1sx0YdS)apLoad 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') NcSsg|] }t|qSr)rO).0rrrr 4rwz%Translations.load..rb)ryr) isinstancer}tuplerrfindrropen)clsdirnameZlocalesrfilenameryrrrload's  zTranslations.loadcCsdt|j|jdfS)Nz <%s: "%s">zproject-id-version)typer,_inforrGrrr__repr__=s  zTranslations.__repr__TcCsbt|d|j}|r&||jkr&||S|j|}|rJ|durJ||n||||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)rerrmergerr add_fallback)r translationsrrexistingrrraddAs       zTranslations.addcCs6t|tjr2|j|jt|tr2|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 )rrGNUTranslationsrzupdaterrextend)rrrrrr[s  zTranslations.merge)NN)NNN)T)r,r-r.r/rrr rrrrr classmethodrrrrrrrrrrs   r)r/rrZ babel.corerZ babel.datesrrrrZ babel.numbersrrr r r Z babel._compatr r rr9rr0rrrrrrrrs  k r