
    Eqg                       d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZ ddlmZ ddlmZmZ dd	lmZmZmZ 	 dd
lmZ ny# e$ rq ddlZej                             ej!        d                   dk    s)ej                             ej!        d                   dk    r e	dej"                   ddiZn Y nw xY weZ#ej$        ej%        z  Z&dZ'dZ(dZ)dZ* ed          Z+dZ,d ed          iZ- e.d          Z/ e.d          Z0e/ e1d          z  Z2 ed          Z3 ed          Z4 ed          Z5 ed          Z6 ed          Z7 ed          Z8 ed            ed            ed!           ed!           ed"           ed"           ed#           ed#           ed$           ed$           ed%           ed%           ed&           ed&           ed'           ed'           ed(           ed(           ed           ed          i
Z9i  ed)           ed"           ed*           ed"           ed+           ed"           ed,           ed#           ed-           ed#           ed.           ed#           ed/           ed$           ed0           ed$           ed1           ed$           ed2           ed%           ed3           ed%           ed4           ed%           ed5           ed&           ed6           ed&           ed7           ed&           ed8           ed'           ed9           ed'           ed:           ed'           ed;           ed'           ed<           ed(           ed=           ed(           ed>           ed(           ed?           ed           ed@           ed           edA           ed           edB           ed          i	Z: e;e:fi e9Z< e;e3e3 ed           ed           edC           edC          ifi e9Z= e;i  edD           edD           edE           edD           edF           edD           edG           edD           edH           edD           edI           edD           edJ           edD           edK           edD           edL           edD           edM           edM           edN           edM           edO           edO           edP           edO           edQ           edO           edR           edR           edS           edR          fi  e;dT e:>                                D             dU e:>                                D             z   fi e9Z? ej@         edV                    ZA edW           edX          z   ZB edY          ZC ejD        e:>                                           ejD        dZ e:>                                D                       z   ZE ed[          ZF ej@         ed\          eFz    ed]          z             ZG ej@         ed\          eBz    ed]          z             ZH ej@         ed^          eCz    ed_          z   ej$                  ZI ed\          eFz   eCz    ed`          z   ZJ ej@        eJej$                  ZK eda          ZL ej@        eL          ZM edb          ZN ej@        eN          ZO ej@         edc                    ZPeC edd          e'z  z    ede          z    ed\          z   eFz    edf          z   eBz   e4z    edg          z   eCz    edh          z   eBz   e4z   eEz   eCz    edg          z   ZQ edi          ZRdj ZSdk ZT eTdl          ZU eTdm          ZVdnZWd^eCz   dez   eWz   d_z   ZXdoe3z   eXz   dz   eCz   eXz   dpz   ZY ej@        eY          ZZeEeCz   Z[d\e[z   dqz   e[z   drz   Z\d\eEz   dqz   e[z   drz   Z]dse\z   dtz   e]z   duz   Z^ ej@        e^          Z_ ej@         edv          eUz    edw          z   e&          Z` ej@        eQ edv          z   eUz    edx          z   e&          Za ej@         edy                    Zb ej@         edz                    Zcd{Zdd|Zed}Zf ej@        d~          Zg G d deh          Zi G d deh          Zj G d deh          Zk G d deh          Zl e1            am e1            an e1            aod Zp ep             d Zqd Zrd Zsd ZtddZud Zvd Zwd Zxd Zyd Zzd Z{d Z|d Z}d Z~d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Z	 ddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdÄ ZdĄ Zejj        fdńZdƄ ZdǄ ZdȄ ZdɄ Zdʄ Zd˄ Zd̄ Zd̈́ Zd΄ Zdτ ZdЄ Zdф Z	 	 dd҄Zdӄ ZdԄ ZdՄ Zdք Zdׄ Zd؄ Zdل Zdڄ Zdۄ Zd܄ Zd݄ Z G dބ deeȦ          Zd Zd ZdS )a"  Python phone number parsing and formatting library

If you use this library, and want to be notified about important changes,
please sign up to the libphonenumber mailing list at
https://groups.google.com/forum/#!aboutgroup/libphonenumber-discuss.

NOTE: A lot of methods in this module require Region Code strings. These must
be provided using CLDR two-letter region-code format. These should be in
upper-case. The list of the codes can be found here:
http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm
    N   )	fullmatch)UnicodeMixinuunicodprntto_long)U_EMPTY_STRINGU_SPACEU_DASHU_TILDEU_ZEROU_SEMICOLON)digit)PhoneNumberCountryCodeSource)NumberFormatPhoneMetadataREGION_CODE_FOR_NON_GEO_ENTITY)_COUNTRY_CODE_TO_REGION_CODEzbuildmetadatafromxml.pyzbuildprefixdata.pyzAFailed to import generated data (but OK as during autogeneration))file)US            ZZ6   9)V   )4   )r!   r   7   >   +*z;ext=ztel:z;phone-context=z;isub=012345678ABCDEFGHIJKLMNOPQRSTUVWXYZ#-u   －u   ‐u   ‑u   ‒u   –u   —u   ―u   −/u   ／ u   　u   ⁠.u   ．c                 :    g | ]}|                                 |fS  lower.0_cs     N/var/www/html/env/lib/python3.11/site-packages/phonenumbers/phonenumberutil.py
<listcomp>rV      s%    0c0c0cb"((**b1A0c0c0c    c                     g | ]}||fS rO   rO   rR   s     rU   rV   rV      s    0[0[0[b"b0[0[0[rW   u   [\d]+(?:[~⁓∼～][\d]+)?u   -x‐-―−ー－-／ u+    ­​⁠　()（）［］.\[\]/~⁓∼～z\dc                 6    g | ]}|                                 S rO   rP   )rS   _ks     rU   rV   rV      s     $Q$Q$QBRXXZZ$Q$Q$QrW   u   +＋[z]+()]z[\\/] *xz(?u)(?:_|[^#\w])+$z(?:.*?[A-Za-z]){3}.*z{%d}|z]*(?:[z]*z){3,}[z ext. c                 V    t          d          t          z   t          d          | z  z   S )Nr\   z{1,%d}))r   _DIGITS)
max_lengths    rU   _extn_digitsrc   "  s#    S66Gq||j899rW   c                 4   d}d}d}d}t          d          }t          d          }t          d          }t          d          }t          d	          }	t          d
          }
t          t          |          z   }||z   |z   t          |          z   |z   }||	z   |z   t          |          z   |z   }|
t          |          z   t          d          z   }|t          d          z   |z   t          d          z   |z   t          d          z   |z   }| rt          d          }t          d          }||z   |z   t          |          z   |z   }|t          d          z   |z   t          |          z   |z   }|t          d          z   |z   t          d          z   |z   S |S )N      	      u	   [  \t,]*u   [:\.．]?[  \t,-]*z#?u:   (?:e?xt(?:ensi(?:ó?|ó))?n?|ｅ?ｘｔｎ?|доб|anexo)u    (?:[xｘ#＃~～]|int|ｉｎｔ)z[- ]+rI   r_   u   [  \t]*z
(?:,{2}|;)z(?:,)+)r   _RFC3966_EXTN_PREFIXrc   )for_parsingext_limit_after_explicit_labelext_limit_after_likely_labelext_limit_after_ambiguous_charext_limit_when_not_sure0possible_separators_between_number_and_ext_labelpossible_chars_after_ext_labeloptional_extn_suffixexplicit_ext_labelsambiguous_ext_labelsambiguous_separatorrfc_extnexplicit_extnambiguous_extnamerican_style_extn_with_suffixextension_pattern-possible_separators_number_ext_label_no_comma"auto_dialling_and_ext_labels_foundauto_dialling_extnonly_commas_extns                       rU   _create_extn_patternr~   )  s!   
 &("#% %&"789I7J7J4%&'E%F%F"T77 xyy QRRG**#l3Q&R&RRHEH[[346BCa6b6bc)*M GI]]457CDb7c7cdfz{N':\Ja=b=b'befgjekek'k# "AcFF*&')*30'(*+C&&1 99
  " 9:/8J8J5 ./|__*K@ACab*+GHHIK_` JhKK(*HIKWXvKwKwx01 "AcFF*"#%&sVV, ! 	" rW   TFz[\-\.\(\)]?z^\z*$z	]+((\-)*[z])*z^(z\.)*z\.?$z(?:z)$z)?z(?:\D+)z(\\\d)z$NPz$FGz$CCz	\(?\\1\)?c                   8    e Zd ZdZdZdZdZdZed             Z	dS )PhoneNumberFormata~  
    Phone number format.

    INTERNATIONAL and NATIONAL formats are consistent with the definition in
    ITU-T Recommendation E123. However we follow local conventions such as using
    '-' instead of whitespace as separators. For example, the number of the
    Google Switzerland office will be written as "+41 44 668 1800" in
    INTERNATIONAL format, and as "044 668 1800" in NATIONAL format. E164 format
    is as per INTERNATIONAL format but with no formatting applied,
    e.g. "+41446681800". RFC3966 is as per INTERNATIONAL format, but with all
    spaces and other separating symbols replaced with a hyphen, and with any
    phone number extension appended with ";ext=". It also will have a prefix of
    "tel:" added, e.g. "tel:+41-44-668-1800".

    Note: If you are considering storing the number in a neutral format, you
    are highly advised to use the PhoneNumber class.
    r   r   r   r   c                    |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S t          d|z            S )z;Return a string representation of a PhoneNumberFormat valueE164INTERNATIONALNATIONALRFC3966INVALID (%d))r   r   r   r   r   r   clsvals     rU   	to_stringzPhoneNumberFormat.to_string  s     #(((V99%333_%%%%...Z== %---Y<<^c)***rW   N)
__name__
__module____qualname____doc__r   r   r   r   classmethodr   rO   rW   rU   r   r     sM         " DMHG+ + [+ + +rW   r   c                   n    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ed             Zed             ZdS )PhoneNumberTypezType of phone numbers.r   r   r   r         rh         rg   
   c   c                    t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j	        t           j
        t           j        t           j        fS N)r   
FIXED_LINEMOBILEFIXED_LINE_OR_MOBILE	TOLL_FREEPREMIUM_RATESHARED_COSTVOIPPERSONAL_NUMBERPAGERUAN	VOICEMAILUNKNOWN)r   s    rU   valueszPhoneNumberType.values  sT    *&4),+$/%#)') 	)rW   c                    |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j	        k    rt          d          S |t           j
        k    rt          d	          S |t           j        k    rt          d
          S |t           j        k    rt          d          S |t           j        k    rt          d          S t          d|z            S )z9Return a string representation of a PhoneNumberType valuer   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s     rU   r   zPhoneNumberType.to_string  sX    /,,,\??"O***X;;O888+,,,O---[>>!O000^$$$O///]###O(((V99O333&'''O)))W::O'''U88OO---[>>!O+++Y<<^c)***rW   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   rW   rU   r   r     s          JF IL KD OE CI G) ) [) + + [+ + +rW   r   c                   <    e Zd ZdZdZdZdZdZdZe	d             Z
dS )		MatchTypezTypes of phone number matches.r   r   r   r   r   c                 \   |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S t          d|z            S )z3Return a string representation of a MatchType valueNOT_A_NUMBERNO_MATCHSHORT_NSN_MATCH	NSN_MATCHEXACT_MATCHr   )r   r   r   r   r   r   r   r   s     rU   r   zMatchType.to_string)  s     )(((^$$$I&&&Z== I---&'''I'''[>>!I)))]###^c)***rW   N)r   r   r   r   r   r   r   r   r   r   r   rO   rW   rU   r   r     sR        ((LH
 O I K+ + [+ + +rW   r   c                   @    e Zd ZdZdZdZdZdZdZdZ	e
d             Zd	S )
ValidationResultzEPossible outcomes when testing if a PhoneNumber is a possible number.r   r   r   r   r   r   c                    |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S |t           j        k    rt          d          S t          d|z            S )z:Return a string representation of a ValidationResult valueIS_POSSIBLEIS_POSSIBLE_LOCAL_ONLYINVALID_COUNTRY_CODE	TOO_SHORTINVALID_LENGTHTOO_LONGr   )r   r   r   r   r   r   r   r   r   s     rU   r   zValidationResult.to_stringQ  s     "...]###$;;;-...$999+,,,$...[>>!$333%&&&$---Z== ^c)***rW   N)r   r   r   r   r   r   r   r   r   r   r   r   rO   rW   rU   r   r   :  sW        OOK
 I NH+ + [+ + +rW   r   c                  :   t                                            t                                           t                                          D ]^\  } }t          |          dk    r,|d         t          k    rt                              |            Dt                               |           _t          t           v rt           	                    t                     t                                           t                              t          t                              d S )Nr   r   )SUPPORTED_REGIONSclear!COUNTRY_CODES_FOR_NON_GEO_REGIONSCOUNTRY_CODE_TO_REGION_CODEitemslenr   addupdateremove_NANPA_REGIONS_NANPA_COUNTRY_CODE)ccregion_codess     rU   _regenerate_derived_datar   j  s    %++---7==?? 3 3L""|A:X'X'X-11"5555$$\2222%):::  !?@@@56IJKKKKKrW   c                     t          | j        | j        t          | j                  | j        | j        | j                  }d|_        |S )z6Return a mutable copy of the given NumberFormat object)patternformatleading_digits_patternnational_prefix_formatting_rule(national_prefix_optional_when_formatting%domestic_carrier_code_formatting_ruleT)	r   r   r   listr   r   r   r   _mutable)othercopys     rU   _copy_number_formatr   |  sP    $|/3E4P/Q/Q8=8]AFAo>C>ik k kD DMKrW   c                 d   t                               |           }|r| |                                d         } t                              |           }|r| d|                                         } t                              |           }|r| d|                                         } | S t
          S )a  Attempt to extract a possible number from the string passed in.

    This currently strips all leading characters that cannot be used to
    start a phone number. Characters that can be used to start a phone number
    are defined in the VALID_START_CHAR_PATTERN. If none of these characters
    are found in the number passed in, an empty string is returned. This
    function also attempts to strip off any alternative extensions or endings
    if two or more are present, such as in the case of: (530) 583-6985
    x302/x2303. The second extension here makes this actually two phone
    numbers, (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the
    second extension so that the first number is parsed correctly.

    Arguments:
    number -- The string that might contain a phone number.

    Returns the number, stripped of any non-phone-number prefix (such
    as "Tel:") or an empty string if no character used to start phone
    numbers (such as + or any digit) is found in the number
    N)_VALID_START_CHAR_PATTERNsearchstart_UNWANTED_END_CHAR_PATTERN_SECOND_NUMBER_START_PATTERNr
   )numbermatchtrailing_chars_matchsecond_number_matchs       rU   _extract_possible_numberr     s    ( &,,V44E '9@@HH 	;9177999:F:AA&II 	:80668889FrW   c                 ~    t          |           t          k     rdS t          t          |           }t	          |          S )aB  Checks to see if a string could possibly be a phone number.

    At the moment, checks to see that the string begins with at least 2
    digits, ignoring any punctuation commonly found in phone numbers.  This
    method does not require the number to be normalized in advance - but does
    assume that leading non-number symbols have been removed, such as by the
    method _extract_possible_number.

    Arguments:
    number -- string to be checked for viability as a phone number

    Returns True if the number could be a phone number of some sort, otherwise
    False
    F)r   _MIN_LENGTH_FOR_NSNr   _VALID_PHONE_NUMBER_PATTERNbool)r   r   s     rU   _is_viable_phone_numberr     s7     6{{(((u16::E;;rW   c                 z    t          t          |           }|rt          | t          d          S t	          |           S )aD  Normalizes a string of characters representing a phone number.

    This performs the following conversions:
     - Punctuation is stripped.
     - For ALPHA/VANITY numbers:
        - Letters are converted to their numeric representation on a telephone
          keypad. The keypad used here is the one defined in ITU
          Recommendation E.161. This is only done if there are 3 or more
          letters in the number, to lessen the risk that such letters are
          typos.
     - For other numbers:
        - Wide-ascii digits are converted to normal ASCII (European) digits.
        - Arabic-Indic numerals are converted to European numerals.
        - Spurious alpha characters are stripped.

    Arguments:
    number -- string representing a phone number

    Returns the normalized string version of the phone number.
    T)r   _VALID_ALPHA_PHONE_PATTERN_normalize_helper_ALPHA_PHONE_MAPPINGSnormalize_digits_only)r   ms     rU   
_normalizer     s=    * 	,f55A - )>EEE$V,,,rW   c                     t          |           } t          |           }t          }t          |          D ]>}t	          | |         d          }|dk    r|t          |          z  }1|r|| |         z  }?|S )a  Normalizes a string of characters representing a phone number.

    This converts wide-ascii and arabic-indic numerals to European numerals,
    and strips punctuation and alpha characters (optional).

    Arguments:
    number -- a string representing a phone number
    keep_non_digits -- whether to keep non-digits

    Returns the normalized string version of the phone number.
    )r   r   r
   rangeunicode_digit)r   keep_non_digitsnumber_lengthnormalized_digitsiids         rU   r   r     s     F^^FKKM&M"" , ,&*b))77* 	,+rW   c                 .    t          | t          d          S )aK  Normalizes a string of characters representing a phone number.

    This strips all characters which are not diallable on a mobile phone
    keypad (including all non-ASCII digits).

    Arguments:
    number -- a string of characters representing a phone number

    Returns the normalized string version of the phone number.
    T)r   _DIALLABLE_CHAR_MAPPINGSr   s    rU   normalize_diallable_chars_onlyr     s     V%=tDDDrW   c                 .    t          | t          d          S )zlConvert alpha chars in a number to their respective digits on a keypad,
    but retains existing formatting.F)r   r   r   s    rU   "convert_alpha_characters_in_numberr     s     V%:EBBBrW   c                 $   t          j        t          |           d          }|dS t          |           }| j        }|j        | j        s|t          vrdS |t          j	        k    r|t          v rdS t          ||          sdS t          |           S )a  Return length of the geographical area code for a number.

    Gets the length of the geographical area code from the PhoneNumber object
    passed in, so that clients could use it to split a national significant
    number into geographical area code and subscriber number. It works in such
    a way that the resultant subscriber number should be diallable, at least
    on some devices. An example of how this could be used:

    >>> import phonenumbers
    >>> numobj = phonenumbers.parse("16502530000", "US")
    >>> nsn = phonenumbers.national_significant_number(numobj)
    >>> ac_len = phonenumbers.length_of_geographical_area_code(numobj)
    >>> if ac_len > 0:
    ...     area_code = nsn[:ac_len]
    ...     subscriber_number = nsn[ac_len:]
    ... else:
    ...     area_code = ""
    ...     subscriber_number = nsn

    N.B.: area code is a very ambiguous concept, so the I18N team generally
    recommends against using it for most purposes, but recommends using the
    more general national_number instead. Read the following carefully before
    deciding to use this method:

     - geographical area codes change over time, and this method honors those
       changes; therefore, it doesn't guarantee the stability of the result it
       produces.
     - subscriber numbers may not be diallable from all devices (notably
       mobile devices, which typically require the full national_number to be
       dialled in most countries).
     - most non-geographical numbers have no area codes, including numbers
       from non-geographical entities.
     - some geographical numbers have no area codes.

    Arguments:
    numobj -- The PhoneNumber object to find the length of the area code form.

    Returns the length of area code of the PhoneNumber object passed in.
    Nr   )r   metadata_for_regionregion_code_for_numbernumber_typecountry_codenational_prefixitalian_leading_zero2_COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODESr   r   /_GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODESis_number_type_geographical#length_of_national_destination_code)numobjmetadatantyper   s       rU    length_of_geographical_area_coder  	  s    P 01G1O1OQUVVHqE&L 	 (1L(NNNq'''	H	H	H
 q&ul;; q.v666rW   c                    | j         +t                      }|                    |            d|_         n| }t          |t          j                  }t          j        t          |          }t          |          dk    rdS t          |           t          j        k    rJt          | j                  }|t          k    r+t          |d                   t          |d                   z   S t          |d                   S )aB  Return length of the national destination code code for a number.

    Gets the length of the national destination code (NDC) from the
    PhoneNumber object passed in, so that clients could use it to split a
    national significant number into NDC and subscriber number. The NDC of a
    phone number is normally the first group of digit(s) right after the
    country calling code when the number is formatted in the international
    format, if there is a subscriber number part that follows.

    N.B.: similar to an area code, not all numbers have an NDC!

    An example of how this could be used:

    >>> import phonenumbers
    >>> numobj = phonenumbers.parse("18002530000", "US")
    >>> nsn = phonenumbers.national_significant_number(numobj)
    >>> ndc_len = phonenumbers.length_of_national_destination_code(numobj)
    >>> if ndc_len > 0:
    ...     national_destination_code = nsn[:ndc_len]
    ...     subscriber_number = nsn[ndc_len:]
    ... else:
    ...     national_destination_code = ""
    ...     subscriber_number = nsn

    Refer to the unittests to see the difference between this function and
    length_of_geographical_area_code.

    Arguments:
    numobj -- The PhoneNumber object to find the length of the NDC from.

    Returns the length of NDC of the PhoneNumber object passed in, which
    could be zero.
    Nr   r   r   )	extensionr   
merge_fromformat_numberr   r   resplitNON_DIGITS_PATTERNr   r   r   r   country_mobile_tokenr   r
   )r  copied_numobjnsnnumber_groupsmobile_tokens        rU   r  r  N  s    D # $  ((("&
'8'F
G
GCH/55M =Qq6o444 ,F,?@@>))}Q'((3}Q/?+@+@@@}Q   rW   c                 B    t                               | t                    S )a  Returns the mobile token for the provided country calling code if it has one, otherwise
    returns an empty string. A mobile token is a number inserted before the area code when dialing
    a mobile number from that country from abroad.

    Arguments:
    country_code -- the country calling code for which we want the mobile token
    Returns the mobile token, as a string, for the given country calling code.
    )_MOBILE_TOKEN_MAPPINGSgetr
   )r   s    rU   r  r    s     "%%lNCCCrW   c                     g }| D ]Y}|                     |                                d          }||                    |           B|s|                    |           Zt          j        |          S )a  Normalizes a string of characters representing a phone number by
    replacing all characters found in the accompanying map with the values
    therein, and stripping all other characters if remove_non_matches is true.

    Arguments:
    number -- a string representing a phone number
    replacements -- a mapping of characters to what they should be replaced
              by in the normalized version of the phone number
    remove_non_matches -- indicates whether characters that are not able to be
              replaced should be stripped from the number. If this is False,
              they will be left unchanged in the number.

    Returns the normalized string version of the phone number.
    N)r  upperappendr
   join)r   replacementsremove_non_matchesnormalized_numberchar	new_digits         rU   r   r     s      + + $$TZZ\\488	 $$Y////# 	+$$T***0111rW   c                  N    t          t                                                    S )a  Returns all country calling codes the library has metadata for, covering
    both non-geographical entities (global network calling codes) and those
    used for geographical entities. This could be used to populate a drop-down
    box of country calling codes for a phone-number widget, for instance.

    Returns an unordered set of the country calling codes for every geographica
    and non-geographical entity the library supports.
    )setr   keysrO   rW   rU   supported_calling_codesr$    s     *//11222rW   c                 \    | dS t          | j                  dk    p| j        d         dk    S )zWReturns true if there is any possible number data set for a particular PhoneNumberDesc.NFr   r   r   )r   possible_lengthdescs    rU   _desc_has_possible_number_datar)    s6    
 |ut#$$)JT-A!-D-JJrW   c                 L    | dS | j         dupt          |           p| j        duS )zGReturns true if there is any data set for a particular PhoneNumberDesc.NF)example_numberr)  national_number_patternr'  s    rU   _desc_has_datar-    s>    |u
  , 7*4007)58rW   c                     t                      }t                                          D ]O}|t          j        t          j        fv rt          t          | |                    r|                    |           P|S )zkReturns the types we have metadata for based on the PhoneMetadata object passed in, which must be non-None.)r"  r   r   r   r   r-  _number_desc_by_typer   )r  numtypesnumtypes      rU   _supported_types_for_metadatar2    st    uuH"))++ " ";_=TUUU .xAABB 	"LL!!!OrW   c                     t          |           st                      S t          j        |                                           }|J t          |          S )aF  Returns the types for a given region which the library has metadata for.

    Will not include FIXED_LINE_OR_MOBILE (if numbers in this region could
    be classified as FIXED_LINE_OR_MOBILE, both FIXED_LINE and MOBILE would
    be present) and UNKNOWN.

    No types will be returned for invalid or unknown region codes.
    )_is_valid_region_coder"  r   r   r  r2  region_coder  s     rU   supported_types_for_regionr7    sS     !-- uu01B1B1D1DEEH(222rW   c                 j    t          j        | d          }|t                      S t          |          S )a  Returns the types for a country-code belonging to a non-geographical entity
    which the library has metadata for. Will not include FIXED_LINE_OR_MOBILE
    (if numbers for this non-geographical entity could be classified as
    FIXED_LINE_OR_MOBILE, both FIXED_LINE and MOBILE would be present) and
    UNKNOWN.

    No types will be returned for country calling codes that do not map to a
    known non-geographical entity.
    N)r   metadata_for_nongeo_regionr"  r2  )r   r  s     rU   "supported_types_for_non_geo_entityr:    s3     7dKKHuu(222rW   c                 N    | dS t          t          t          |                     S )zHelper function to check if the national prefix formatting rule has the
    first group only, i.e., does not start with the national prefix.
    NT)r   r    _FIRST_GROUP_ONLY_PREFIX_PATTERN)r   s    rU   %_formatting_rule_has_first_group_onlyr=    s3     '.t	:9; ; < < <rW   c                 F    t          t          |           | j                  S )a>  Tests whether a phone number has a geographical association.

    It checks if the number is associated with a certain region in the country
    to which it belongs. Note that this doesn't verify if the number is
    actually in use.
    country_code -- the country calling code for which we want the mobile token
    )r  r   r   r  s    rU   is_number_geographicalr@    s     '{6':':F<OPPPrW   c                 t    | t           j        k    p(| t           j        k    p|t          v o| t           j        k    S )a*  Tests whether a phone number has a geographical association,
    as represented by its type and the country it belongs to.

    This version of isNumberGeographical exists since calculating the phone
    number type is expensive; if we have already done this, we don't want to
    do it again.
    )r   r   r   _GEO_MOBILE_COUNTRIESr   )num_typer   s     rU   r  r  !  sB     22 1<<133 0//2rW   c                     | dS | t           v S )z;Helper function to check region code is not unknown or NoneNF)r   r6  s    rU   r4  r4  /  s    u,,-rW   c                     | t           v S r   )r   )country_calling_codes    rU   _has_valid_country_calling_coderH  6  s     $??@rW   c                    | j         dk    r*| j        pd}t          |          dk    s| j        | j        S | j        }t	          |           }|t
          j        k    rt          |||          S t          |          s|S t          |          }t          j        ||                                          }t          |||          }t          | |||          }t          |||          S )am  Formats a phone number in the specified format using default rules.

    Note that this does not promise to produce a phone number that the user
    can dial from where they are - although we do format in either 'national'
    or 'international' format depending on what the client asks for, we do not
    currently support a more abbreviated format, such as for users in the same
    "area" who could potentially dial the number without area code. Note that
    if the phone number has a country calling code of 0 or an otherwise
    invalid country calling code, we cannot work out which formatting rules to
    apply so we return the national significant number with no formatting
    applied.

    Arguments:
    numobj -- The phone number to be formatted.
    num_format -- The format the phone number should be formatted into

    Returns the formatted phone number.
    r    )national_number	raw_inputr   r   national_significant_numberr   r   (_prefix_number_with_country_calling_coderH  region_code_for_country_coder   #metadata_for_region_or_calling_coder  _format_nsn!_maybe_append_formatted_extension)r  
num_formatrL  rG  r  r6  r  formatted_numbers           rU   r  r  :  s   & ""
 $*	y>>A!4!<##!.
%f
-
-C&+++ 88LjZ]^^^*+?@@ 

 //CDDK @AUWbWhWhWjWjkkH"3*==89A9C9IK K 44H4>4DF F FrW   c                 "   | j         }t          |           }t          |          s|S t          |          }t	          j        ||          }t          }t          ||          }||}nt          |          }	|j	        }
|
r[|j
        }|rK|
                    t          |          }
|
                    t          t          d                    }
|
|	_	        nd|	_	        t          ||	|          }t!          | |||          }t#          |||          }|S )a  Formats a phone number using client-defined formatting rules.

    Note that if the phone number has a country calling code of zero or an
    otherwise invalid country calling code, we cannot work out things like
    whether there should be a national prefix applied, or how to format
    extensions, so we return the national significant number with no
    formatting applied.

    Arguments:
    numobj -- The phone number to be formatted
    number_format -- The format the phone number should be formatted into,
              as a PhoneNumberFormat value.
    user_defined_formats -- formatting rules specified by clients, as a list
              of NumberFormat objects.

    Returns the formatted phone number.
    N\1)r   rM  rH  rO  r   rP  r
   %_choose_formatting_pattern_for_numberr   r   r   replace
_NP_STRING
_FG_STRINGr   _format_nsn_using_patternrR  rN  )r  number_formatuser_defined_formatsr   r  r6  r  rT  formatting_patternnum_format_copynp_formatting_ruler   s               rU   format_by_patternra  q  sI   $ &L
%f
-
-C*<88 

 /|<<K@{[[H%>?SUXYY!-.@AA
 0O 	G&6O 	G &8%?%?
O%\%\"%7%?%?
FSXMM%Z%Z"BT?? CG?4S/=YY89A9F9IK K @@M@PR R rW   c                 B   | j         }t          |           }t          |          s|S t          |          }t	          j        ||          }t          ||t          j        |          }t          | |t          j        |          }t          |t          j        |          }|S )aE  Format a number in national format for dialing using the specified carrier.

    The carrier-code will always be used regardless of whether the phone
    number already has a preferred domestic carrier code stored. If
    carrier_code contains an empty string, returns the number in national
    format without any carrier code.

    Arguments:
    numobj -- The phone number to be formatted
    carrier_code -- The carrier selection code to be used

    Returns the formatted phone number in national format for dialing using
    the carrier as specified in the carrier_code.
    )r   rM  rH  rO  r   rP  rQ  r   r   rR  rN  )r  carrier_coder   r  r6  r  rT  s          rU   (format_national_number_with_carrier_coderd    s     &L
%f
-
-C*<88 

 /|<<K@{[[H"3#+#4#=#/1 1 99A9J9S9IK K @@Q@Z@PR R rW   c                 t    | j          t          | j                   dk    r| j         }n|}t          | |          S )a  Formats a phone number in national format for dialing using the carrier
    as specified in the preferred_domestic_carrier_code field of the
    PhoneNumber object passed in. If that is missing, use the
    fallback_carrier_code passed in instead. If there is no
    preferred_domestic_carrier_code, and the fallback_carrier_code contains an
    empty string, return the number in national format without any carrier
    code.

    Use format_national_number_with_carrier_code instead if the carrier code
    passed in should take precedence over the number's
    preferred_domestic_carrier_code when formatting.

    Arguments:
    numobj -- The phone number to be formatted
    carrier_code -- The carrier selection code to be used, if none is found in the
              phone number itself.

    Returns the formatted phone number in national format for dialing using
    the number's preferred_domestic_carrier_code, or the fallback_carrier_code
    pass in if none is found.
    Nr   )preferred_domestic_carrier_coder   rd  )r  fallback_carrier_coderc  s      rU   2format_national_number_with_preferred_carrier_coderh    sB    4 	.:F233a77=,3FLIIIrW   c                    | j         }t          |          s| j        t          S | j        S t          }t	                      }|                    |            d|_        t          |          }t          |          }|t          j
        k    }||k    r|t          j        k    p|t          j        k    p|t          j        k    }	|dk    r<|	r:|j        *t          |j                  dk    rt!          |d          }nnt          }ne|t"          k    rt%          j        |          }
|
J t)          |          rFt+          t-          |          |
          t.          j        k    rt3          |t4          j                  }nt3          |t4          j                  }n|t:          k    s;|t=          d          k    s&|t=          d          k    s|t=          d          k    r,|	r*t)          |          rt3          |t4          j                  }nbt3          |t4          j                  }nG|rEt)          |          r6|rt3          |t4          j                  S t3          |t4          j                  S |r|S tA          |          S )aY  Returns a number formatted in such a way that it can be dialed from a
     mobile phone in a specific region.

    If the number cannot be reached from the region (e.g. some countries block
    toll-free numbers from being called outside of the country), the method
    returns an empty string.

    Arguments:
    numobj -- The phone number to be formatted
    region_calling_from -- The region where the call is being placed.

    with_formatting -- whether the number should be returned with formatting
              symbols, such as spaces and dashes.

    Returns the formatted phone number.
    NBRr   rJ  MXCLUZ)!r   rH  rL  r
   r   r  r
  rO  r   r   r   r   r   r   rf  r   rh  r   r   r   can_be_internationally_dialled_test_number_lengthrM  r   r   r  r   r   r   r   r   r   r   )r  region_calling_fromwith_formattingrG  rT  numobj_no_extr6  numobj_typeis_valid_numberis_fixed_line_or_mobiler  s              rU    format_number_for_mobile_dialingrv    s   " ".*+?@@ $#!!##%MMMV$$$"M./CDDKm,,K"o&==Ok))$/?3M$M $Y$/?3I$I$Y$/?3W$W 	  $#:
 =IMABBQFF#UVceg#h#h   $2  !%888
 %89LMMH'''.}== \#$?$N$N$,. .1A1KL L#0@Q@_#`#`  #0@Q@Z#[#[  
  >>>&,,..+2M2M&,,..) /.}== /, $1@Q@_#`#`  #0@Q@Z#[#[  	 H;MJJ H  	H 0A0OPPP 0A0FGGG @-.>???rW   c                    t          |          st          | t          j                  S | j        }t          |           }t          |          s|S |t          k    rBt          |          r2t          |          t          z   t          | t          j                  z   S n-|t          |          k    rt          | t          j                  S t          j        ||                                          }|j        }t"          }|j        |j        }nt'          t(          |          r|}t+          |          }t          j        ||                                          }t-          ||t          j                  }	t/          | |t          j        |	          }
t1          |          dk    r&|t          z   t          |          z   t          z   |
z   }
nt3          |t          j        |
          }
|
S )a  Formats a phone number for out-of-country dialing purposes.

    If no region_calling_from is supplied, we format the number in its
    INTERNATIONAL format. If the country calling code is the same as that of
    the region where the number is from, then NATIONAL formatting will be
    applied.

    If the number itself has a country calling code of zero or an otherwise
    invalid country calling code, then we return the number with no formatting
    applied.

    Note this function takes care of the case for calling inside of NANPA and
    between Russia and Kazakhstan (who share the same country calling
    code). In those cases, no international prefix is used. For regions which
    have multiple international prefixes, the number in its INTERNATIONAL
    format will be returned instead.

    Arguments:
    numobj -- The phone number to be formatted
    region_calling_from -- The region where the call is being placed

    Returns the formatted phone number
    Nr   )r4  r  r   r   r   rM  rH  r   is_nanpa_countryr   r   r   country_code_for_valid_regionr   rP  r  international_prefixr
   preferred_international_prefixr   _SINGLE_INTERNATIONAL_PREFIXrO  rQ  rR  r   rN  )r  rp  r   r   metadata_for_region_calling_fromrz  i18n_prefix_for_formattingr6  r   formatted_national_numberrT  s              rU   $format_out_of_country_calling_numberr  i  s&   0 !!455 FV%6%DEEE&L
%f
-
-C*<88 
***/00 	G <((72!&*;*DEEF G	G
 
67JKK	K	K V%6%?@@@ (5'XYegz  hA  hA  hC  hC  (D  (D$;P
 "0'FR%E%d""	/1E	F	F :%9".|<<K'KLZeZkZkZmZmnn +C,?,=,K!M !M 99L9J9X9RT T %&&**6@"<0013:;=MN DLDUDcDTV V rW   c                    | j         t          |           s| j         S | j        t          j        u rt          | t          j                  S t          | |          }| j         }|(|r&t          |          }t          |          }||k    r|}|S )a  Formats a phone number using the original phone number format
    (e.g. INTERNATIONAL or NATIONAL) that the number is parsed from, provided
    that the number has been parsed with parse(.., keep_raw_input=True).
    Otherwise the number will be formatted in NATIONAL format.

    The original format is embedded in the country_code_source field of the
    PhoneNumber object passed in, which is only set when parsing keeps the raw
    input. When we don't have a formatting pattern for the number, the method
    falls back to returning the raw input.

    Note this method guarantees no digit will be inserted, removed or modified
    as a result of formatting.

    Arguments:
    number -- The phone number that needs to be formatted in its original
              number format
    region_calling_from -- The region whose IDD needs to be prefixed if the
              original number has one.

    Returns the formatted phone number in its original number format.
    )
rL  "_has_formatting_pattern_for_numbercountry_code_sourcer   UNSPECIFIEDr  r   r   _format_original_allow_modsr   )r  rp  rT  num_raw_inputnormalized_formatted_numbernormalized_raw_inputs         rU   format_in_original_formatr    s    , 	$-OPV-W-W$ !%6%BBBV%6%?@@@26;NOO$M 	$$&DEU&V&V#=mLL&*>>>,rW   c                    | j         t          j        k    rt          | t          j                  S | j         t          j        k    rt          | |          S | j         t          j        k    r"t          | t          j                  dd          S t          | j
                  }t          |d          }t          | t          j                  }|t          |          dk    r|S t          | j        ||          r|S t!          j        |          }|J t%          |           }t'          |j        |          }||S |j        }||S |                    d          }	|	dk    r|S |d |	         }t/          |          }t          |          dk    r|S t1          |          }
d |
_        t3          | t          j        |
g          S )Nr   Tr   rV  )r  r   FROM_NUMBER_WITH_PLUS_SIGNr  r   r   FROM_NUMBER_WITH_IDDr  FROM_NUMBER_WITHOUT_PLUS_SIGNrO  r   ndd_prefix_for_regionr   r   #_raw_input_contains_national_prefixrL  r   r   rM  rW  r\  r   findr   r   ra  )r  rp  r6  r   national_formatr  rK  format_rulecandidate_national_prefix_ruleindex_of_first_groupnew_format_rules              rU   r  r    s   "&7&RRRV%6%DEEE		#'8'M	M	M3F<OPPP

$(9(W
W
WV%6%DEEabbII263FGG 0TBB'0A0JKK#s?';';q'@'@ #"/0@/S^__ 	#"" !4[AA###5f==;H<RTcdd
 "" *5)T&)1""=BB5II A%%"")GH]I]H])^&)>?])^)^&-..!33""-k:::>7 ):)CoEVWWWrW   c                     t          |           }|                    |          rD	 t          t          |t	          |          d         |                    S # t
          $ r Y dS w xY wdS )zCheck if raw_input, which is assumed to be in the national format, has a
    national prefix. The national prefix is assumed to be in digits-only
    form.NF)r   
startswithrt  parser   NumberParseException)rL  r   r6  nnns       rU   r  r    s~      	
*
*C
~~o&& 		 #5S-A-A-B-B)C[#Q#QRRR# 	 	 	55	5s   1A 
A&%A&c                     | j         }t          |          }t          j        ||          }|dS t	          |           }t          |j        |          }|d uS )NF)r   rO  r   rP  rM  rW  r\  )r  r   phone_number_regionr  rK  r  s         rU   r  r  -  sa    &L6|DD@ObccHu1&99O78NP_``Kd""rW   c                 x   | j         }|t          |          dk    rt          | |          S | j        }t	          |          s|S t          |t          d          }t          |           }t          |          dk    r-|                    |dd                   }|dk    r
||d         }t          j
        |                                d          }|t          k    r*t          |          rt          |          t          z   |z   S n|~|t!          |          k    rkt#          |j        |          }||S t'          |          }t)          d          |_        t)          d          |_        t/          ||t0          j                  S t4          }	|(|j        }
t9          t:          |
          }|r|
}	n|j        }	t?          |          }t          j         ||          }tC          | |t0          j"        |          }|	r&|	t          z   t          |          z   t          z   |z   }ntG          |t0          j"        |          }|S )a  Formats a phone number for out-of-country dialing purposes.

    Note that in this version, if the number was entered originally using
    alpha characters and this version of the number is stored in raw_input,
    this representation of the number will be used rather than the digit
    representation. Grouping information, as specified by characters such as
    "-" and " ", will be retained.

    Caveats:

     - This will not produce good results if the country calling code is both
       present in the raw input _and_ is the start of the national
       number. This is not a problem in the regions which typically use alpha
       numbers.

     - This will also not produce good results if the raw input has any
       grouping information within the first three digits of the national
       number, and if the function needs to strip preceding digits/words in
       the raw input before these digits. Normally people group the first
       three digits together so this is not a huge problem - and will be fixed
       if it proves to be so.

    Arguments:
    numobj -- The phone number that needs to be formatted.
    region_calling_from -- The region where the call is being placed.

    Returns the formatted phone number
    Nr   Tr   r   z	(\d+)(.*)z\1\2)$rL  r   r  r   rH  r   !_ALL_PLUS_NUMBER_GROUPING_SYMBOLSrM  r  r   r   r  r   rx  r   r   country_code_for_regionrW  r\  r   r   r   r   r[  r   r   r
   rz  r   r|  r{  rO  rP  rR  r   rN  )r  rp  r  r   rK  first_national_number_digitr}  r^  
new_formatr~  rz  
i18n_matchr6  r   rT  s                  rU   )format_out_of_country_keeping_alpha_charsr  8  s   : $M M 2 2a 7 73F<OPPP&L*<88 
 &m&G&*, ,M 2&99O
?a&3&8&8!9L&M&M#&",,)*E*F*FGM'4'HI\IbIbIdIdfj'k'k$***/00 	B,'''1MAA	B
*
6
12EFF
F
FBCcCqCRT T%  ();<<
 |__
 gJJ
 ))3):)CE E 	E "0 (3?T;=QRR
 	i)=&&)I)h&.|<<K'KLZeff89L9J9X9FH H " 	V6@"<0013:;=MN DLDUDcDTV V rW   c                     t           }| j        r| j        }|d}|dk    r
t          |z  }|t	          | j                  z  }|S )ap  Gets the national significant number of a phone number.

    Note that a national significant number doesn't contain a national prefix
    or any formatting.

    Arguments:
    numobj -- The PhoneNumber object for which the national significant number
              is needed.

    Returns the national significant number of the PhoneNumber object passed
    in.
    Nr   r   )r
   r   number_of_leading_zerosr   strrK  )r  rK  	num_zeross      rU   rM  rM    sU     %O" 12	Iq==$y0Os61222OrW   c                 2   |t           j        k    rt          t          |           z   |z   S |t           j        k    r"t          t          |           z   t
          z   |z   S |t           j        k    r*t          t          z   t          |           z   t          z   |z   S |S )zFA helper function that is used by format_number and format_by_pattern.)	r   r   
_PLUS_SIGNr   r   r   r   _RFC3966_PREFIXr   )r   rS  rT  s      rU   rN  rN    s    &+++F<0003CCC	(6	6	6F<0007:=MMM	(0	0	0+f\.B.BBVKN^^^rW   c                     |j         }t          |          dk    s|t          j        k    r|j        }n|j         }t          ||           }|| S t          | |||          S )zFormat a national number.r   )intl_number_formatr   r   r   r\  rW  r[  )r   r  rS  rc  intl_number_formatsavailable_formatsr^  s          rU   rQ  rQ    sz     #5
 	  A%%'000$2$7>?PRXYY!(1CZQ]^^^rW   c                    | D ]}t          |j                  }|dk    r4t          j        |j        d                   }|                    |          }|dk    s|r-t          j        |j                  }t          ||          r|c S d S )Nr   r   )r   r   r  compiler   r   r   )r  rK  rS  size
ld_patternld_matchformat_patterns          rU   rW  rW    s    ' 	" 	"
:455!88J$Eb$IJJJ!''88H1999Z
(:;;N99 "!!!!4rW   c                    |j         }t          j        |j                  }t          }|t
          j        k    r_|r]|j        rV|j        }|                    t          |          }t          j
        t          ||d          }t          j
        |||           }nc|j        }|t
          j        k    r6|r4t          j
        t          ||d          }	t          j
        ||	|           }nt          j
        |||           }|t
          j        k    r^t                              |          }
|
r"t          j
        t          t          |d          }t          j
        t          t           |          }|S )Nr   count)r   r  r  r   r
   r   r   r   rX  
_CC_STRINGsub_FIRST_GROUP_PATTERNr   r   _SEPARATOR_PATTERNr   r   )rK  r^  r\  rc  number_format_rulem_rer  cc_format_ruler   first_group_ruler   s              rU   r[  r[    s{    ,2:(011D .*3333@ 	4 ,Q'//
LII
  V$8$2$6*+- - - %'F41C_$U$U!! +=*\'.777+ 8!v&:&E&8,- /  /  / )+t5E(W(W%%(*t5G(Y(Y%)111$$%>?? 	w(*/A>Sltu(v(v(v%$&F+=vG`$a$a!$$rW   c                 6    t          | t          j                  S )a  Gets a valid number for the specified region.

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

    Returns a valid fixed-line number for the specified region. Returns None
    when the metadata does not contain such information, or the region 001 is
    passed in.  For 001 (representing non-geographical numbers), call
    example_number_for_non_geo_entity instead.
    )example_number_for_typer   r   rE  s    rU   r+  r+  &  s     #;0JKKKrW   c                    t          |           sdS t          j        |                                           }|J t	          |t
          j                  }||j        dS |j        }t          |          dz
  }|t          k    rM|d|         }	 t          ||           }t          |          s|S n# t          $ r Y nw xY w|dz  }|t          k    MdS )a  Gets an invalid number for the specified region.

    This is useful for unit-testing purposes, where you want to test what
    will happen with an invalid number. Note that the number that is
    returned will always be able to be parsed and will have the correct
    country code. It may also be a valid *short* number/code for this
    region. Validity checking such numbers is handled with shortnumberinfo.

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


    Returns an invalid number for the specified region. Returns None when an
    unsupported region or the region 001 (Earth) is passed in.
    Nr   )r4  r   r   r  r/  r   r   r+  r   r   r  rt  r  )r6  r  r(  r+  phone_number_lengthnumber_to_trypossibly_valid_numbers          rU   invalid_example_numberr  4  s     !-- t 01B1B1D1DEEH/*DEED|t*2t(N  n--1
!4
4
4&';(;';<	$)-$E$E!"#899 -,,-# 	 	 	 D		
 	q  !4
4
4 4s    B2 2
B?>B?c                    | t          |          S t          |           sdS t          j        |                                           }|J t          ||          }|-|j        &	 t          |j        |           S # t          $ r Y nw xY wdS )a_  Gets a valid number for the specified region and number type.

    If None is given as the region_code, then the returned number object
    may belong to any country.

    Arguments:
    region_code -- The region for which an example number is needed, or None.
    num_type -- The type of number that is needed.

    Returns a valid number for the specified region and type. Returns None
    when the metadata does not contain such information or if an invalid
    region or region 001 was specified.  For 001 (representing
    non-geographical numbers), call example_number_for_non_geo_entity instead.
    N)	!_example_number_anywhere_for_typer4  r   r   r  r/  r+  r  r  )r6  rC  r  r(  s       rU   r  r  t  s     0::: -- t01B1B1D1DEEH(33DD/;	,k:::# 	 	 	D	4s   'A< <
B	B	c                 @   t           D ]}t          ||           }||c S t          D ]u}t          j        |d          }t          ||           }|L|j        E	 t          t          t          |          z   |j        z   t                    c S # t          $ r Y qw xY wvdS )ay  Gets a valid number for the specified number type (it may belong to any country).

    Arguments:
    num_type -- The type of number that is needed.

    Returns a valid number for the specified type. Returns None when the
    metadata does not contain such information. This should only happen when
    no numbers of this type are allocated anywhere in the world anymore.
    N)r   r  r   r   r9  r/  r+  r  r  r   UNKNOWN_REGIONr  )rC  r6  example_numobjrG  r  r(  s         rU   r  r    s     ) " "0hGG%!!!! & !B   ;<PRVWW#Hh77 3 ?Z&1E*F*FFI\\^lmmmmm'    4s   1B
BBc                 .   t          j        | d          }|}|j        |j        |j        |j        |j        |j        |j        fD ]P}	 |;|j	        4t          t          t          |           z   |j	        z   t                    c S A# t          $ r Y Mw xY wdS )a  Gets a valid number for the specified country calling code for a non-geographical entity.

    Arguments:
    country_calling_code -- The country calling code for a non-geographical entity.

    Returns a valid number for the non-geographical entity. Returns None when
    the metadata does not contain such information, or the country calling
    code passed in does not belong to a non-geographical entity.
    N)r   r9  mobile	toll_freeshared_costvoip	voicemailuanpremium_rater+  r  r  r   r  r  )rG  r  r(  s      rU   !example_number_for_non_geo_entityr    s     78LdSSH
 _h&8(:NPXP]'x7LN 	 	D$)<)H f5I.J.J!JTM`!`bpqqqqq'   4s   :B
BBc                     | j         rM|t          j        k    r|t          z   | j         z   S |j        ||j        z   | j         z   S |t
          z   | j         z   S |S )zAppends the formatted extension of a phone number to formatted number,
    if the phone number had an extension specified.
    )r
  r   r   ri   preferred_extn_prefix_DEFAULT_EXTN_PREFIX)r  r  rS  r   s       rU   rR  rR    sk      H*2220063CCC-9 >>AQQQ 44v7GGGMrW   c                    |t           j        k    r| j        S |t           j        k    r| j        S |t           j        k    r| j        S |t           j        k    s|t           j        k    r| j	        S |t           j
        k    r| j        S |t           j        k    r| j        S |t           j        k    r| j        S |t           j        k    r| j        S |t           j        k    r| j        S |t           j        k    r| j        S | j        S )zDReturn the PhoneNumberDesc of the metadata for the given number type)r   r   r  r   r  r   r  r   r   
fixed_liner   r  r   r  r   personal_numberr   pagerr   r  r   r  general_desc)r  rC  s     rU   r/  r/    s    ?///$$	_.	.	.!!	_+	+	+
o0
0
0
o:
:
:""	_0	0	0##	_)	)	)}	_4	4	4''	_*	*	*~	_(	(	(|	_.	.	.!!$$rW   c                     t          |           }t          j        | j        |          }|t          j        S t          |           }t          ||          S )zGets the type of a valid phone number.

    Arguments:
    numobj -- The PhoneNumber object that we want to know the type of.

    Returns the type of the phone number, as a PhoneNumberType value;
    returns PhoneNumberType.UNKNOWN if it is invalid.
    )r   r   rP  r   r   r   rM  _number_type_helper)r  r6  r  rK  s       rU   r   r     sS     )00K@ATVabbH&&1&99O999rW   c                 f   t          | |j                  st          j        S t          | |j                  rt          j        S t          | |j                  rt          j        S t          | |j                  rt          j	        S t          | |j
                  rt          j        S t          | |j                  rt          j        S t          | |j                  rt          j        S t          | |j                  rt          j        S t          | |j                  rt          j        S t          | |j                  r@|j        rt          j        S t          | |j                  rt          j        S t          j        S |j        s!t          | |j                  rt          j        S t          j        S )z8Return the type of the given number against the metadata)_is_number_matching_descr  r   r   r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  "same_mobile_and_fixed_line_patternr   r  r   r   )rK  r  s     rU   r  r    s   #OX5JKK '&&1FGG ,++1CDD )((1EFF +**?? $##1IJJ /..@@ %$$>> #""1CDD )((1DEE *6 	8"77%oxGG 	8"77)) 7 & (/BB&%%""rW   c                     |dS t          |           }|j        }t          |          dk    r||vrdS t          | |d          S )z9Determine if the number matches the given PhoneNumberDescNFr   )r   r&  _match_national_number)rK  number_descactual_lengthpossible_lengthss       rU   r  r  (  s[    
 u((M"2
q  ]:J%J%Ju!/;FFFrW   c                 @    t          |           }t          | |          S )a  Tests whether a phone number matches a valid pattern.

    Note this doesn't verify the number is actually in use, which is
    impossible to tell by just looking at a number itself.  It only verifies
    whether the parsed, canonicalised number is valid: not whether a
    particular series of digits entered by the user is diallable from the
    region provided when parsing. For example, the number +41 (0) 78 927 2696
    can be parsed into a number with country code "41" and national
    significant number "789272696". This is valid, while the original string
    is not diallable.

    Arguments:
    numobj -- The phone number object that we want to validate

    Returns a boolean that indicates whether the number is of a valid pattern.
    )r   is_valid_number_for_region)r  r6  s     rU   rt  rt  6  s!    " )00K%fk:::rW   c                    | j         }|dS t          j        ||                                          }||t          k    r|t          |          k    rdS t          |           }t          ||          t          j	        k    S )a3  Tests whether a phone number is valid for a certain region.

    Note this doesn't verify the number is actually in use, which is
    impossible to tell by just looking at a number itself. If the country
    calling code is not the same as the country calling code for the region,
    this immediately exits with false. After this, the specific number pattern
    rules for the region are examined. This is useful for determining for
    example whether a particular number is valid for Canada, rather than just
    a valid NANPA number.

    Warning: In most cases, you want to use is_valid_number instead. For
    example, this method will mark numbers from British Crown dependencies
    such as the Isle of Man as invalid for the region "GB" (United Kingdom),
    since it has its own region code, "IM", which may be undesirable.

    Arguments:
    numobj -- The phone number object that we want to validate.
    region_code -- The region that we want to validate the phone number for.

    Returns a boolean that indicates whether the number is of a valid pattern.
    NF)
r   r   rP  r  r   ry  rM  r  r   r   )r  r6  r   r  r  s        rU   r  r  K  s    , &Lu@{O`O`ObObccH	6	6	6	6{CC	C	C u
%f
-
-CX../2IIJrW   c                     | j         }t                              |d          }|dS t          |          dk    r|d         S t	          | |          S )a  Returns the region where a phone number is from.

    This could be used for geocoding at the region level. Only guarantees
    correct results for valid, full numbers (not short-codes, or invalid
    numbers).

    Arguments:
    numobj -- The phone number object whose origin we want to know

    Returns the region where the phone number is from, or None if no region
    matches this calling code.

    Nr   r   )r   r   r  r   !_region_code_for_number_from_list)r  r   regionss      rU   r   r   o  sV     &L)--lDAAGt
7||qqz0AAArW   c                 >   t          |           }|D ]}t          j        |                                d          }|,|j        5t          j        |j                  }|                    |          }|r|c S ht          ||          t          j
        k    r|c S dS )z/Find the region in a list that matches a numberN)rM  r   r   r  leading_digitsr  r  r   r  r   r   )r  r  rK  r6  r  leading_digit_rer   s          rU   r  r    s    1&99O  
 !4[5F5F5H5H$OO".!z(*ABB$**?;;E #""""# (;;?VVV W4rW   c                 Z    t                               | d          }|t          S |d         S )a%  Returns the region code that matches a specific country calling code.

    In the case of no region code being found, UNKNOWN_REGION ('ZZ') will be
    returned. In the case of multiple regions, the one designated in the
    metadata as the "main" region for this calling code will be returned.  If
    the country_code entered is valid but doesn't match a specific region
    (such as in the case of non-geographical calling codes like 800) the value
    "001" will be returned (corresponding to the value for World in the UN
    M.49 schema).
    Nr   )r   r  r  r   r  s     rU   rO  rO    s.     *--lDAAGqzrW   c                 D    t                               | d          }|dS |S )zReturns a list with the region codes that match the specific country calling code.

    For non-geographical country calling codes, the region code 001 is
    returned. Also, in the case of no region code being found, an empty
    list is returned.
    NrO   )r   r  r  s     rU   region_codes_for_country_coder    s(     *--lDAAGrrW   c                 B    t          |           sdS t          |           S )a>  Returns the country calling code for a specific region.

    For example, this would be 1 for the United States, and 64 for New
    Zealand.

    Arguments:
    region_code -- The region that we want to get the country calling code for.

    Returns the country calling code for the region denoted by region_code.
    r   )r4  ry  rE  s    rU   r  r    s'     !-- q(555rW   c                     t          j        |                                 d          }|t          d| z            |j        S )ad  Returns the country calling code for a specific region.

    For example, this would be 1 for the United States, and 64 for New
    Zealand.  Assumes the region is already valid.

    Arguments:
    region_code -- The region that we want to get the country calling code for.

    Returns the country calling code for the region denoted by region_code.
    NzInvalid region code %s)r   r   r  	Exceptionr   r5  s     rU   ry  ry    sE     01B1B1D1DdKKH0;>???  rW   c                     | dS t          j        |                                 d          }|dS |j        }|t	          |          dk    rdS |r t          j        t          t          |          }|S )aQ  Returns the national dialling prefix for a specific region.

    For example, this would be 1 for the United States, and 0 for New
    Zealand. Set strip_non_digits to True to strip symbols like "~" (which
    indicates a wait for a dialling tone) from the prefix returned. If no
    national prefix is present, we return None.

    Warning: Do not use this method for do-your-own formatting - for some
    regions, the national dialling prefix is used only for certain types of
    numbers. Use the library's formatting functions to prefix the national
    prefix when required.

    Arguments:
    region_code -- The region that we want to get the dialling prefix for.
    strip_non_digits -- whether to strip non-digits from the national
               dialling prefix.

    Returns the dialling prefix for the region denoted by region_code.
    Nr   )	r   r   r  r   r   r  r  r   r
   )r6  strip_non_digitsr  r   s       rU   r  r    s    ( t01B1B1D1DdKKHt.O#o"6"6!";";t K &./JJrW   c                     | t           v S )zChecks if this region is a NANPA region.

    Returns True if region_code is one of the regions under the North American
    Numbering Plan Administration (NANPA).
    )r   rE  s    rU   rx  rx  	  s     .((rW   c                     t          |           sdS t          |           \  }}t          t          t          |                    S )a  Checks if the number is a valid vanity (alpha) number such as 800
    MICROSOFT. A valid vanity number will start with at least 3 digits and
    will have three or more alpha characters. This does not do region-specific
    checks - to work out if this number is actually valid for a region, it
    should be parsed and methods such as is_possible_number_with_reason() and
    is_valid_number() should be used.

    Arguments:
    number -- the number that needs to be checked

    Returns True if the number is a valid vanity number
    F)r   _maybe_strip_extensionr   r   r   )r   r
  stripped_numbers      rU   is_alpha_numberr  
	  sD     #6** u!7!?!?I	4oFFGGGrW   c                 `    t          |           }|t          j        k    p|t          j        k    S )a  Convenience wrapper around is_possible_number_with_reason.

    Instead of returning the reason for failure, this method returns true if
    the number is either a possible fully-qualified number (containing the area
    code and country code), or if the number could be a possible local number
    (with a country code, but missing an area code). Local numbers are
    considered possible if they could be possibly dialled in this format: if
    the area code is needed for a call to connect, the number is not considered
    possible without it.

    Arguments:
    numobj -- the number object that needs to be checked

    Returns True if the number is possible

    )is_possible_number_with_reasonr   r   r   )r  results     rU   is_possible_numberr  	  s3    " ,F33F&22 >&==?rW   c                 b    t          | |          }|t          j        k    p|t          j        k    S )a  Convenience wrapper around is_possible_number_for_type_with_reason.

    Instead of returning the reason for failure, this method returns true if
    the number is either a possible fully-qualified number (containing the area
    code and country code), or if the number could be a possible local number
    (with a country code, but missing an area code). Local numbers are
    considered possible if they could be possibly dialled in this format: if
    the area code is needed for a call to connect, the number is not considered
    possible without it.

    Arguments:
    numobj -- the number object that needs to be checked
    numtype -- the type we are interested in

    Returns True if the number is possible

    )'is_possible_number_for_type_with_reasonr   r   r   )r  r1  r   s      rU   is_possible_number_for_typer  4	  s5    $ 5VWEEF&22 >&==?rW   c                    t          ||          }||j        j        }d}n-|j        }t          |          dk    r|j        j        }|j        }|t
          j        k    rt          t          |t
          j                            st          | |t
          j
                  S t          |t
          j
                  }t          |          rt          |          }t          |j                  dk    r||j        j        z  }n
||j        z  }t                              |           t          |          dk    r|j        }n3t          |          }||j        z  }t                              |           |t          j        S t          |           }||v rt          j        S |d         }||k    rt          j        S ||k    rt          j        S |d         |k     rt          j        S ||dd         v rt          j        S t          j        S )zHelper method to check a number against possible lengths for this number,
    and determine whether it matches, or is too short or too long.
    NrO   r   r   r   )r/  r  r&  r   possible_length_local_onlyr   r   r)  r   ro  r   r   sortr   r   r   r   r   r   )	rK  r  r1  desc_for_typer  local_lengthsmobile_descr  minimum_lengths	            rU   ro  ro  K	  s    )7;;M#0@ )8  A%%'4D%@/666-.B8_Mg.h.hii 	- '/BXYYY.x9OPPK-k:: -#'(8#9#9  {233q88$(=(MM$$$(CC$ 		*+++}%%**$/$JMM$($7$7M![%KKMIIm,,, ..((M %%66%a(N&&++	-	'	'))	"		-	-(((,,,++..rW   c                 6    t          | t          j                  S r   )r  r   r   r?  s    rU   r  r  	  s    26?;RSSSrW   c                     t          |           }| j        }t          |          st          j        S t          |          }t          j        ||          }t          |||          S )a  Check whether a phone number is a possible number of a particular type.

    For types that don't exist in a particular region, this will return a result
    that isn't so useful; it is recommended that you use
    supported_types_for_region or supported_types_for_non_geo_entity
    respectively before calling this method to determine whether you should call
    it for this number at all.

    This provides a more lenient check than is_valid_number in the following sense:

     - It only checks the length of phone numbers. In particular, it doesn't
       check starting digits of the number.

     - For some numbers (particularly fixed-line), many regions have the
       concept of area code, which together with subscriber number constitute
       the national significant number. It is sometimes okay to dial only the
       subscriber number when dialing in the same area. This function will
       return IS_POSSIBLE_LOCAL_ONLY if the subscriber-number-only version is
       passed in. On the other hand, because is_valid_number validates using
       information on both starting digits (for fixed line numbers, that would
       most likely be area codes) and length (obviously includes the length of
       area codes for fixed line numbers), it will return false for the
       subscriber-number-only version.

    Arguments:
    numobj -- The number object that needs to be checked
    numtype -- The type we are interested in

    Returns a value from ValidationResult which indicates whether the number
    is possible
    )	rM  r   rH  r   r   rO  r   rP  ro  )r  r1  rK  r   r6  r  s         rU   r  r  	  sf    @ 2&99O&L +<88 544.|<<K@{[[H'BBBrW   c                 `    	 t          t          | |                    S # t          $ r Y dS w xY w)a  Check whether a phone number string is a possible number.

    Takes a number in the form of a string, and the region where the number
    could be dialed from. It provides a more lenient check than
    is_valid_number; see is_possible_number_with_reason() for details.

    This method first parses the number, then invokes is_possible_number with
    the resultant PhoneNumber object.

    Arguments:
    number -- The number that needs to be checked, in the form of a string.
    region_dialling_from -- The region that we are expecting the number to be
              dialed from.  Note this is different from the region where the
              number belongs.  For example, the number +1 650 253 0000 is a
              number that belongs to US. When written in this form, it can be
              dialed from any region. When it is written as 00 1 650 253 0000,
              it can be dialed from any region which uses an international
              dialling prefix of 00. When it is written as 650 253 0000, it
              can only be dialed from within the US, and when written as 253
              0000, it can only be dialed from within a smaller area in the US
              (Mountain View, CA, to be more specific).

    Returns True if the number is possible
    F)r  r  r  )r   region_dialing_froms     rU   is_possible_number_stringr  	  sB    2!%0C"D"DEEE   uus    
--c                 ,   t          |           rdS t                      }|                    |            | j        }t          |          sB|dz  }||_        t	          |          }|t
          j        k    s|dk    rdS t          |          B|| _        dS )a  Truncate a number object that is too long.

    Attempts to extract a valid number from a phone number that is too long
    to be valid, and resets the PhoneNumber object passed in to that valid
    version. If no valid number could be extracted, the PhoneNumber object
    passed in will not be modified.

    Arguments:
    numobj -- A PhoneNumber object which contains a number that is too long to
              be valid.

    Returns True if a valid phone number can be successfully extracted.
    Tr   r   F)rt  r   r  rK  r  r   r   )r  numobj_copyrK  validation_results       rU   truncate_too_long_numberr  	  s     v t--K6""",Ok** )R/&5#:;GG!1!;;;q  5 k**  -F4rW   c                 D   t          |           dk    s| d         t          k    rd| fS t          dt          t          |           t                    dz             D ]A}	 t          | d|                   }|t          v r|| |d         fc S 2# t          $ r Y >w xY wd| fS )a  Extracts country calling code from number.

    Returns a 2-tuple of (country_calling_code, rest_of_number).  It assumes
    that the leading plus sign or IDD has already been removed.  Returns (0,
    number) if number doesn't start with a valid country calling code.
    r   r   N)r   r   r   min_MAX_LENGTH_COUNTRY_CODEintr   r  )r   r   r   s      rU   _extract_country_coder  	
  s     6{{a6!9..6{As3v;;(@AAAEFF  	vcrc{++L:::$fRSSk2222 ; 	 	 	D	v;s   +B
BBc                 Z   t          |           dk    r	dt          fS | }t          d          }||j        |j        }t	          ||          \  }}|r||_        |t          j        k    rot          |          t          k    rt          t          j
        d          t          |          \  }}|dk    r||_        ||fS t          t          j        d          ||j        }	t          |j                  }
|}|                    |
          r|t          |
          d         }|j        }t#          ||          \  }}}t%          ||d          st%          ||d          st'          ||          t(          j        k    r|rt          j        |_        |	|_        |	|fS d|_        dt          fS )a  Tries to extract a country calling code from a number.

    This method will return zero if no country calling code is considered to
    be present. Country calling codes are extracted in the following ways:

     - by stripping the international dialing prefix of the region the person
       is dialing from, if this is present in the number, and looking at the
       next digits

     - by stripping the '+' sign if present and then looking at the next
       digits

     - by comparing the start of the number and the country calling code of
       the default region.  If the number is not considered possible for the
       numbering plan of the default region initially, but starts with the
       country calling code of this region, validation will be reattempted
       after stripping this country calling code. If this number is considered
       a possible number, then the first digits will be considered the country
       calling code and removed as such.

    It will raise a NumberParseException if the number starts with a '+' but
    the country calling code supplied after this does not match that of any
    known region.

    Arguments:
    number -- non-normalized telephone number that we wish to extract a
              country calling code from; may begin with '+'
    metadata -- metadata about the region this number may be from, or None
    keep_raw_input -- True if the country_code_source and
              preferred_carrier_code fields of numobj should be populated.
    numobj -- The PhoneNumber object where the country_code and
              country_code_source need to be populated. Note the country_code
              is always populated, whereas country_code_source is only
              populated when keep_raw_input is True.

    Returns a 2-tuple containing:
      - the country calling code extracted or 0 if none could be extracted
      - a string holding the national significant number, in the case
        that a country calling code was extracted. If no country calling code
        was extracted, this will be empty.
    r   NonMatchNzXPhone number had an IDD, but after this was not long enough to be a viable phone number.z1Country calling code supplied was not recognised.F)r   r
   r   rz  &_maybe_strip_i18n_prefix_and_normalizer  r   FROM_DEFAULT_COUNTRYr   r  TOO_SHORT_AFTER_IDDr  r   r   r  r  r  )_maybe_strip_national_prefix_carrier_coder  ro  r   r   r  )r   r  keep_raw_inputr  full_numberpossible_country_idd_prefixr  potential_country_coderest_of_numberdefault_country_codedefault_country_code_strr  potential_national_numberr  _s                  rU   _maybe_extract_country_coder)  
  s   T 6{{a>""K"("4"4 = I&.&C#'MkNi(k (k$ 9%8"/DDD{222&';'O(RS S S 2G{1S1S.!Q&&"8F*N;; ##7#L#VX X 	X		  (4#&x'<#=#= '''(@AA 	I(3C8P4Q4Q4R4R(S%#0L.WXqX`/b /b+A(! ,KuMM I'(A<QVWWI$[(;;?O?XXX! a1B1`F.&:#,.GHH F~rW   c                 "   |                      |          }|ru|                                }t                              ||d                   }|r1t	          |                    d                    }|t          k    rd|fS d||d         fS d|fS )zStrips the IDD from the start of the number if present.

    Helper function used by _maybe_strip_i18n_prefix_and_normalize().

    Returns a 2-tuple:
      - Boolean indicating if IDD was stripped
      - Number with IDD stripped
    Nr   FT)r   end_CAPTURING_DIGIT_PATTERNr   r   groupr   )idd_patternr   r   	match_enddigit_matchnormalized_groups         rU   _parse_prefix_as_iddr2  
  s     f%%E 	*IIKK	 /55fYZZ6HII 	'4[5F5Fq5I5IJJ6))v&fYZZ())6?rW   c                    t          |           dk    rt          j        | fS t                              |           }|r7| |                                d         } t          j        t          |           fS t          j	        |          }t          |           } t          ||           \  }} |rt          j        | fS t          j        | fS )a  Strips any international prefix (such as +, 00, 011) present in the
    number provided, normalizes the resulting number, and indicates if an
    international prefix was present.

    Arguments:
    number -- The non-normalized telephone number that we wish to strip any international
              dialing prefix from.
    possible_idd_prefix -- The international direct dialing prefix from the region we
              think this number may be dialed in.

    Returns a 2-tuple containing:
      - The corresponding CountryCodeSource if an international dialing prefix
        could be removed from the number, otherwise
        CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did not seem to
        be in international format.
      - The number with the prefix stripped.
    r   N)r   r   r  _PLUS_CHARS_PATTERNr   r+  r  r   r  r  r2  r  )r   possible_idd_prefixr   r.  strippeds        rU   r  r  
  s    $ 6{{a!6??!!&))A $! "<6""$ 	$ *011KF+K@@Hf @!6??!6??rW   c                 |   t           }|j        }t          |           dk    s|t          |          dk    r
t           | dfS t          j        |          }|                    |           }|rL|j        }t          | |d          }t          |                                          }|j	        }	|	0t          |	          dk    s|                                |dz
           t          | |
                                d         |d          }
|r|
s
t           | dfS |dk    r*|                    |          |                    d          }|| |
                                d         dfS t          j        ||	| d          }t          ||d          }
|r|
sd| dfS |dk    r|                    d          }||dfS || dfS )a  Strips any national prefix (such as 0, 1) present in a number.

    Arguments:
    number -- The normalized telephone number that we wish to strip any
              national dialing prefix from
    metadata -- The metadata for the region that we think this number
              is from.

    Returns a 3-tuple of
     - The carrier code extracted if it is present, otherwise an empty string.
     - The number with the prefix stripped.
     - Boolean indicating if a national prefix or carrier code (or both) could be extracted.
     r   NFr   Tr  rJ  )r
   national_prefix_for_parsingr   r  r  r   r  r  groupsnational_prefix_transform_ruler+  r-  r  )r   r  rc  possible_national_prefixprefix_patternprefix_matchr  is_viable_original_number
num_groupstransform_rulenational_number_matchtransformed_numbers               rU   r  r  
  s    "L'CFq ($%%**.. Z 899N!''//L "-,$:6<QV$W$W! ,,..//
!@"1$$!!*q.19 %;6,BRBRBTBTBUBU;VXdfk$l$l!) 72G 7&66Q##J//;+11!44 &)9)9););)<)<"=tDD
 "$VW!X!X!X$:;M|]b$c$c!) +2G +FE**A~~+11!44 "4d;;fe,,rW   c                    t                               |           }|rbt          | d|                                                   r9|                                D ]$}| || d|                                         fc S %d| fS )a  Strip extension from the end of a number string.

    Strips any extension (as in, the part of the number dialled after the
    call is connected, usually indicated with extn, ext, x or similar) from
    the end of the number, and returns it.

    Arguments:
    number -- the non-normalized telephone number that we wish to strip the extension from.

    Returns a 2-tuple of:
     - the phone extension (or "" or not present)
     - the number before the extension.
    NrJ  )_EXTN_PATTERNr   r   r   r9  )r   r   r-  s      rU   r  r  
  s       ((E  7()?@@ 7\\^^ 	7 	7E  vnu{{}}n56666 !<rW   c                     t          |          s5| t          |           dk    rdS t                              |           }|dS dS )a  Checks to see that the region code used is valid, or if it is not
    valid, that the number to parse starts with a + symbol so that we can
    attempt to infer the region from the number.  Returns False if it cannot
    use the region provided and the region cannot be inferred.
    Nr   FT)r4  r   r4  r   )r   default_regionr   s      rU   _check_region_for_parsingrG    sP     !00 >S[[A--5#))&11=54rW   c                 ,   t          |           dk    r|| d         t          k    rmd|_        d}|t          |           dz
  k     r=| |         t          k    r,|dz  }|t          |           dz
  k     r| |         t          k    ,|dk    r||_        dS dS dS dS )zRA helper function to set the values related to leading zeros in a
    PhoneNumber.r   r   TN)r   r   r   r  )rK  r  r  s      rU   +_set_italian_leading_zeros_for_phone_numberrI  )  s     ?aOA$6&$@$@&*#"# '_)=)=)AAA676AA#q(# '_)=)=)AAA676AA"a''-DF***  $@$@ ('rW   c                    |t                      }| t          t          j        d          t          |           t          k    rt          t          j        d          t          |           }t          |          st          t          j        d          |r*t          ||          st          t          j	        d          |r| |_
        t          |          \  }}t          |          dk    r||_        |d}n't          j        |                                d          }d}	 t!          ||||          \  }}	n# t          $ r t#          j                    \  }
}}
t&                              |          }|j        t          j	        k    rR|Pt!          ||                                d         |||          \  }}	|dk    rt          t          j	        d          n Y nw xY w|dk    r/t/          |          }||k    rt          j        ||          }|J n6|	t3          |          z  }	||j        }||_        n|rt6          j        |_        t          |	          t<          k     rt          t          j        d          |k|	}tA          ||          \  }}}
tC          ||          }|tD          j#        tD          j$        tD          j%        fvr |}	|r|t          |          dk    r||_&        t          |	          }|t<          k     rt          t          j        d          |tN          k    rt          t          j        d	          tQ          |	|           tS          |	          |_*        |S )
a  Parse a string and return a corresponding PhoneNumber object.

    The method is quite lenient and looks for a number in the input text
    (raw input) and does not check whether the string is definitely only a
    phone number. To do this, it ignores punctuation and white-space, as
    well as any text before the number (e.g. a leading "Tel: ") and trims
    the non-number bits.  It will accept a number in any format (E164,
    national, international etc), assuming it can be interpreted with the
    defaultRegion supplied. It also attempts to convert any alpha characters
    into digits if it thinks this is a vanity number of the type "1800
    MICROSOFT".

    This method will throw a NumberParseException if the number is not
    considered to be a possible number. Note that validation of whether the
    number is actually a valid number for a particular region is not
    performed. This can be done separately with is_valid_number.

    Note this method canonicalizes the phone number such that different
    representations can be easily compared, no matter what form it was
    originally entered in (e.g. national, international). If you want to
    record context about the number being parsed, such as the raw input that
    was entered, how the country code was derived etc. then ensure
    keep_raw_input is set.

    Note if any new field is added to this method that should always be filled
    in, even when keep_raw_input is False, it should also be handled in the
    _copy_core_fields_only() function.

    Arguments:
    number -- The number that we are attempting to parse. This can
              contain formatting such as +, ( and -, as well as a phone
              number extension. It can also be provided in RFC3966 format.
    region -- The region that we are expecting the number to be from. This
              is only used if the number being parsed is not written in
              international format. The country_code for the number in
              this case would be stored as that of the default region
              supplied. If the number is guaranteed to start with a '+'
              followed by the country calling code, then None or
              UNKNOWN_REGION can be supplied.
    keep_raw_input -- Whether to populate the raw_input field of the
              PhoneNumber object with number (as well as the
              country_code_source field).
    numobj -- An optional existing PhoneNumber object to receive the
              parsing results
    _check_region -- Whether to check the supplied region parameter;
              should always be True for external callers.

    Returns a PhoneNumber object filled with the parse number.

    Raises:
    NumberParseException if the string is not considered to be a viable
    phone number (e.g.  too few or too many digits) or if no default
    region was supplied and the number is not in international format
    (does not start with +).

    Nz#The phone number supplied was None.z*The string supplied was too long to parse.z6The string supplied did not seem to be a phone number.z"Missing or invalid default region.r   z,Could not interpret numbers after plus-sign.z6The string supplied is too short to be a phone number.z5The string supplied is too long to be a phone number.)+r   r  r   r   _MAX_INPUT_STRING_LENGTHr   "_build_national_number_for_parsingr   rG  r   rL  r  r
  r   r   r  r)  sysexc_infor4  r   
error_typer+  rO  rP  r   r   r   r  r  r   TOO_SHORT_NSNr  ro  r   r   r   r   rf  _MAX_LENGTH_FOR_NSNrI  r	   rK  )r   regionr   r  _check_regionrK  r
  r  r   normalized_national_numberr(  ematchobjnumber_regionr'  rc  r  len_national_numbers                     rU   r  r  8  s   t ~~"#7#D#HJ J 	J	V/	/	/"#7#@#OQ Q 	Q 9@@O"?33 ]"#7#D#[] ] 	]
  I6OO I"#7#L#GI I 	I "! "8!H!HI
9~~$~ 4V\\^^TJJL3NOWO]OU4W 4W000     ,..1a&,,_==L0EEE 7RSbckcococqcqcrcrSsS[SaSY8[ 8[4L4 q  *+?+T+Y[ [ [ ! 	 !   q4\BBF""$HWdeeH'''
 	#j&A&AA"#0L".F 	G):)FF&
%&&)<<<"#7#E#[] ] 	]$>!5^_x_g6i 6i2/ 00I8TT%5%?%5%L%5%D%F F F *C& F,":s<?P?PST?T?T9E6899000 ##7#E#[] ] 	]000"#7#@#Z\ \ 	\/0JFSSS$%?@@FMs   'D= =B#G#"G#c                     |dk    rdS |t          t                    z   }|t          |           k    rt          S |                     d|          }|dk    r
| ||         S | |d         S )a.  Extracts the value of the phone-context parameter of number_to_extract_from where the index of
    ";phone-context=" is the parameter index_of_phone_context, following the syntax defined in
    RFC3966.

    Returns the extracted string (possibly empty), or None if no phone-context parameter is found.r   N;)r   _RFC3966_PHONE_CONTEXTr
   r  )number_to_extract_fromindex_of_phone_contextphone_context_startphone_context_ends       rU   _extract_phone_contextr`    s     ##t037M3N3NNc"89999.33C9LMMB%&9:K&KLL%&9&:&:;;rW   c                     | dS t          |           dk    rdS t          t          |           pt          t          |           S )zQ"Returns whether the value of phoneContext follows the syntax defined in RFC3966.NTr   F)r   r   %_RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_RFC3966_DOMAINNAME_PATTERN)phone_contexts    rU   _is_phone_context_validre    sO    t
=Qu ;]KK B1=AACrW   c                    |                      t                    }t          | |          }t          |          st	          t          j        d          |b|d         t          k    r|}nt          }|                      t                    }|dk    r|t          t                    z   nd}|| ||         z  }nt          |           }|                     t                    }|dk    r
|d|         }|S )zConverts number to a form that we can parse and return it if it is
    written in RFC3966; otherwise extract a possible number out of it and return it.z"The phone-context value is invalidNr   )r  r[  r`  re  r  r   r  r
   r  r   r   _RFC3966_ISDN_SUBADDRESS)r   r]  rd  rK  index_of_rfc3996_prefixindex_of_national_numberindex_of_isdns          rU   rL  rL    s    $[[)?@@*63IJJM"=11 l"#7#DFjkkk  z)) ,OO,O #)++o">">(?1(D(D &=s??S?S%S%SKL 	!6":;Q"QRR 36::
 $(()ABBMq).=.9 rW   c                     t                      }| j        |_        | j        |_        | j        $t	          | j                  dk    r| j        |_        | j        r!d|_        | j        |_        |j        d|_        |S )zReturns a new phone number containing only the fields needed to uniquely
    identify a phone number, rather than any fields that capture the context in
    which the phone number was created.
    Nr   Tr   )r   r   rK  r
  r   r   r  )inobjr  s     rU   _copy_core_fields_onlyrm  ,  s}    
 ]]F,F"2F"s5?';';a'?'? ?! /&*#).)F&)1-.F*MrW   c                    t          |           }t          |          }|j        #|j        |j        |j        k    rt          j        S |j        }|j        }|dk    rF|dk    r@||k    rt          j        S ||k    rt          ||          rt          j        S t          j        S ||_        ||k    rt          j        S t          ||          rt          j        S t          j        S )z>Takes two phone number objects and compares them for equality.Nr   )	rm  r
  r   r   r   r   #_is_national_number_suffix_of_otherr   r   )
numobj1_in
numobj2_innumobj1numobj2country_code1country_code2s         rU   _is_number_match_OOrv  @  s     %Z00G$Z00G 	%%W...!!(M(Mmq00g((},,1'7CC - ,,!!
 )G'""*7G<< )((rW   c                     t          | j                  }t          |j                  }|                    |          p|                    |          S )z_Returns true when one national number is the suffix of the other or both
    are the same.
    )r  rK  endswith)rr  rs  nn1nn2s       rU   ro  ro  h  sG     g%
&
&C
g%
&
&C<<1S 1 11rW   c           	      `   	 t          | t                    }t          ||          S # t          $ r t	          j                    \  }}}|j        t          j        k    r	 t          |t                    }t          ||           cY S # t          $ r t	          j                    \  }}}|j        t          j        k    r]	 t          | dddd          }t          |dddd          }t          ||          cY cY S # t          $ r t          j
        cY cY cY S w xY wY nw xY wY nw xY wt          j
        S )zTakes two phone numbers as strings and compares them for equality.

    This is a convenience wrapper for _is_number_match_OO/_is_number_match_OS.
    No default region is known.
    NFr   rS  r  )r  r  _is_number_match_OSr  rM  rN  rO  r   rv  r   r   )number1number2rr  r(  excrs  exc2s          rU   _is_number_match_SSr  r  s{   600"7G444 6 6 6LNN	3>1FFF688*7G<<<<<' 
6 
6 
6 \^^
4?&:&OOO6"'e6;D#J #J #J"'e6;D#J #J #J27GDDDDDDD/ 6 6 6(555555556 PO
66( !!sc   $' 6D!$BD!6D<7C83D4D!8DDD!DDD!DD! D!c                 *   	 t          |t                    }t          | |          S # t          $ r t	          j                    \  }}}|j        t          j        k    rt          | j	                  }	 |t          k    rBt          ||          }t          | |          }|t          j        k    rt          j        cY S |cY S t          |dddd          }t          | |          cY S # t          $ r t          j        cY cY S w xY wY nw xY wt          j        S )zaWrapper variant of _is_number_match_OO that copes with one
    PhoneNumber object and one string.NFr|  )r  r  rv  r  rM  rN  rO  r   rO  r   r   r   r   r   )rr  r  rs  r(  r  region1r   s          rU   r}  r}    sG   
.00"7G444 . . .LNN	3>1FFF
 373GHHG.n,,#GW55G/AAE	 555(2222$
 $GT%27F F FG.w@@@@@' . . . ------.+ GF.6 !!sM   $' A
D2AC%8D;C%<D?#C%"D%D ;D?D  DDc                 *   t          | t                    r%t          |t                    rt          | |          S t          | t                    rt          | |          S t          |t                    rt          ||           S t	          | |          S )aC  Takes two phone numbers and compares them for equality.

    For example, the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH.
    The numbers +1 345 657 1234 and 345 657 are a NO_MATCH.

    Arguments
    num1 -- First number object or string to compare. Can contain formatting,
              and can have country calling code specified with + at the start.
    num2 -- Second number object or string to compare. Can contain formatting,
              and can have country calling code specified with + at the start.

    Returns:
     - EXACT_MATCH if the country_code, NSN, presence of a leading zero for
       Italian numbers and any extension present are the same.
     - NSN_MATCH if either or both has no region specified, and the NSNs and
       extensions are the same.
     - SHORT_NSN_MATCH if either or both has no region specified, or the
       region specified is the same, and one NSN could be a shorter version of
       the other number. This includes the case where one has an extension
       specified, and the other does not.
     - NO_MATCH otherwise.
     )
isinstancer   rv  r}  r  )num1num2s     rU   is_number_matchr    s    . $$$ /D+)F)F /"4...	D+	&	& /"4...	D+	&	& /"4..."4...rW   c                     t          j        t          |           d          }|dS t          |           }t	          ||j                   S )a  Returns True if the number can only be dialled from outside the region,
    or unknown.

    If the number can only be dialled from within the region
    as well, returns False. Does not check the number is a valid number.
    Note that, at the moment, this method does not handle short numbers (which
    are currently all presumed to not be diallable from outside their country).

    Arguments:
    numobj -- the phone number objectfor which we want to know whether it is
              diallable from outside the region.
    NT)r   r   r   rM  r  no_international_dialling)r  r  r  s      rU   rn  rn    sP     01G1O1OQUVVH t
%f
-
-C'X-OPPPPrW   c                 B    t          j        | d          }|dS |j        S )aA  Returns true if the supplied region supports mobile number portability.
    Returns false for invalid, unknown or regions that don't support mobile
    number portability.

    Arguments:
    region_code -- the region for which we want to know whether it supports mobile number
                   portability or not.
    NF)r   r   mobile_number_portable_regionr5  s     rU    is_mobile_number_portable_regionr    s*     0dCCHu11rW   c                   8    e Zd ZdZdZdZdZdZdZd Z	d Z
d	 Zd
S )r  z:Exception when attempting to parse a putative phone numberr   r   r   r   r   c                 X    t                               | |           || _        || _        d S r   )r  __init__rO  _msg)selfrO  msgs      rU   r  zNumberParseException.__init__   s*    4%%%$			rW   c                 <    t          |           | j        | j        ffS r   )typerO  r  r  s    rU   
__reduce__zNumberParseException.__reduce__%  s    T

T_di899rW   c                 >    t          d          | j        | j        fz  S )Nz(%s) %s)r   rO  r  r  s    rU   __unicode__z NumberParseException.__unicode__(  s    i  DOTY#???rW   N)r   r   r   r   r   r   r  rP  r   r  r  r  rO   rW   rU   r  r    sq        DD  L
  M H  
: : :@ @ @ @ @rW   r  c                     ||j         t          |j                   dk    rdS t          | t          j        |j                   |          S )zReturns whether the given national number (a string containing only decimal digits) matches
    the national number pattern defined in the given PhoneNumberDesc object.
    Nr   F)r,  r   _matchr  r  )r   r  allow_prefix_matchs      rU   r  r  ,  sN     kAISQ\QtMuMuyzMzMzu&"*[%HIIK]^^^rW   c                 X    |                     |           sdS t          ||           rdS |S )NFT)r   r   )r   r   r  s      rU   r  r  7  s8    ==   &uWf%% 	&4%%rW   )Fr   )NFNT)r   rM  r  re_utilr   utilr   r   r   r   r	   r
   r   r   r   r   r   unicode_utilr   r   phonenumberr   r   phonemetadatar   r   r   datar   ImportErrorospathbasenameargvstderrr   UNICODE
IGNORECASE_REGEX_FLAGSr   rQ  r  rK  r  r   r  	frozensetr  r  r"  rB  r  
_STAR_SIGNri   r  r[  rg  _ASCII_DIGITS_MAP_ALPHA_MAPPINGSdictr   r   r#  r  r  r|  _VALID_PUNCTUATIONra   r  _VALID_ALPHA_PLUS_CHARSr4  r  r,  _VALID_START_CHARr   _SECOND_NUMBER_STARTr   _UNWANTED_END_CHARSr   r   _VALID_PHONE_NUMBERr  rc   r~   _EXTN_PATTERNS_FOR_PARSING_EXTN_PATTERNS_FOR_MATCHING_RFC3966_VISUAL_SEPARATOR_RFC3966_PHONE_DIGIT_RFC3966_GLOBAL_NUMBER_DIGITSrb  	_ALPHANUM_RFC3966_DOMAINLABEL_RFC3966_TOPLABEL_RFC3966_DOMAINNAMErc  rD  r   r  r  rY  rZ  r  r<  objectr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r$  r)  r-  r2  r7  r:  r=  r@  r  r4  rH  r  ra  rd  rh  rv  r  r  r  r  r  r  rM  rN  rQ  rW  r[  r+  r  r  r  r  rR  r/  r   r  r  rt  r  r   r  rO  r  r  ry  r  rx  r  r  r  r   ro  r  r  r  r  r  r)  r2  r  r  r  rG  rI  r  r`  re  rL  rm  rv  ro  r  r}  r  rn  r  r  r  r  r  rO   rW   rU   <module>r     so  
 
4 


 				       8 8 8 8 8 8 8 8 8 8 8 8 8 8 O O O O O O O O O O O O O O O O 0 0 0 0 0 0 8 7 7 7 7 7 7 7 V V V V V V V V V V2222222 
 
 
 III
!%%)BBB
!%%)===PWZWabbbb()7|$$ 	%$
 ;  zBM)      4 
 aaff  3<) =	 3
 3
 / 6?Y @	 6
 6
 2 H## O K	 K	 	  QsVV
QsVV
qzz !F)),-- 1X;;  QsVVQQsVVQQsVVQQsVVQsVVQQsVVQQsVVQQsVVQsVVQQsVVQQsVVQQsVVQsVVQQsVVQQsVVQQsVVQsVVQQsVVQQsVVQQsVV	5 %11S6611S66 %1S6611S66%1S6611S66% 1S6611S66% 1S6611S66	%
 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66% 1S6611S66%  1S6611S66!%" 1S6611S661S6611S661S6611S661S6611S661S6611S661S6611S661S6611S661S6611S661S6611S663% %6 _BB0ABB 
  4Z!"33!"33!1 5 5 #45 5  %)D *?!!C&&!!C&& *?*+!H++qqvv*?*+!H++qqvv*? +,!H++qqvv*? +,!H++qqvv	*?
 +,!H++qqvv*? +,!H++qqvv*? +,!H++qqvv*? +,!H++qqvv*? +,!C&&!!C&&*? +,!H++qqvv*? +,!C&&!!C&&*? +,!H++qqvv*? +,!H++qqvv*? +,!C&&!!C&&*? +,!H++qqvv*? %F %F$ ,040c0cOL`L`LbLb0c0c0c0[0[ODXDXDZDZ0[0[0[1\ ,E ,E2C,E ,E%%F %F !8  *rz!!,T*U*UVV  aCDDamnno  &-- $#O$8$8$:$:;;##$Q$Q/:N:N:P:P$Q$Q$QRRSa	ll bj3+!5$!?@@ RZ#); ;aagg EFF %2:aaffw&63&?LL  AcFF[(72QQsVV; &BJ'8"*EE  q )rz*>?? & a-.. 'RZ(;<< 
 (RZ*@(A(ABB & !!F)).A"ABQQsVVKqvv +,./akk:<NOQ[\^_^_`d^e^efhoprsrst|r}r}~)*,679EFHOPRSRSTXRYRYZ  q{{ 
: : :E E EV 21$77 22599  . W}s*-FFL  %
 25I IC ORY Y\p psw w (2
3P(Q(Q % 7"	Y5	AEI ,&5	AEI 11G;>OORYY (bj)<==  
11U88&@@11T77JLYY )bj)<qqxx)GJd)dghghimgngn)np|}}   RZ*..  "rz!!I,,//  



 $.2:o#>#>  #+ #+ #+ #+ #+ #+ #+ #+LJ+ J+ J+ J+ J+f J+ J+ J+Z +  +  +  +  +  +  +  +F'+ '+ '+ '+ '+v '+ '+ '+V CEE $'CEE !L L L     	 	 	! ! !H  *- - -8   0E E EC C CB7 B7 B7JA! A! A!H	D 	D 	D2 2 24	3 	3 	3K K K
8 
8 
8
 
 
3 3 3 3 3 3 < < <Q Q Q2 2 2. . .A A A4F 4F 4Fn> > >B% % %PJ J JDj@ j@ j@ZN N Nb' ' 'T4X 4X 4Xn  $# # #o o od  4	  	  	 _ _ _ _2   ,0,% ,% ,% ,%^L L L= = =@  >  8  4  % % %6: : :"!# !# !#HG G G; ; ;*!K !K !KHB B B2  *  $  6 6 6 ! ! !"     F) ) )H H H(? ? ?,? ? ?. <K;R E/ E/ E/ E/PT T T/C /C /Cd  >  B  *` ` `F  .$@ $@ $@N;- ;- ;-|  8   E E E /4%)_ _ _ _D< < <.	C 	C 	C, , ,^  (% % %P2 2 2" " "@#" #" #"L/ / /BQ Q Q.2 2 2*@ *@ *@ *@ *@< *@ *@ *@Z_ _ _& & & & &s   A A3CC