a
    ÆZEg$5  ã                   @  s  d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 erLd dl
mZ G dd„ deƒZd	efd
ddddœdd„Zefd
dddœdd„Zd	defddœdd
ddddd
dœdd„Zefd
d
dddœdd„Zdddd	defddœddddddddddœ	d d!„ZdS )"é    )ÚannotationsN)ÚTYPE_CHECKING)ÚLocale)Ú
LC_NUMERICÚformat_decimal)ÚLiteralc                   @  s   e Zd Zddddœdd„ZdS )ÚUnknownUnitErrorÚstrr   ÚNone)ÚunitÚlocaleÚreturnc                 C  s   t  | |› d|› �¡ d S )Nz is not a known unit in )Ú
ValueErrorÚ__init__)Úselfr   r   © r   úD/var/www/sistema_ama/venv/lib/python3.9/site-packages/babel/units.pyr      s    zUnknownUnitError.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   Úlongr	   z"Literal['short', 'long', 'narrow']zLocale | str | Nonez
str | None)Úmeasurement_unitÚlengthr   r   c                 C  s:   t  |¡}t| |d�}|s&t| |d�‚|j |i ¡ |¡S )aè  
    Get the display name for a measurement unit in the given locale.

    >>> get_unit_name("radian", locale="en")
    'radians'

    Unknown units will raise exceptions:

    >>> get_unit_name("battery", locale="fi")
    Traceback (most recent call last):
        ...
    UnknownUnitError: battery/long is not a known unit/length in fi

    :param measurement_unit: the code of a measurement unit.
                             Known units can be found in the CLDR Unit Validity XML file:
                             https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml

    :param length: "short", "long" or "narrow"
    :param locale: the `Locale` object or locale identifier
    :return: The unit display name, or None.
    ©r   ©r   r   )r   ÚparseÚ_find_unit_patternr   Zunit_display_namesÚget)r   r   r   r   r   r   r   Úget_unit_name   s
    
r   )Úunit_idr   r   c                 C  sH   t  |¡}|jd }| |v r | S t|td�D ]}| | ¡r,|  S q,dS )aÇ  
    Expand a unit into a qualified form.

    Known units can be found in the CLDR Unit Validity XML file:
    https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml

    >>> _find_unit_pattern("radian", locale="en")
    'angle-radian'

    Unknown values will return None.

    >>> _find_unit_pattern("horse", locale="en")

    :param unit_id: the code of a measurement unit.
    :return: A key to the `unit_patterns` mapping, or None.
    Úunit_patterns)ÚkeyN)r   r   Ú_dataÚsortedÚlenÚendswith)r   r   r    Zunit_patternr   r   r   r   3   s    



r   Zlatn©Únumbering_systemzstr | float | decimal.DecimalzLiteral['default'] | str)Úvaluer   r   Úformatr   r'   r   c                C  s    t  |¡}t||d�}|s&t||d�‚|jd |  |i ¡}t| tƒrP| }d}	nt| |||d�}| 	| ¡}	|	|v r€||	  
|¡S t|||d�}
|› d|
pš|› �S )uw  Format a value of a given unit.

    Values are formatted according to the locale's usual pluralization rules
    and number formats.

    >>> format_unit(12, 'length-meter', locale='ro_RO')
    u'12 metri'
    >>> format_unit(15.5, 'length-mile', locale='fi_FI')
    u'15,5 mailia'
    >>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb')
    u'1\xa0200 millimeter kvikks\xf8lv'
    >>> format_unit(270, 'ton', locale='en')
    u'270 tons'
    >>> format_unit(1234.5, 'kilogram', locale='ar_EG', numbering_system='default')
    u'1Ù¬234Ù«5 ÙƒÙŠÙ„ÙˆØºØ±Ø§Ù…'

    Number formats may be overridden with the ``format`` parameter.

    >>> import decimal
    >>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr')
    u'-42,8\u202f\xb0C'

    The locale's usual pluralization rules are respected.

    >>> format_unit(1, 'length-meter', locale='ro_RO')
    u'1 metru'
    >>> format_unit(0, 'length-mile', locale='cy')
    u'0 mi'
    >>> format_unit(1, 'length-mile', locale='cy')
    u'1 filltir'
    >>> format_unit(3, 'length-mile', locale='cy')
    u'3 milltir'

    >>> format_unit(15, 'length-horse', locale='fi')
    Traceback (most recent call last):
        ...
    UnknownUnitError: length-horse is not a known unit in fi

    .. versionadded:: 2.2.0

    :param value: the value to format. If this is a string, no number formatting will be attempted.
    :param measurement_unit: the code of a measurement unit.
                             Known units can be found in the CLDR Unit Validity XML file:
                             https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
    :param length: "short", "long" or "narrow"
    :param format: An optional format, as accepted by `format_decimal`.
    :param locale: the `Locale` object or locale identifier
    :param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
                             The special value "default" will use the default numbering system of the locale.
    :raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
    r   r   r    Zoner&   )r   r   ú )r   r   r   r   r"   r   Ú
isinstancer	   r   Úplural_formr)   r   )r(   r   r   r)   r   r'   Zq_unitr    Zformatted_valuer,   Zfallback_namer   r   r   Úformat_unitN   s    <


r-   )Únumerator_unitÚdenominator_unitr   r   c                 C  sd   t  |¡}t| |d�}t||d�}|r*|s.dS | dd¡d }| dd¡d }t|› d|› �|d�S )aæ  
    Find a predefined compound unit pattern.

    Used internally by format_compound_unit.

    >>> _find_compound_unit("kilometer", "hour", locale="en")
    'speed-kilometer-per-hour'

    >>> _find_compound_unit("mile", "gallon", locale="en")
    'consumption-mile-per-gallon'

    If no predefined compound pattern can be found, `None` is returned.

    >>> _find_compound_unit("gallon", "mile", locale="en")

    >>> _find_compound_unit("horse", "purple", locale="en")

    :param numerator_unit: The numerator unit's identifier
    :param denominator_unit: The denominator unit's identifier
    :param locale: the `Locale` object or locale identifier
    :return: A key to the `unit_patterns` mapping, or None.
    :rtype: str|None
    r   Nú-é   éÿÿÿÿz-per-)r   r   r   Úsplit)r.   r/   r   Zresolved_numerator_unitZresolved_denominator_unitZbare_numerator_unitZbare_denominator_unitr   r   r   Ú_find_compound_unit¡   s    
r4   r1   )	Únumerator_valuer.   Údenominator_valuer/   r   r)   r   r'   r   c                C  s0  t  |¡}|r@|r@|dkr@t|||d�}|r@t| |||||d�S t| tƒrP| }	n*|rjt| |||||d�}	nt| |||d�}	t|tƒrŠ|}
nx|rò|dkrÔt||d�}|jd  	|i ¡ 	|i ¡ 	d¡}|rÐ| 
|	¡S d}t||pÞd||||d� ¡ }
nt||||d�}
|jd	  	di ¡ 	|i ¡ 	d
d¡}| 
|	|
¡S )u­	  
    Format a compound number value, i.e. "kilometers per hour" or similar.

    Both unit specifiers are optional to allow for formatting of arbitrary values still according
    to the locale's general "per" formatting specifier.

    >>> format_compound_unit(7, denominator_value=11, length="short", locale="pt")
    '7/11'

    >>> format_compound_unit(150, "kilometer", denominator_unit="hour", locale="sv")
    '150 kilometer per timme'

    >>> format_compound_unit(150, "kilowatt", denominator_unit="year", locale="fi")
    '150 kilowattia / vuosi'

    >>> format_compound_unit(32.5, "ton", 15, denominator_unit="hour", locale="en")
    '32.5 tons per 15 hours'

    >>> format_compound_unit(1234.5, "ton", 15, denominator_unit="hour", locale="ar_EG", numbering_system="arab")
    '1Ù¬234Ù«5 Ø·Ù† Ù„ÙƒÙ„ 15 Ø³Ø§Ø¹Ø©'

    >>> format_compound_unit(160, denominator_unit="square-meter", locale="fr")
    '160 par m\xe8tre carr\xe9'

    >>> format_compound_unit(4, "meter", "ratakisko", length="short", locale="fi")
    '4 m/ratakisko'

    >>> format_compound_unit(35, "minute", denominator_unit="nautical-mile", locale="sv")
    '35 minuter per nautisk mil'

    >>> from babel.numbers import format_currency
    >>> format_compound_unit(format_currency(35, "JPY", locale="de"), denominator_unit="liter", locale="de")
    '35\xa0\xa5 pro Liter'

    See https://www.unicode.org/reports/tr35/tr35-general.html#perUnitPatterns

    :param numerator_value: The numerator value. This may be a string,
                            in which case it is considered preformatted and the unit is ignored.
    :param numerator_unit: The numerator unit. See `format_unit`.
    :param denominator_value: The denominator value. This may be a string,
                              in which case it is considered preformatted and the unit is ignored.
    :param denominator_unit: The denominator unit. See `format_unit`.
    :param length: The formatting length. "short", "long" or "narrow"
    :param format: An optional format, as accepted by `format_decimal`.
    :param locale: the `Locale` object or locale identifier
    :param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
                             The special value "default" will use the default numbering system of the locale.
    :return: A formatted compound value.
    :raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
    r1   r   )r   r)   r   r'   )r)   r   r'   r    ZperÚ )r   r   r)   r   r'   Zcompound_unit_patternsZcompoundz{0}/{1})r   r   r4   r-   r+   r	   r   r   r"   r   r)   Ústrip)r5   r.   r6   r/   r   r)   r   r'   Zcompound_unitZformatted_numeratorZformatted_denominatorZper_patternr   r   r   Úformat_compound_unitÕ   sl    =
ú
ú	ü
 
ú	ü"r9   )Ú
__future__r   ÚdecimalÚtypingr   Z
babel.corer   Zbabel.numbersr   r   Ztyping_extensionsr   r   r   r   r   r-   r4   r9   r   r   r   r   Ú<module>   s8   ý!ûùVý6ù	÷