
    EqgP                        d Z ddlmZmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ 	 ddlmZ n{# e$ rs dd	lZdd	lZej                            ej        d                   d
k    s)ej                            ej        d                   dk    r edej                   g Zn Y nw xY weZ eg d          Z G d de          Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d  Z+d! Z,d	S )"zMethods for getting information about short phone numbers,
such as short codes and emergency numbers.

Note most commercial short numbers are not handled here, but by phonenumberutil.py
   )U_EMPTY_STRINGprnt)PhoneMetadata)_extract_possible_number_PLUS_CHARS_PATTERN)normalize_digits_onlyregion_codes_for_country_code)national_significant_number_match_national_number)_AVAILABLE_REGION_CODES    Nzbuildmetadatafromxml.pyzbuildprefixdata.pyzFFailed to import generated shortdata (but OK as during autogeneration))file)BRCLNIc                   "    e Zd ZdZdZdZdZdZdS )ShortNumberCostz!Cost categories of short numbers.r   r         N)__name__
__module____qualname____doc__	TOLL_FREESTANDARD_RATEPREMIUM_RATEUNKNOWN_COST     N/var/www/html/env/lib/python3.11/site-packages/phonenumbers/shortnumberinfo.pyr   r   3   s(        ++IMLLLLr   r   c                 2    t          | j                  }||v S )zqHelper method to check that the country calling code of the number matches
    the region it's being dialed from.)r	   country_code)numobjregion_dialing_fromregion_codess      r    #_region_dialing_from_matches_numberr&   ;   s     11DEEL</0r   c                     t          | |          sdS t          j        |          }|dS t          t	          |                     }||j        j        v S )a|  Check whether a short number is a possible number when dialled from a
    region. This provides a more lenient check than
    is_valid_short_number_for_region.

    Arguments:
    short_numobj -- the short number to check as a PhoneNumber object.
    region_dialing_from -- the region from which the number is dialed

    Return whether the number is a possible short number.
    F)r&   r   short_metadata_for_regionlenr
   general_descpossible_length)short_numobjr$   metadatashort_numlens       r    #is_possible_short_number_for_regionr/   B   s_     /|=PQQ u67JKKHu2<@@AALH1AABr   c                     t          | j                  }t          t          |                     }|D ]*}t	          j        |          }|||j        j        v r dS +dS )ab  Check whether a short number is a possible number.

    If a country calling code is shared by multiple regions, this returns True
    if it's possible in any of them. This provides a more lenient check than
    is_valid_short_number.

    Arguments:
    numobj -- the short number to check

    Return whether the number is a possible short number.
    NTF)r	   r"   r)   r
   r   r(   r*   r+   )r#   r%   short_number_lenregionr-   s        r    is_possible_short_numberr3   V   sv     11DEEL6v>>??   :6BBx4DDD44 E5r   c                     t          | |          sdS t          j        |          }|dS t          |           }|j        }t          ||          sdS |j        }|j        dS t          ||          S )a  Tests whether a short number matches a valid pattern in a region.

    Note that this doesn't verify the number is actually in use, which is
    impossible to tell by just looking at the number itself.

    Arguments:
    short_numobj -- the short number to check as a PhoneNumber object.
    region_dialing_from -- the region from which the number is dialed

    Return whether the short number matches a valid pattern
    F)r&   r   r(   r
   r*   ,_matches_possible_number_and_national_number
short_codenational_number_pattern)r,   r$   r-   short_numberr*   short_number_descs         r     is_valid_short_number_for_regionr:   n   s     /|=PQQ u67JKKHu.|<<L(L7lSS u +08u7FWXXXr   c                     t          | j                  }t          | |          }t          |          dk    r|dS t	          | |          S )a  Tests whether a short number matches a valid pattern.

    If a country calling code is shared by multiple regions, this returns True
    if it's valid in any of them. Note that this doesn't verify the number is
    actually in use, which is impossible to tell by just looking at the number
    itself. See is_valid_short_number_for_region for details.

    Arguments:
    numobj - the short number for which we want to test the validity

    Return whether the short number matches a valid pattern
    r   NT)r	   r"   ._region_code_for_short_number_from_region_listr)   r:   )r#   r%   region_codes      r    is_valid_short_numberr>      sR     11DEEL@VVK
<1!8 t+FK@@@r   c                     t          | |          st          j        S t          j        |          }|t          j        S t          |           }t          |          |j        j        vrt          j        S t          ||j
                  rt          j        S t          ||j                  rt          j        S t          ||j                  rt          j        S t!          ||          rt          j        S t          j        S )a  Gets the expected cost category of a short number when dialled from a
    region (however, nothing is implied about its validity). If it is
    important that the number is valid, then its validity must first be
    checked using is_valid_short_number_for_region. Note that emergency
    numbers are always considered toll-free.

    Example usage:
    short_number = "110"
    region_code = "FR"
    if phonenumbers.is_valid_short_number_for_region(short_number, region_code):
        cost = phonenumbers.expected_cost(short_number, region_code)  # ShortNumberCost
        # Do something with the cost information here.

    Arguments:
    short_numobj -- the short number for which we want to know the expected cost category
              as a PhoneNumber object.
    region_dialing_from -- the region from which the number is dialed

    Return the expected cost category for that region of the short
    number. Returns UNKNOWN_COST if the number does not match a cost
    category. Note that an invalid number may match any cost category.
    )r&   r   r   r   r(   r
   r)   r*   r+   r5   premium_rater   standard_rater   	toll_freer   is_emergency_number)r,   r$   r-   r8   s       r    expected_cost_for_regionrD      s    . /|=PQQ ,++67JKKH++.|<<L
 !6!FFF++
 4L(BWXX ,++3L(BXYY -,,3L(BTUU )((<)<== )((''r   c                    t          | j                  }t          |          dk    rt          j        S t          |          dk    rt          | |d                   S t          j        }|D ]}t          | |          }|t          j        k    rt          j        c S |t          j        k    rt          j        c S |t          j        k    r|t          j        k    rt          j        }{|t          j        k    rt          d|          |S )a  Gets the expected cost category of a short number (however, nothing is
    implied about its validity). If the country calling code is unique to a
    region, this method behaves exactly the same as
    expected_cost_for_region. However, if the country calling code is
    shared by multiple regions, then it returns the highest cost in the
    sequence PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason
    for the position of UNKNOWN_COST in this order is that if a number is
    UNKNOWN_COST in one region but STANDARD_RATE or TOLL_FREE in another, its
    expected cost cannot be estimated as one of the latter since it might be a
    PREMIUM_RATE number.

    For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in
    Canada, the expected cost returned by this method will be STANDARD_RATE,
    since the NANPA countries share the same country calling code.

    Note: If the region from which the number is dialed is known, it is highly preferable to call
    expected_cost_for_region instead.

    Arguments:
    numobj -- the short number for which we want to know the expected cost category

    Return the highest expected cost category of the short number in the
    region(s) with the given country calling code
    r   r   z Unrecognized cost for region: %s)
r	   r"   r)   r   r   rD   r   r   r   	Exception)r#   r%   costr=   cost_for_regions        r    expected_costrI      s   2 11DEEL
<A++
<A'Q@@@$D# Q Q26;GGo:::"//// <<<"//// ===333&4 999>PPPKr   c                     t          |          dk    rdS t          |          dk    r|d         S t          |           }|D ]1}t          j        |          }|t	          ||j                  r|c S 2dS )zHelper method to get the region code for a given phone number, from a list of possible region
    codes. If the list contains more than one region, the first region for which the number is
    valid is returned.
    r   Nr   )r)   r
   r   r(   r5   r6   )r#   r%   national_numberr=   r-   s        r    r<   r<     s    
 <At	\		a		A1&99O#   :;GG$PQ`bjbu$v$v4r   c                 t    t          j        |           }|t          S |j        }|j        |j        S t          S )a   Gets a valid short number for the specified region.

    Arguments:
    region_code -- the region for which an example short number is needed.

    Returns a valid short number for the specified region. Returns an empty
    string when the metadata does not contain such information.
    )r   r(   r   r6   example_number)r=   r-   descs      r    _example_short_numberrO     s@     6{CCHD&""r   c                     t          j        |           }|t          S d}|t          j        k    r|j        }n1|t          j        k    r|j        }n|t          j        k    r|j	        }n	 ||j
        |j
        S t          S )a  Gets a valid short number for the specified cost category.

    Arguments:
    region_code -- the region for which an example short number is needed.
    cost -- the cost category of number that is needed.

    Returns a valid short number for the specified region and cost
    category. Returns an empty string when the metadata does not contain such
    information, or the cost is UNKNOWN_COST.
    N)r   r(   r   r   r   rB   r   rA   r   r@   rM   )r=   rG   r-   rN   s       r    _example_short_number_for_costrQ   '  s     6{CCHD(((!	.	.	.%	-	-	-$ 	D/;""r   c                 $    t          | |d          S )a  Returns whether the given number, exactly as dialled, might be used to
    connect to an emergency service in the given region.

    This function accepts a string, rather than a PhoneNumber, because it
    needs to distinguish cases such as "+1 911" and "911", where the former
    may not connect to an emergency service in all cases but the latter would.

    This function takes into account cases where the number might contain
    formatting, or might have additional digits appended (when it is okay to
    do that in the specified region).

    Arguments:
    number -- The phone number to test.
    region_code -- The region where the phone number is being dialed.

    Returns whether the number might be used to connect to an emergency
    service in the given region.
    T _matches_emergency_number_helpernumberr=   s     r    connects_to_emergency_numberrW   E  s    & ,FKFFFr   c                 $    t          | |d          S )aC  Returns true if the given number exactly matches an emergency service
    number in the given region.

    This method takes into account cases where the number might contain
    formatting, but doesn't allow additional digits to be appended.  Note that
    is_emergency_number(number, region) implies
    connects_to_emergency_number(number, region).

    Arguments:
    number -- The phone number to test.
    region_code -- The region where the phone number is being dialed.

    Returns if the number exactly matches an emergency services number in the
    given region.
    FrS   rU   s     r    rC   rC   [  s      ,FKGGGr   c                    t          |           }t          j        |          rdS t          j        |                                d           }||j        dS t          |          }|o|t          v}t          ||j        |          S )NF)
r   r   matchr   r(   upper	emergencyr   ._REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACTr   )rV   r=   allow_prefix_matchpossible_numberr-   normalized_numberallow_prefix_match_for_regions          r    rT   rT   n  s    .v66O 11 
 u6{7H7H7J7JDQQH8-5u-o>>%7 &i&19g&g "!"3X5G"?A A Ar   c                     t          | j                  }t          | |          }t          |           }t	          j        |          }|duot          ||j                  S )aH  Given a valid short number, determines whether it is carrier-specific
    (however, nothing is implied about its validity).  Carrier-specific numbers
    may connect to a different end-point, or not connect at all, depending
    on the user's carrier. If it is important that the number is valid, then
    its validity must first be checked using is_valid_short_number or
    is_valid_short_number_for_region.

    Arguments:
    numobj -- the valid short number to check

    Returns whether the short number is carrier-specific, assuming the input
    was a valid short number.
    N)r	   r"   r<   r
   r   r(   r5   carrier_specific)r#   r%   r=   rK   r-   s        r    is_carrier_specificrd     sd     11DEEL@VVK1&99O6{CCHD  e8(Jcddfr   c                     t          | |          sdS t          |           }t          j        |          }|duot	          ||j                  S )a  Given a valid short number, determines whether it is carrier-specific when
    dialed from the given region (however, nothing is implied about its
    validity). Carrier-specific numbers may connect to a different end-point,
    or not connect at all, depending on the user's carrier. If it is important
    that the number is valid, then its validity must first be checked using
    isValidShortNumber or isValidShortNumberForRegion. Returns false if the
    number doesn't match the region provided.

    Arguments:
    numobj -- the valid short number to check
    region_dialing_from -- the region from which the number is dialed

    Returns whether the short number is carrier-specific, assuming the input
    was a valid short number.
    FN)r&   r
   r   r(   r5   rc   )r#   r$   rK   r-   s       r    is_carrier_specific_for_regionrf     s^      /v7JKK u1&99O67JKKHD  e8(Jcddfr   c                     t          | |          sdS t          j        |          }|duo!t          t	          |           |j                  S )a4  Given a valid short number, determines whether it is an SMS service
    (however, nothing is implied about its validity). An SMS service is where
    the primary or only intended usage is to receive and/or send text messages
    (SMSs). This includes MMS as MMS numbers downgrade to SMS if the other
    party isn't MMS-capable. If it is important that the number is valid, then
    its validity must first be checked using is_valid_short_number or
    is_valid_short_number_for_region.  Returns False if the number doesn't
    match the region provided.

    Arguments:
    numobj -- the valid short number to check
    region_dialing_from -- the region from which the number is dialed

    Returns whether the short number is an SMS service in the provided region,
    assuming the input was a valid short number.
    FN)r&   r   r(   r5   r
   sms_services)r#   r$   r-   s      r    is_sms_service_for_regionri     s\    " /v7JKK u67JKKHD  u89TU[9\9\^f^sttvr   c                     |dS t          |j                  dk    rt          |           |j        vrdS t          | |d          S )NFr   )r)   r+   r   )rV   number_descs     r    r5   r5     sM    u
;&''!++S[[KD_5_5_u!&+u===r   )-r   utilr   r   phonemetadatar   phonenumberutilr   r   r   r	   r
   r   	shortdatar   _AVAILABLE_SHORT_REGION_CODESImportErrorossyspathbasenameargvstderrSUPPORTED_SHORT_REGIONSsetr]   objectr   r&   r/   r3   r:   r>   rD   rI   r<   rO   rQ   rW   rC   rT   rd   rf   ri   r5   r   r   r    <module>r{      s   ( ' & & & & & & & ( ( ( ( ( ( J J J J J J J J Q Q Q Q Q Q Q Q P P P P P P P PSSSSSSS    IIIJJJ
!%%)BBB
!%%)===U\_\fgggg(*%% 	&% 8  255G5G5G1H1H .    f   1 1 1C C C(  0Y Y Y6A A A,1( 1( 1(h- - -`  $  $  <G G G,H H H&A A A&f f f,f f f0v v v4> > > > >s   1 A5B)(B)