관리-도구
편집 파일: plural.cpython-39.opt-1.pyc
a �����`BS������������������� ���@���s���d�Z�ddlZddlmZ�dZdZdd��ZG�dd ��d e�Zd d��Z dd ��Z dd��Zdd��Zdd��Z dd��ZG�dd��de�ZdZde�dej�fde�d�e��fde�d�fde�d�fd e�d!ej�fgZd"d#��ZdBd$d%�ZdCd&d'�Zd(d)��Zd*d+��Zd,d-��Zd.d/��ZG�d0d1��d1e�Zd2d3��Zd4d5��Zd6d7��Z G�d8d9��d9e�Z!G�d:d;��d;e!�Z"G�d<d=��d=e!�Z#G�d>d?��d?e#�Z$G�d@dA��dAe!�Z%dS�)Dz� babel.numbers ~~~~~~~~~~~~~ CLDR Plural support. See UTS #35. :copyright: (c) 2013-2021 by the Babel Team. :license: BSD, see LICENSE for more details. �����N)�decimal)ZzeroZoneZtwo�few�many�otherr���c�����������������C���s����t�|��}t|�}t|t�r6||kr(|}nt�t|��}t|tj�r�|���}|j}|dk�rf|j |d��nd}d� dd��|D���}|�d�}t|�}t|�} t|p�d�} t|p�d�}nd�}�} �} }|||| | |fS�)a#��Extract operands from a decimal, a float or an int, according to `CLDR rules`_. The result is a 6-tuple (n, i, v, w, f, t), where those symbols are as follows: ====== =============================================================== Symbol Value ------ --------------------------------------------------------------- n absolute value of the source number (integer and decimals). i integer digits of n. v number of visible fraction digits in n, with trailing zeros. w number of visible fraction digits in n, without trailing zeros. f visible fractional digits in n, with trailing zeros. t visible fractional digits in n, without trailing zeros. ====== =============================================================== .. _`CLDR rules`: https://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Operands :param source: A real number :type source: int|float|decimal.Decimal :return: A n-i-v-w-f-t tuple :rtype: tuple[decimal.Decimal, int, int, int, int, int] r���N����c�����������������s���s���|�]}t�|�V��qd�S��N��str)�.0�dr���r����0/usr/lib/python3.9/site-packages/babel/plural.py� <genexpr>A��������z#extract_operands.<locals>.<genexpr>�0) �abs�int� isinstance�floatr���ZDecimalr ���Zas_tupleZexponent�digits�join�rstrip�len)�source�n�iZ dec_tupleZexpZfraction_digitsZtrailingZno_trailing�v�w�f�tr���r���r ����extract_operands���s$���� r ���c�������������������@���sd���e�Zd�ZdZdZdd��Zdd��Zedd���Ze d d ���Z e dd��d d�Zdd��Zdd��Z dd��ZdS�)� PluralRuleaf��Represents a set of language pluralization rules. The constructor accepts a list of (tag, expr) tuples or a dict of `CLDR rules`_. The resulting object is callable and accepts one parameter with a positive or negative number (both integer and float) for the number that indicates the plural form for a string and returns the tag for the format: >>> rule = PluralRule({'one': 'n is 1'}) >>> rule(1) 'one' >>> rule(2) 'other' Currently the CLDR defines these tags: zero, one, two, few, many and other where other is an implicit default. Rules should be mutually exclusive; for a given numeric value, only one rule should apply (i.e. the condition should only be true for one of the plural rule elements. .. _`CLDR rules`: https://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules )�abstract�_funcc�����������������C���s����t�|t�r|���}t��}g�|�_tt|��D�]Z\}}|tvrHtd|���n||v�r\td|���|� |��t |�j}|r*|�j�||f��q*dS�)a$��Initialize the rule instance. :param rules: a list of ``(tag, expr)``) tuples with the rules conforming to UTS #35 or a dict with the tags as keys and expressions as values. :raise RuleError: if the expression is malformed zunknown tag %rztag %r defined twiceN) r����dict�items�setr"����sorted�list�_plural_tags� ValueError�add�_Parser�ast�append)�self�rules�found�key�exprr-���r���r���r ����__init__c���s���� zPluralRule.__init__c��������������������s,���|�j���dt|��jd���fdd�tD���f�S�)Nz<%s %r>z, c��������������������s$���g�|�]}|��v�rd�|��|�f��qS�)z%s: %sr����r����tag�r0���r���r ���� <listcomp>}���s����z'PluralRule.__repr__.<locals>.<listcomp>)r0����type�__name__r���r)����r/���r���r7���r ����__repr__y���s �����zPluralRule.__repr__c�����������������C���s���t�||��r|S�|�|�S�)a ��Create a `PluralRule` instance for the given rules. If the rules are a `PluralRule` object, that object is returned. :param rules: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed )r���)�clsr0���r���r���r ����parse����s���� zPluralRule.parsec��������������������s ���t���j��t��fdd�|�jD���S�)z�The `PluralRule` as a dict of unicode plural rules. >>> rule = PluralRule({'one': 'n is 1'}) >>> rule.rules {'one': 'n is 1'} c��������������������s���g�|�]\}}|��|�f�qS�r���r���)r���r6���r-�����_compiler���r ���r8�������r���z$PluralRule.rules.<locals>.<listcomp>)�_UnicodeCompiler�compiler$���r"���r;���r���r?���r ���r0�������s����zPluralRule.rulesc�����������������C���s���t�dd��|�jD���S�)Nc�����������������S���s���g�|�]}|d���qS�)r���r���)r���r���r���r���r ���r8�������r���z'PluralRule.<lambda>.<locals>.<listcomp>)� frozensetr"�����xr���r���r ����<lambda>����r���zPluralRule.<lambda>z� A set of explicitly defined tags in this rule. The implicit default ``'other'`` rules is not part of this set unless there is an explicit rule for it.)�docc�����������������C���s���|�j�S�r����r"���r;���r���r���r ����__getstate__����s����zPluralRule.__getstate__c�����������������C���s ���||�_�d�S�r���rH���)r/���r"���r���r���r ����__setstate__����s����zPluralRule.__setstate__c�����������������C���s���t�|�d�st|��|�_|��|�S�)Nr#���)�hasattr� to_pythonr#���)r/���r���r���r���r ����__call__����s���� zPluralRule.__call__N)r:���� __module__�__qualname__�__doc__� __slots__r4���r<����classmethodr>����propertyr0����tagsrI���rJ���rM���r���r���r���r ���r!���L���s��� r!���c�����������������C���sR���t���j}dg}t�|��jD�]\}}|�d||�|f���q|�dt���d�|�S�)a���Convert a list/dict of rules or a `PluralRule` object into a JavaScript function. This function depends on no external library: >>> to_javascript({'one': 'n is 1'}) "(function(n) { return (n == 1) ? 'one' : 'other'; })" Implementation detail: The function generated will probably evaluate expressions involved into range operations multiple times. This has the advantage that external helper functions are not required and is not a big performance hit for these simple calculations. :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed z(function(n) { return z %s ? %r : z%r; })r���)�_JavaScriptCompilerrB���r!���r>���r"���r.���� _fallback_tagr���)�ruleZto_js�resultr6���r-���r���r���r ���� to_javascript����s����rY���c�����������������C���s����t�tttd�}t��j}ddg}t�|��jD�]"\}}|� d||�t |�f���q*|� dt���td�|�dd�}t ||��|d �S�) a<��Convert a list/dict of rules or a `PluralRule` object into a regular Python function. This is useful in situations where you need a real function and don't are about the actual rule object: >>> func = to_python({'one': 'n is 1', 'few': 'n in 2..4'}) >>> func(1) 'one' >>> func(3) 'few' >>> func = to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'}) >>> func(11) 'one' >>> func(15) 'few' :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed )�INZWITHINZMODr ���zdef evaluate(n):z' n, i, v, w, f, t = extract_operands(n)z if (%s): return %rz return %r� z<rule>�execZevaluate)� in_range_list�within_range_list�cldr_modulor ����_PythonCompilerrB���r!���r>���r"���r.���r ���rV���r����eval)rW���� namespaceZto_python_funcrX���r6���r-����coder���r���r ���rL�������s������ rL���c��������������������s����t��|��}�|�jthB���t��j}��fdd�tD��j}dt����g}|�j D�]"\}}|� d||�||�f���qF|� d|t����d�|�S�)a~��The plural rule as gettext expression. The gettext expression is technically limited to integers and returns indices rather than tags. >>> to_gettext({'one': 'n is 1', 'two': 'n is 2'}) 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2)' :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed c��������������������s���g�|�]}|��v�r|�qS�r���r���r5����Z used_tagsr���r ���r8�������r���zto_gettext.<locals>.<listcomp>znplurals=%d; plural=(z %s ? %d : z%d)r���)r!���r>���rT���rV����_GettextCompilerrB���r)����indexr���r"���r.���r���)rW���r@���Z _get_indexrX���r6���r-���r���rd���r ���� to_gettext����s���� rg���c�����������������C���s���|�t�|��kot|�|�S�)a���Integer range list test. This is the callback for the "in" operator of the UTS #35 pluralization rule language: >>> in_range_list(1, [(1, 3)]) True >>> in_range_list(3, [(1, 3)]) True >>> in_range_list(3, [(1, 3), (5, 8)]) True >>> in_range_list(1.2, [(1, 4)]) False >>> in_range_list(10, [(1, 4)]) False >>> in_range_list(10, [(1, 4), (6, 8)]) False )r���r^�����num� range_listr���r���r ���r]�������s����r]���c��������������������s���t���fdd�|D���S�)a���Float range test. This is the callback for the "within" operator of the UTS #35 pluralization rule language: >>> within_range_list(1, [(1, 3)]) True >>> within_range_list(1.0, [(1, 3)]) True >>> within_range_list(1.2, [(1, 4)]) True >>> within_range_list(8.8, [(1, 4), (7, 15)]) True >>> within_range_list(10, [(1, 4)]) False >>> within_range_list(10.5, [(1, 4), (20, 30)]) False c�����������������3���s"���|�]\}}��|ko��|kV��qd�S�r���r���)r���Zmin_Zmax_�ri���r���r ���r���$��r���z$within_range_list.<locals>.<genexpr>)�anyrh���r���rk���r ���r^�����s����r^���c�����������������C���s@���d}|�dk�r|�d9�}�d}|dk�r(|d9�}|�|�}|r<|d9�}|S�)z�Javaish modulo. This modulo operator returns the value with the sign of the dividend rather than the divisor like Python does: >>> cldr_modulo(-3, 5) -3 >>> cldr_modulo(-3, -5) -3 >>> cldr_modulo(3, 5) 3 r����������r���)�a�b�reverse�rvr���r���r ���r_���'��s����r_���c�������������������@���s���e�Zd�ZdZdS�)� RuleErrorzRaised if a rule is malformed.N)r:���rN���rO���rP���r���r���r���r ���rs���>��s���rs���Znivwftz\s+�wordz)\b(and|or|is|(?:with)?in|not|mod|[{0}])\b�valuez\d+�symbolz%|,|!=|=�ellipsisz\.{2,3}|\u2026c�����������������C���s����|���d�d�}�g�}d}t|��}||k�r|tD�]>\}}|�|�|�}|d�ur*|���}|rd|�||���f���qq*td|�|����q|d�d�d��S�)N�@r���z5malformed CLDR pluralization rule. Got unexpected %rrm���)�splitr����_RULES�match�endr.����grouprs���)�srX����posr|����tokrW���r{���r���r���r ���� tokenize_ruleM��s �����r����c�����������������C���s,���|�o*|�d�d�|ko*|d�u�p*|�d�d�|kS�)Nrm���r���rn���r�����tokens�type_ru���r���r���r ����test_next_token`��s�����r����c�����������������C���s���t�|�||�r|����S�d�S�r���)r�����popr����r���r���r ���� skip_tokene��s����r����c�����������������C���s ���d|�ffS�)Nru���r���)ru���r���r���r ���� value_nodej��s����r����c�����������������C���s���|�dfS�)Nr���r���)�namer���r���r ���� ident_noden��s����r����c�����������������C���s���d|�fS�)Nrj���r���)rj���r���r���r ����range_list_noder��s����r����c�����������������C���s ���d|�ffS�)N�notr���)rr���r���r���r ����negatev��s����r����c�������������������@���sb���e�Zd�ZdZdd��Zddd�Zdd��Zd d ��Zdd��Zd d��Z dd��Z dd��Zdd��Zdd��Z dS�)r,���u���Internal parser. This class can translate a single rule into an abstract tree of tuples. It implements the following grammar:: condition = and_condition ('or' and_condition)* ('@integer' samples)? ('@decimal' samples)? and_condition = relation ('and' relation)* relation = is_relation | in_relation | within_relation is_relation = expr 'is' ('not')? value in_relation = expr (('not')? 'in' | '=' | '!=') range_list within_relation = expr ('not')? 'within' range_list expr = operand (('mod' | '%') value)? operand = 'n' | 'i' | 'f' | 't' | 'v' | 'w' range_list = (range | value) (',' range_list)* value = digit+ digit = 0|1|2|3|4|5|6|7|8|9 range = value'..'value samples = sampleRange (',' sampleRange)* (',' ('…'|'...'))? sampleRange = decimalValue '~' decimalValue decimalValue = value ('.' value)? - Whitespace can occur between or around any of the above tokens. - Rules should be mutually exclusive; for a given numeric value, only one rule should apply (i.e. the condition should only be true for one of the plural rule elements). - The in and within relations can take comma-separated lists, such as: 'n in 3,5,7..15'. - Samples are ignored. The translator parses the expression on instanciation into an attribute called `ast`. c�����������������C���sD���t�|�|�_|�jsd�|�_d�S�|����|�_|�jr@td|�jd�d����d�S�)NzExpected end of rule, got %rrm���rn���)r����r����r-���� conditionrs���)r/����stringr���r���r ���r4������s���� �z_Parser.__init__Nc�����������������C���sf���t�|�j||�}|d�ur|S�|d�u�r6t|d�u�r0|p2|�}|�jsHtd|���td||�jd�d�f���d�S�)Nz#expected %s but end of rule reachedzexpected %s but got %rrm���rn���)r����r�����reprrs���)r/���r����ru���Zterm�tokenr���r���r ����expect���s����z_Parser.expectc�����������������C���s,���|�����}t|�jdd�r(d||�����ff}q|S�)Nrt����or)� and_conditionr����r�����r/����opr���r���r ���r�������s����z_Parser.conditionc�����������������C���s,���|�����}t|�jdd�r(d||�����ff}q|S�)Nrt����and)�relationr����r����r����r���r���r ���r�������s����z_Parser.and_conditionc�����������������C���s����|�����}t|�jdd�r8t|�jdd�r(dp*d||����ffS�t|�jdd�}d}t|�jdd�r^d}n$t|�jdd�s�|rxtd��|��|�S�d|||����ff}|r�t|�S�|S�) Nrt����isr����Zisnot�inZwithinz#Cannot negate operator based rules.r����)r3���r����r����ru���rs����newfangled_relationrj���r����)r/����left�negated�methodrr���r���r���r ���r�������s���� � z_Parser.relationc�����������������C���sR���t�|�jdd�rd}nt�|�jdd�r(d}ntd��dd||����ff}|rNt|�S�|S�) Nrv����=Fz!=Tz'Expected "=" or "!=" or legacy relationr����r����)r����r����rs���rj���r����)r/���r����r����rr���r���r���r ���r�������s����z_Parser.newfangled_relationc�����������������C���s,���|�����}t|�jd�r ||�����fS�||fS�d�S�)Nrw���)ru���r����r����)r/���r����r���r���r ����range_or_value���s����z_Parser.range_or_valuec�����������������C���s0���|�����g}t|�jdd�r(|�|�������q t|�S�)Nrv����,)r����r����r����r.���r����)r/���rj���r���r���r ���rj������s���� z_Parser.range_listc�����������������C���s|���t�|�jd�}|d�u�s |d�tvr(td��|d�}t�|�jdd�rRd|df|����ffS�t�|�jdd�rtd|df|����ffS�t|�S�)Nrt���rn���zExpected identifier variable�modr���rv����%)r����r�����_VARSrs���ru���r����)r/���rt���r����r���r���r ���r3������s����z_Parser.exprc�����������������C���s���t�t|��d�d���S�)Nru���rn���)r����r���r����r;���r���r���r ���ru������s����z _Parser.value)NN)r:���rN���rO���rP���r4���r����r����r����r����r����r����rj���r3���ru���r���r���r���r ���r,���z��s���! r,���c��������������������s�����fdd�S�)�%Compiler factory for the `_Compiler`.c��������������������s�����|���|�|���|�f�S�r����rB���)r/����l�r�Ztmplr���r ���rF������r���z"_binary_compiler.<locals>.<lambda>r���r����r���r����r ����_binary_compiler���s����r����c��������������������s�����fdd�S�)r����c��������������������s�����|���|��S�r���r����)r/���rE���r����r���r ���rF������r���z!_unary_compiler.<locals>.<lambda>r���r����r���r����r ����_unary_compiler���s����r����c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF������r���rF���c�������������������@���s����e�Zd�ZdZdd��Zdd��Zdd��Zdd��Zdd��Zd d��Z d d��Z dd��Zed�Z ed �Zed�Zed�Zed�Zed�Zdd��ZdS�)� _CompilerzZThe compilers are able to transform the expressions into multiple output formats. c�����������������C���s���|\}}t�|�d|��|��S�)NZcompile_)�getattr)r/����argr�����argsr���r���r ���rB�����s����z_Compiler.compilec�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF�����r���z_Compiler.<lambda>c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF�����r���c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF��� ��r���c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF�����r���c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF�����r���c�����������������C���s���dS�)Nr���r���rD���r���r���r ���rF�����r���c�����������������C���s���t�|�S�r���r ���)rE���r���r���r���r ���rF�����r���z (%s && %s)z (%s || %s)z(!%s)z (%s %% %s)� (%s == %s)z (%s != %s)c�����������������C���s ���t����d�S�r���)�NotImplementedError)r/���r����r3���rj���r���r���r ����compile_relation��s����z_Compiler.compile_relationN)r:���rN���rO���rP���rB���� compile_n� compile_i� compile_v� compile_w� compile_f� compile_tZ compile_valuer�����compile_and� compile_orr�����compile_not�compile_mod� compile_is� compile_isnotr����r���r���r���r ���r������s ���r����c�������������������@���s8���e�Zd�ZdZed�Zed�Zed�Zed�Z dd��Z dS�) r`���z!Compiles an expression to Python.z(%s and %s)z (%s or %s)z(not %s)zMOD(%s, %s)c��������������������s8���dd����fdd�|d�D����}d|������|�|f�S�)Nz[%s]r����c��������������������s ���g�|�]}d�t�t��j|����qS�)z(%s, %s))�tuple�maprB���)r���Zrange_r;���r���r ���r8���'��s����z4_PythonCompiler.compile_relation.<locals>.<listcomp>rn���z %s(%s, %s))r����upperrB���)r/���r����r3���rj���Zcompile_range_listr���r;���r ���r����%��s���� ���z _PythonCompiler.compile_relationN)r:���rN���rO���rP���r����r����r����r����r����r����r����r���r���r���r ���r`�����s���r`���c�������������������@���s.���e�Zd�ZdZejZeZeZ eZ eZdd��ZdS�)re���z)Compile into a gettext plural expression.c�������������� ���C���s~���g�}|���|�}|d�D�]X}|d�|d�krH|�d||���|d��f���qt|�j�|�\}}|�d||||f���qdd�|��S�)Nrn���r���r����z(%s >= %s && %s <= %s)z(%s)z || )rB���r.���r����r���)r/���r����r3���rj���rr����item�min�maxr���r���r ���r����6��s ���� � � z!_GettextCompiler.compile_relationN) r:���rN���rO���rP���r����r����r�����compile_zeror����r����r����r����r����r���r���r���r ���re���-��s���re���c�������������������@���s0���e�Zd�ZdZdd��ZeZeZeZeZ dd��Z dS�)rU���z/Compiles the expression to plain of JavaScript.c�����������������C���s���dS�)NzparseInt(n, 10)r���rD���r���r���r ���rF���O��r���z_JavaScriptCompiler.<lambda>c�����������������C���s4���t��|�|||�}|dkr0|��|�}d|||f�}|S�)Nr����z(parseInt(%s, 10) == %s && %s))re���r����rB���)r/���r����r3���rj���rc���r���r���r ���r����U��s����� z$_JavaScriptCompiler.compile_relationN)r:���rN���rO���rP���r����r����r����r����r����r����r����r���r���r���r ���rU���J��s���rU���c�������������������@���sJ���e�Zd�ZdZed�Zed�Zed�Zed�Zed�Z dd��Z d d d�ZdS�)rA���z+Returns a unicode pluralization rule again.z%s is %sz%s is not %sz %s and %sz%s or %sz %s mod %sc�����������������C���s���|�j�|d�ddi�S�)Nrn���r����T)r����)r/���r����r���r���r ���r����k��s����z_UnicodeCompiler.compile_notFc�����������������C���sv���g�}|d�D�]D}|d�|d�kr6|��|��|d����q|��dtt|�j|�����qd|��|�|rddpfd|d�|�f�S�)Nrn���r���z%s..%sz %s%s %s %sz notr���r����)r.���rB���r����r����r���)r/���r����r3���rj���r����Zrangesr����r���r���r ���r����n��s���� �z!_UnicodeCompiler.compile_relationN)F)r:���rN���rO���rP���r����r����r����r����r����r����r����r����r���r���r���r ���rA���^��s���rA���)N)N)&rP����reZ babel._compatr���r)���rV���r ����objectr!���rY���rL���rg���r]���r^���r_���� Exceptionrs���r����rB����UNICODE�formatrz���r����r����r����r����r����r����r����r,���r����r����r����r����r`���re���rU���rA���r���r���r���r ����<module>���sL��� 8]( �� {