3

X'íc¤Vã@s–dZddlZddlZddlmZddlmZmZmZm	Z	ddl
mZmZm
Z
mZGdd„dƒZGdd	„d	ƒZGd
d„dejƒZGdd
„d
eejƒZdS)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-2022 by the Babel Team.
    :license: BSD, see LICENSE for more details.
éN)ÚLocale)Úformat_dateÚformat_datetimeÚformat_timeÚformat_timedelta)Úformat_decimalÚformat_currencyÚformat_percentÚformat_scientificc@sneZdZdZddd„Zddd„Zddd	„Zdd
d„Zd dd„Zdd„Z	d!dd„Z
dd„Zd"dd„Zdd„Z
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'
    NcCstj|ƒ|_||_dS)z¯Initialize the formatter.

        :param locale: the locale identifier or `Locale` instance
        :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
        N)rÚparseÚlocaleÚtzinfo)Úselfr
r©rú./tmp/pip-build-gk9425m9/babel/babel/support.pyÚ__init__%szFormat.__init__ÚmediumcCst|||jdS)zÇReturn 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
)rÚdateÚformatrrrr.szFormat.datecCst|||j|jdS)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
)rÚdatetimerrrrr8s	
zFormat.datetimecCst|||j|jdS)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'
        )rr
)rrr
)rÚtimerrrrrDs	zFormat.timeÚsecondç333333ë?ÚlongFcCst||||||jdS)zÙReturn 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
)rÚdeltarrrrrrrÚ	timedeltaOs	zFormat.timedeltacCst||jdS)zŒReturn an integer number formatted for the locale.

        >>> fmt = Format('en_US')
        >>> fmt.number(1099)
        u'1,099'
        )r
)rr
)rÚnumberrrrr ]sz
Format.numbercCst|||jdS)zŽReturn a decimal number formatted for the locale.

        >>> fmt = Format('en_US')
        >>> fmt.decimal(1.2345)
        u'1.234'
        )r
)rr
)rr rrrrÚdecimalfszFormat.decimalcCst|||jdS)zHReturn a number in the given currency formatted for the locale.
        )r
)rr
)rr Úcurrencyrrrr"oszFormat.currencycCst|||jdS)zReturn a number formatted as percentage for the locale.

        >>> fmt = Format('en_US')
        >>> fmt.percent(0.34)
        u'34%'
        )r
)r	r
)rr rrrrÚpercenttszFormat.percentcCst||jdS)zLReturn a number formatted using scientific notation for the locale.
        )r
)r
r
)rr rrrÚ
scientific}szFormat.scientific)N)Nr)Nr)Nr)rrrF)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rrrrrr r!r"r#r$rrrrrs
	





	
	
	rc@seZdZdZddddddgZdd	„Zed
d„ƒZdd
„Zdd„Z	dd„Z
dd„Zdd„Zdd„Z
dd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Zd:d;„Zd<d=„Z d>d?„Z!d@dA„Z"dBS)CÚ	LazyProxyaClass 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|jddƒ}tj|d|ƒtj|d|ƒtj|d|ƒtj|d|ƒtj|ddƒtj|ddƒdS)	NÚenable_cacheTr*r+r,r.r-r/)ÚpopÚobjectÚ__setattr__)rÚfuncÚargsÚkwargsZis_cache_enabledrrrr¨szLazyProxy.__init__cCsp|jdkrjy|j|j|jŽ}Wn2tk
rP}ztj|d|ƒ‚WYdd}~XnX|js\|Stj|d|ƒ|jS)Nr/r-)r-r*r+r,ÚAttributeErrorr2r3r.)rÚvalueÚerrorrrrr8²s
zLazyProxy.valuecCs
||jkS)N)r8)rÚkeyrrrÚ__contains__ÀszLazyProxy.__contains__cCs
t|jƒS)N)Úboolr8)rrrrÚ__bool__ÃszLazyProxy.__bool__cCs
t|jƒS)N)Údirr8)rrrrÚ__dir__ÆszLazyProxy.__dir__cCs
t|jƒS)N)Úiterr8)rrrrÚ__iter__ÉszLazyProxy.__iter__cCs
t|jƒS)N)Úlenr8)rrrrÚ__len__ÌszLazyProxy.__len__cCs
t|jƒS)N)Ústrr8)rrrrÚ__str__ÏszLazyProxy.__str__cCs
|j|S)N)r8)rÚotherrrrÚ__add__ÒszLazyProxy.__add__cCs
||jS)N)r8)rrFrrrÚ__radd__ÕszLazyProxy.__radd__cCs
|j|S)N)r8)rrFrrrÚ__mod__ØszLazyProxy.__mod__cCs
||jS)N)r8)rrFrrrÚ__rmod__ÛszLazyProxy.__rmod__cCs
|j|S)N)r8)rrFrrrÚ__mul__ÞszLazyProxy.__mul__cCs
||jS)N)r8)rrFrrrÚ__rmul__ászLazyProxy.__rmul__cOs|j||ŽS)N)r8)rr5r6rrrÚ__call__äszLazyProxy.__call__cCs
|j|kS)N)r8)rrFrrrÚ__lt__çszLazyProxy.__lt__cCs
|j|kS)N)r8)rrFrrrÚ__le__êszLazyProxy.__le__cCs
|j|kS)N)r8)rrFrrrÚ__eq__íszLazyProxy.__eq__cCs
|j|kS)N)r8)rrFrrrÚ__ne__ðszLazyProxy.__ne__cCs
|j|kS)N)r8)rrFrrrÚ__gt__ószLazyProxy.__gt__cCs
|j|kS)N)r8)rrFrrrÚ__ge__öszLazyProxy.__ge__cCst|j|ƒdS)N)Údelattrr8)rÚnamerrrÚ__delattr__ùszLazyProxy.__delattr__cCs|jdk	r|j‚t|j|ƒS)N)r/Úgetattrr8)rrUrrrÚ__getattr__üs
zLazyProxy.__getattr__cCst|j||ƒdS)N)Úsetattrr8)rrUr8rrrr3szLazyProxy.__setattr__cCs|j|=dS)N)r8)rr:rrrÚ__delitem__szLazyProxy.__delitem__cCs
|j|S)N)r8)rr:rrrÚ__getitem__szLazyProxy.__getitem__cCs||j|<dS)N)r8)rr:r8rrrÚ__setitem__
szLazyProxy.__setitem__cCs t|jf|jžd|ji|j—ŽS)Nr0)r)r*r+r.r,)rrrrÚ__copy__
szLazyProxy.__copy__cCsDddlm}t||j|ƒf||j|ƒžd||j|ƒi||j|ƒ—ŽS)Nr)Údeepcopyr0)Úcopyr^r)r*r+r.r,)rÚmemor^rrrÚ__deepcopy__s
zLazyProxy.__deepcopy__N)#r%r&r'r(Ú	__slots__rÚpropertyr8r;r=r?rArCrErGrHrIrJrKrLrMrNrOrPrQrRrSrVrXr3rZr[r\r]rarrrrr)ƒs>"
r)csÖeZdZdZd)‡fdd„	Zdd„Zdd„Zdd	„ZeZd
d„Z	dd
„Z
dd„ZeZdZ
dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „ZeZd!d"„Zd#d$„Zd%d&„ZeZd'd(„ZejjZejjZ ‡Z!S)*ÚNullTranslationsNcsJi|_dd„|_tƒj|dttdt|ddƒgƒƒ|_|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|dkƒS)Né)Úint)ÚnrrrÚ<lambda>.sz+NullTranslations.__init__.<locals>.<lambda>)ÚfpNrU)Ú_catalogÚpluralÚsuperrÚlistÚfilterrWÚfilesÚDEFAULT_DOMAINÚdomainÚ_domains)rri)Ú	__class__rrr#s

zNullTranslations.__init__cCs|jj||ƒj|ƒS)zULike ``gettext()``, but look the message up in the specified
        domain.
        )rrÚgetÚgettext)rrqÚmessagerrrÚdgettext4szNullTranslations.dgettextcCs*ddl}|jdtdƒ|jj||ƒj|ƒS)zVLike ``lgettext()``, but look the message up in the specified
        domain.
        rNz1ldgettext() is deprecated, use dgettext() insteadé)ÚwarningsÚwarnÚDeprecationWarningrrrtÚlgettext)rrqrvryrrrÚ	ldgettext:szNullTranslations.ldgettextcCs|jj||ƒj|ƒS)zVLike ``ugettext()``, but look the message up in the specified
        domain.
        )rrrtÚugettext)rrqrvrrrÚ	udgettextCszNullTranslations.udgettextcCs|jj||ƒj|||ƒS)zVLike ``ngettext()``, but look the message up in the specified
        domain.
        )rrrtÚngettext)rrqÚsingularrkÚnumrrrÚ	dngettextKszNullTranslations.dngettextcCs.ddl}|jdtdƒ|jj||ƒj|||ƒS)zWLike ``lngettext()``, but look the message up in the specified
        domain.
        rNz3ldngettext() is deprecated, use dngettext() insteadrx)ryrzr{rrrtÚ	lngettext)rrqrrkr‚ryrrrÚ
ldngettextQszNullTranslations.ldngettextcCs|jj||ƒj|||ƒS)zVLike ``ungettext()`` but look the message up in the specified
        domain.
        )rrrtÚ	ungettext)rrqrrkr‚rrrÚ
udngettextZszNullTranslations.udngettextz%s%scCsF|j||f}tƒ}|jj||ƒ}||krB|jr>|jj||ƒS|S|S)a¯Look 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_ENCODINGr2rjrtÚ	_fallbackÚpgettext)rÚcontextrvÚctxt_msg_idÚmissingÚtmsgrrrrŠiszNullTranslations.pgettextcCs@ddl}|jdtdƒ|j||ƒ}t|ddƒp4tjƒ}|j|ƒS)zÄEquivalent to ``pgettext()``, but the translation is returned in the
        preferred system encoding, if no other encoding was explicitly set with
        ``bind_textdomain_codeset()``.
        rNz1lpgettext() is deprecated, use pgettext() insteadrxÚ_output_charset)ryrzr{rŠrWr
ÚgetpreferredencodingÚencode)rr‹rvryrŽÚencodingrrrÚ	lpgettextzszNullTranslations.lpgettextcCsh|j||f}y|j||j|ƒf}|Stk
rb|jrN|jj||||ƒS|dkrZ|S|SYnXdS)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.
        reN)rˆrjrkÚKeyErrorr‰Ú	npgettext)rr‹rrkr‚rŒrŽrrrr•†szNullTranslations.npgettextc	Cs˜ddl}|jdtdƒ|j||f}y2|j||j|ƒf}t|ddƒpLtjƒ}|j	|ƒSt
k
r’|jr~|jj||||ƒS|dkrŠ|S|SYnXdS)zÅEquivalent to ``npgettext()``, but the translation is returned in the
        preferred system encoding, if no other encoding was explicitly set with
        ``bind_textdomain_codeset()``.
        rNz3lnpgettext() is deprecated, use npgettext() insteadrxrre)
ryrzr{rˆrjrkrWr
rr‘r”r‰Ú
lnpgettext)	rr‹rrkr‚ryrŒrŽr’rrrr–s
zNullTranslations.lnpgettextcCsJ|j||f}tƒ}|jj||ƒ}||krF|jr>|jj||ƒ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.
        )rˆr2rjrtr‰Ú	upgettextrD)rr‹rvÚctxt_message_idrrŽrrrr—²szNullTranslations.upgettextcCsr|j||f}y|j||j|ƒf}WnFtk
rl|jrN|jj||||ƒS|dkr`t|ƒ}nt|ƒ}YnX|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.
        re)rˆrjrkr”r‰Ú
unpgettextrD)rr‹rrkr‚r˜rŽrrrr™Âs
zNullTranslations.unpgettextcCs|jj||ƒj||ƒS)zVLike `pgettext()`, but look the message up in the specified
        `domain`.
        )rrrtrŠ)rrqr‹rvrrrÚ	dpgettextÙszNullTranslations.dpgettextcCs|jj||ƒj||ƒS)zWLike `upgettext()`, but look the message up in the specified
        `domain`.
        )rrrtr—)rrqr‹rvrrrÚ
udpgettextßszNullTranslations.udpgettextcCs|jj||ƒj||ƒS)zÅEquivalent to ``dpgettext()``, but the translation is returned in the
        preferred system encoding, if no other encoding was explicitly set with
        ``bind_textdomain_codeset()``.
        )rrrtr“)rrqr‹rvrrrÚ
ldpgettextçszNullTranslations.ldpgettextcCs|jj||ƒj||||ƒS)zWLike ``npgettext``, but look the message up in the specified
        `domain`.
        )rrrtr•)rrqr‹rrkr‚rrrÚ
dnpgettextîszNullTranslations.dnpgettextcCs|jj||ƒj||||ƒS)zXLike ``unpgettext``, but look the message up in the specified
        `domain`.
        )rrrtr™)rrqr‹rrkr‚rrrÚudnpgettextõszNullTranslations.udnpgettextcCs|jj||ƒj||||ƒS)zÆEquivalent to ``dnpgettext()``, but the translation is returned in
        the preferred system encoding, if no other encoding was explicitly set
        with ``bind_textdomain_codeset()``.
        )rrrtr–)rrqr‹rrkr‚rrrÚldnpgettextþszNullTranslations.ldnpgettext)N)"r%r&r'rprrwr}rZ	dugettextrƒr…r‡Z
dungettextrˆrŠr“r•r–r—r™ršr›Z
dupgettextrœrržZdunpgettextrŸrurdr~r€r†Ú
__classcell__rr)rsrrds6		rdcs^eZdZdZdZd‡fdd„	ZejjZejj	Z
eddd„ƒZdd	„Z
ddd„Zd
d„Z‡ZS)ÚTranslationsz&An extended translation catalog class.ÚmessagesNcstƒj|d|p|j|_dS)zºInitialize the translations catalog.

        :param fp: the file-like object the translation should be read from
        :param domain: the message domain (default: 'messages')
        )riN)rlrrprq)rrirq)rsrrrszTranslations.__init__cCsn|dk	r*t|ttfƒs|g}dd„|Dƒ}|s4|j}tj|||ƒ}|sLtƒSt|dƒ}|||dSQRXdS)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)rD)Ú.0r
rrrú
<listcomp>(sz%Translations.load.<locals>.<listcomp>Úrb)rirq)Ú
isinstancermÚtuplerpruÚfindrdÚopen)ÚclsÚdirnameZlocalesrqÚfilenamerirrrÚloads
zTranslations.loadcCsdt|ƒj|jjdƒfS)Nz
<%s: "%s">zproject-id-version)Útyper%Ú_infort)rrrrÚ__repr__1s
zTranslations.__repr__TcCsbt|d|jƒ}|r&||jkr&|j|ƒS|jj|ƒ}|rJ|dk	rJ|j|ƒn|j|ƒ||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
        rqN)rWrprqÚmergerrrtÚadd_fallback)rÚtranslationsr±rqÚexistingrrrÚadd5s



zTranslations.addcCs6t|tjƒr2|jj|jƒt|tƒr2|jj|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
        )r¦ruÚGNUTranslationsrjÚupdater¡roÚextend)rr³rrrr±Os
	
zTranslations.merge)NN)NNN)T)r%r&r'r(rprrur¶r~r€r†Úclassmethodr­r°rµr±r rr)rsrr¡
s	
r¡)r(rur
Z
babel.corerZbabel.datesrrrrZ
babel.numbersrrr	r
rr)rdr¶r¡rrrrÚ<module>skl