dbfdg 3 4\0XW@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 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 ZdddZddZ 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' NcCstj||_||_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.6/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' )rr)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)rZdeltarrrr rrr timedeltaQs zFormat.timedeltacCst||jdS)zReturn an integer number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.number(1099) u'1,099' )r)rr)rnumberrrrr"_sz Format.numbercCst|||jdS)zReturn a decimal number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.decimal(1.2345) u'1.234' )r)rr)rr"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 r)rr"rrrrpercentvszFormat.percentcCst||jdS)zLReturn a number formatted using scientific notation for the locale. )r)r r)rr"rrr scientificszFormat.scientific)N)Nr)Nr)Nr)rrrF)N)N)__name__ __module__ __qualname____doc__rrrrr!r"r#r$r%r&rrrrrs     rc@seZdZdZdddddgZddZed d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCS)D 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|jdd}tj|d|tj|d|tj|d|tj|d|tj|dddS)N enable_cacheTr,r-r.r0r/)popobject __setattr__)rfuncargskwargsZis_cache_enabledrrrrs  zLazyProxy.__init__cCs8|jdkr2|j|j|j}|js$|Stj|d||jS)Nr/)r/r,r-r.r0r3r4)rvaluerrrr8s  zLazyProxy.valuecCs ||jkS)N)r8)rkeyrrr __contains__szLazyProxy.__contains__cCs t|jS)N)boolr8)rrrr __nonzero__szLazyProxy.__nonzero__cCs t|jS)N)dirr8)rrrr__dir__szLazyProxy.__dir__cCs t|jS)N)iterr8)rrrr__iter__szLazyProxy.__iter__cCs t|jS)N)lenr8)rrrr__len__szLazyProxy.__len__cCs t|jS)N)strr8)rrrr__str__szLazyProxy.__str__cCs t|jS)N)Zunicoder8)rrrr __unicode__szLazyProxy.__unicode__cCs |j|S)N)r8)rotherrrr__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)rr6r7rrr__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)rnamerrr __delattr__szLazyProxy.__delattr__cCs t|j|S)N)getattrr8)rrUrrr __getattr__szLazyProxy.__getattr__cCst|j||dS)N)setattrr8)rrUr8rrrr4szLazyProxy.__setattr__cCs |j|=dS)N)r8)rr9rrr __delitem__szLazyProxy.__delitem__cCs |j|S)N)r8)rr9rrr __getitem__szLazyProxy.__getitem__cCs||j|<dS)N)r8)rr9r8rrr __setitem__szLazyProxy.__setitem__cCs t|jf|jd|ji|jS)Nr1)r+r,r-r0r.)rrrr__copy__ szLazyProxy.__copy__cCsDddlm}t||j|f||j|d||j|i||j|S)Nr)deepcopyr1)copyr^r+r,r-r0r.)rmemor^rrr __deepcopy__s   zLazyProxy.__deepcopy__N)$r'r(r)r* __slots__rpropertyr8r:r<r>r@rBrDrErGrHrIrJrKrLrMrNrOrPrQrRrSrVrXr4rZr[r\r]rarrrrr+s@" r+cseZdZdZd)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)nrrr+sz+NullTranslations.__init__..)fpNrU) _catalogpluralsuperrdrlistfilterrWfilesDEFAULT_DOMAINdomain_domains)rri) __class__rrr s  zNullTranslations.__init__cCs|jj||j|S)zULike ``gettext()``, but look the message up in the specified domain. )rrgetgettext)rrqmessagerrrdgettext1szNullTranslations.dgettextcCs|jj||j|S)zVLike ``lgettext()``, but look the message up in the specified domain. )rrrtZlgettext)rrqrvrrr ldgettext7szNullTranslations.ldgettextcCs|jj||j|S)zVLike ``ugettext()``, but look the message up in the specified domain. )rrrtugettext)rrqrvrrr udgettext=szNullTranslations.udgettextcCs|jj||j|||S)zVLike ``ngettext()``, but look the message up in the specified domain. )rrrtngettext)rrqsingularrknumrrr dngettextEszNullTranslations.dngettextcCs|jj||j|||S)zWLike ``lngettext()``, but look the message up in the specified domain. )rrrtZ lngettext)rrqr|rkr}rrr ldngettextKszNullTranslations.ldngettextcCs|jj||j|||S)zVLike ``ungettext()`` but look the message up in the specified domain. )rrrt ungettext)rrqr|rkr}rrr udngettextQszNullTranslations.udngettextz%s%scCsj|j||f}t}|jj||}||krB|jr>|jj||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_ENCODINGr3rjrt _fallbackpgettext_output_charsetr_charset)rcontextrv ctxt_msg_idmissingtmsgrrrr`s  zNullTranslations.pgettextcCsb|j||f}t}|jj||}||krB|jr>|jj||S|S|jrT|j|jS|jtj 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()``. ) rr3rjrtr lpgettextrencodergetpreferredencoding)rrrvrrrrrrrvs zNullTranslations.lpgettextc Cs|j||f}y<|j||j|f}|jr6t||jS|jrHt||jS|Stk r|jrr|jj||||S|dkr~|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) rrjrkrrrKeyErrorr npgettext)rrr|rkr}rrrrrrs   zNullTranslations.npgettextc Cs|j||f}y4|j||j|f}|jr6|j|jS|jtjStk r~|jrj|jj ||||S|dkrv|S|SYnXdS)zEquivalent to ``npgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. reN) rrjrkrrrrrr lnpgettext)rrr|rkr}rrrrrrs 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. )rr3rjrtr upgettextr )rrrvctxt_message_idrrrrrrszNullTranslations.upgettextc Csr|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)rrjrkrr unpgettextr )rrr|rkr}rrrrrrs  zNullTranslations.unpgettextcCs|jj||j||S)zVLike `pgettext()`, but look the message up in the specified `domain`. )rrrtr)rrqrrvrrr dpgettextszNullTranslations.dpgettextcCs|jj||j||S)zWLike `upgettext()`, but look the message up in the specified `domain`. )rrrtr)rrqrrvrrr udpgettextszNullTranslations.udpgettextcCs|jj||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()``. )rrrtr)rrqrrvrrr ldpgettextszNullTranslations.ldpgettextcCs|jj||j||||S)zWLike ``npgettext``, but look the message up in the specified `domain`. )rrrtr)rrqrr|rkr}rrr dnpgettextszNullTranslations.dnpgettextcCs|jj||j||||S)zXLike ``unpgettext``, but look the message up in the specified `domain`. )rrrtr)rrqrr|rkr}rrr udnpgettextszNullTranslations.udnpgettextcCs|jj||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()``. )rrrtr)rrqrr|rkr}rrr ldnpgettextszNullTranslations.ldnpgettext)N)#r'r(r)rprrwrxrzZ dugettextr~rrZ dungettextrrrrrrrrrZ dupgettextrrrZ dunpgettextrr rurdryr{r __classcell__rr)rsrrds8rdcsbeZdZdZdZdfdd Zes2ejjZ ejj Z e dddZ dd Zdd d Zd dZZS) Translationsz&An extended translation catalog class.ZmessagesNcs"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') )riN)rlrrrprq)rrirq)rsrrrszTranslations.__init__c Csn|dk r*t|ttfs|g}dd|D}|s4|j}tj|||}|sLtSt|d}|||dSQRXdS)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)rC).0rrrr ,sz%Translations.load..rb)rirq) isinstancermtuplerprufindrdopen)clsdirnameZlocalesrqfilenamerirrrloads  zTranslations.loadcCsdt|j|jjdfS)Nz <%s: "%s">zproject-id-version)typer'Z_infort)rrrr__repr__5s 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)rWrprqmergerrrtZ add_fallback)r translationsrrqZexistingrrradd9s       zTranslations.addcCs6t|tjr2|jj|jt|tr2|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 )rruGNUTranslationsrjupdaterroextend)rrrrrrSs  zTranslations.merge)NN)NNN)T)r'r(r)r*rprr rurryr{r classmethodrrrrrrr)rsrr s   r)r*rurZ babel.corerZ babel.datesrrrrZ babel.numbersrrr r r Z babel._compatr r rr3rr+rdrrrrrr s kr