
    ƪb+                     x    d dl mZ d dlmZmZ  G d de      ZdefdZefdZddefd	Z	efd
Z
dddddefdZy)    )Locale)format_decimal
LC_NUMERICc                       e Zd Zd Zy)UnknownUnitErrorc                 :    t         j                  | |d|       y )Nz is not a known unit in )
ValueError__init__)selfunitlocales      -/usr/lib/python3/dist-packages/babel/units.pyr
   zUnknownUnitError.__init__   s    DD&"QR    N)__name__
__module____qualname__r
    r   r   r   r      s    Sr   r   longc                     t        j                  |      }t        | |      }|st        | |      |j                  j                  |i       j                  |      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   unit_display_namesget)measurement_unitlengthr   r   s       r   get_unit_namer   
   sT    , \\&!F.v>D$4VDD$$((r266v>>r   c                     t        j                  |      }|j                  d   }| |v r| S t        |t              D ]  }|j                  |       s|c S  y)a  
    Expand an 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)unit_idr   r    unit_patterns       r   r   r   '   sW    " \\&!FLL1M-}#6    ) r   Nc                 l   t        j                  |      }t        ||      }|st        ||      |j                  d   |   j                  |i       }t        | t              r| }d}nt        | ||      }|j                  |       }||v r||   j                  |      S |dt        |||      xs |S )a  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'

    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
    r   r   r    one )r   r   )r   r   r   r   r"   r   
isinstancestrr   plural_formformatr   )	valuer   r   r.   r   q_unitr    formatted_valuer-   s	            r   format_unitr2   A   s    ^ \\&!F 0@F$4VDDLL1&9==fbIM%(?((/m#[)00AA
 		'v	F	ZJZ	Z r   c                     t        j                  |      }t        | |      } t        ||      }| r|sy| j                  dd      d   }|j                  dd      d   }t        |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)numerator_unitdenominator_unitr   bare_numerator_unitbare_denominator_units        r   _find_compound_unitr<      s    0 \\&!F (vFN)*:6J /
 )..sA6r:,223:2> -@BWXaghhr   r5   c                    t        j                  |      }|r'|r%|dk(  r t        |||      }|rt        | ||||      S t	        | t
              r| }n!|rt        | ||||      }nt        | ||      }t	        |t
              r|}	n|r|dk(  r`t        ||      }|j                  d   j                  |i       j                  |i       j                  d      }
|
r|
j                  |      S d}t        |||||      j                         }	nt        |||      }	|j                  d   j                  di       j                  |i       j                  d	d
      }
|
j                  ||	      S )a  
    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(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="fathom", locale="sv")
    '35 minuter per famn'

    >>> 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
    :return: A formatted compound value.
    r5   r   )r   r.   r   )r.   r   r    per compound_unit_patternscompoundz{0}/{1})r   r   r<   r2   r+   r,   r   r   r"   r   r.   strip)numerator_valuer8   denominator_valuer9   r   r.   r   compound_unitformatted_numeratorformatted_denominatorper_patterns              r   format_compound_unitrI      s   b \\&!F */@A/E+N<LU[\fU[djkk /3'-	)^F6RX
 -_VTZ[#S) 1	!12B6R ,,7;;<LbQUUV\^`aeefklK"))*=>> !# +/vV\!

%' 	 !//@X^ _ ,,78<<UBGKKFTVW[[\fhqrK13HIIr   )
babel.corer   babel.numbersr   r   r	   r   r   r   r2   r<   rI   r   r   r   <module>rL      se     4Sz S
 ,2* ?: (2  4 17tJ EP BL -ib %)$$z[Jr   