
    b9                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
	 ddlZ	 ddlZddlZddlmZ dZdZd	Z ej(                  d
      Z e ee	j0                  e         g      ZdZdZ G d d ej:                  dd            Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&ddZ'ddZ(d Z)d Z*d Z+y# e$ r dZY w xY w# e$ r dZY w xY w)z:Internal helper functions for Abseil Python flags library.    )absolute_import)division)print_functionN)rangeP   (   g      ?u    [ -----￾￿]c                       e Zd ZdZy)_ModuleObjectAndNamezkModule object and name.

  Fields:
  - module: object, module object.
  - module_name: str, module name.
  N)__name__
__module____qualname____doc__     .platform/bq/third_party/absl/flags/_helpers.pyr
   r
   P   s    r   r
   zmodule module_namec                     | j                  dd      }t        j                  j                  |d      }t        ||dk(  rt        j                  d         S |      S )aN  Returns the module that defines a global environment, and its name.

  Args:
    globals_dict: A dictionary that should correspond to an environment
      providing the values of the globals.

  Returns:
    _ModuleObjectAndName - pair of module object & module name.
    Returns (None, None) if the module could not be identified.
  r   N__main__r   )getsysmodulesr
   argv)globals_dictnamemodules      r   get_module_object_and_namer   Z   s\     
		*d	+$;;??4&&	f.2j.@sxx{
M MFJ
M Mr   c                      t        dt        j                               D ]Q  } t        j                  |       j                  }t        |      \  }}t        |      t        vsB|Et        ||      c S  t        d      )a)  Returns the module that's calling into this module.

  We generally use this function to get the name of the module calling a
  DEFINE_foo... function.

  Returns:
    The module object that called into this one.

  Raises:
    AssertionError: Raised when no calling module could be identified.
     zNo module was found)
r   r   getrecursionlimit	_getframe	f_globalsr   iddisclaim_module_idsr
   AssertionError)depthglobals_for_framer   module_names       r   "get_calling_module_object_and_namer'   l   sr     Q--/0e e,6645FGFK	&z,,1H!&+66 1 	,--r   c                  *    t               j                  S )z?Returns the name of the module that's calling into this module.)r'   r&   r   r   r   get_calling_moduler)      s    	+	-	9	99r   c                 N    	 t        |       S # t        $ r t        |       cY S w xY w)a  Converts a value to a python string.

  Behavior of this function is intentionally different in Python2/3.

  In Python2, the given value is attempted to convert to a str (byte string).
  If it contains non-ASCII characters, it is converted to a unicode instead.

  In Python3, the given value is always converted to a str (unicode string).

  This behavior reflects the (bad) practice in Python2 to try to represent
  a string as str as long as it contains ASCII characters only.

  Args:
    value: An object to be converted to a string.

  Returns:
    A string representation of the given value. See the description above
    for its type.
  )strUnicodeEncodeErrorunicode)values    r   str_or_unicoder/      s*    (u:	 5>s   
 $$c                 N   t        |      }t        j                  r"t        |t              s|j                  dd      }t        |t              r|j                         }t        j                  d|      }| j                  |      }|j                  | j                  |             |S )a  Returns an XML DOM element with name and text value.

  Args:
    doc: minidom.Document, the DOM document it should create nodes from.
    name: str, the tag of XML element.
    value: object, whose string representation will be used
        as the value of the XML element. Illegal or highly discouraged xml 1.0
        characters are stripped.

  Returns:
    An instance of minidom.Element.
  zutf-8ignore )r/   sixPY2
isinstancer-   decodeboollower_ILLEGAL_XML_CHARS_REGEXsubcreateElementappendChildcreateTextNode)docr   r.   ses        r   create_xml_dom_elementrA      s     U!WWZ7+	(#At		A""3*!	!--""1%&	
(r   c                     t         j                  j                         rt        t        t
        S 	 t	        j                  t         j                  t        j                  d      } t        j                  d|       d   }|t        k\  r|S t        t        j                  dt
                    S # t        t        t        j                   f$ r	 t
        cY S w xY w)zAReturns the integer width of help lines that is used in TextWrap.1234hhr   COLUMNS)r   stdoutisattytermiosfcntl_DEFAULT_HELP_WIDTHioctl
TIOCGWINSZstructunpack_MIN_HELP_WIDTHintosgetenv	TypeErrorIOErrorerror)datacolumnss     r   get_help_widthrX      s    				5=;;szz7#5#5v>DmmD$'*G /!nryy$7899
Wfll	+ s   AB, 	"B, ,$CCc                 v   t        |       dk  s|sg S |D cg c]  }|j                  d      d    }}|D cg c]  }t        | |dt        |              |f }}|j                          |d   \  }}|t        t        |       z  k\  rg S g }|D ]  \  }	}
|	|k(  r|j                  |
        |S  |S c c}w c c}w )z4Returns helpful similar matches for an invalid flag.   =r   )lensplit_damerau_levenshteinsort _SUGGESTION_ERROR_RATE_THRESHOLDappend)attemptlongopt_listvoption_namesoption	distancesleast_errors_suggestionserrorsr   s              r   get_flag_suggestionsrl      s     	\QlI+78<a!''#,q/<,8
 *+)6 %WfQs7|.DEvN)  + ..aL/,5GDDI+lfd	  
 
) 9+s
   B1!B6c                 (    i fd | |      S )z6Returns Damerau-Levenshtein edit distance from a to b.c           
      ~   | |fv r| |f   S | st        |      }n|st        |       }nt         | dd |      dz    | |dd       dz    | dd |dd       | d   |d   k7  z         }t        |       dk\  r=t        |      dk\  r/| d   |d   k(  r$| d   |d   k(  r | dd |dd       dz   }||kD  r|}|| |f<   |S )z5Recursively defined string distance with memoization.r   Nr   rZ   )r\   min)xydtdistancememos       r   rt   z&_damerau_levenshtein.<locals>.distance   s    	1v~!Q$Z
a&a
a&a

1QR5!
q
 
1ae
q
 
1QR5!AB%
 AaDAaDL
13a 
Q1Q111!A$!A$,QqrUAabE"Q&q5!DAJHr   r   )abrt   ru   s     @@r   r^   r^      s    	$* 
!Qr   c                    |
t               }|d}||}t        |      |k\  rt        d      t        |      |k\  rt        d      | j                  d      } g }t	        j
                  |||      }t	        j
                  |||      }d | j                         D        D ]8  }|r!|j                  |j                  |             n|j                  d       |}: dj                  |      S )aL  Wraps a given text to a maximum line length and returns it.

  It turns lines that only contain whitespace into empty lines, keeps new lines,
  and expands tabs using 4 spaces.

  Args:
    text: str, text to wrap.
    length: int, maximum length of a line, includes indentation.
        If this is None then use get_help_width()
    indent: str, indent for all but first line.
    firstline_indent: str, indent for first line; if None, fall back to indent.

  Returns:
    str, the wrapped text.

  Raises:
    ValueError: Raised if indent or firstline_indent not shorter than length.
  r2   zLength of indent exceeds lengthz*Length of first line indent exceeds length   )widthinitial_indentsubsequent_indentc              3   <   K   | ]  }|j                           y wN)strip).0ps     r   	<genexpr>ztext_wrap.<locals>.<genexpr>6  s     9'8!AGGI'8s   
)rX   r\   
ValueError
expandtabstextwrapTextWrapper
splitlinesextendwrapra   join)textlengthindentfirstline_indentresultwrappersubsequent_wrapper	paragraphs           r   	text_wrapr     s    ( ^F^F[F
6
77	f$
A
BB		$&   #3vO'++6VE :t'89immGLL+,mmB G : 
6	r   c              #     K   t        j                  |       D ]  \  }}|d|z   t        |t              r|rd|z   *d|z   2t        |t        t        d      f      rd|d| X	 |r||v r|D ]  }d|dt        |        n d|ddj                  d |D                y# t        $ r d|d| Y w xY ww)	a  Convert a dict of values into process call parameters.

  This method is used to convert a dictionary into a sequence of parameters
  for a binary that parses arguments using this module.

  Args:
    flag_map: dict, a mapping where the keys are flag names (strings).
        values are treated according to their type:
        * If value is None, then only the name is emitted.
        * If value is True, then only the name is emitted.
        * If value is False, then only the name prepended with 'no' is emitted.
        * If value is a string then --name=value is emitted.
        * If value is a collection, this will emit --name=value1,value2,value3,
          unless the flag name is in multi_flags, in which case this will emit
          --name=value1 --name=value2 --name=value3.
        * Everything else is converted to string an passed as such.
    multi_flags: set, names (strings) of flags that should be treated as
        multi-flags.
  Yields:
    sequence of string suitable for a subprocess execution.
  Nz--%sz--no%sr2   z--r[   ,c              3   2   K   | ]  }t        |        y wr~   )r+   )r   items     r   r   z$flag_dict_to_args.<locals>.<genexpr>i  s     *G3t9s   )	r3   	iteritemsr5   r7   bytestyper+   r   rS   )flag_mapmulti_flagskeyr.   r   s        r   flag_dict_to_argsr   A  s     , MM(+jc5}SL	E4	 	sln	EE49-	.e$$'3+-d"CI..  !#((*G*G"GH
H% ,&  '&&'s+   A.C1AB52C5CC
CCc                    | syd}| j                         j                         }|}|dd D ]6  }|j                         }|st        |t	        |      t	        |      z
        }8 |d   j                         g}||k  r,|dd D ]$  }|j                  ||d j                                & |r|d   s|j                          |r|d   s|r|d   s|j                  d       |r|d   sdj                  |      S )a  Removes indentation from triple-quoted strings.

  This is the function specified in PEP 257 to handle docstrings:
  https://www.python.org/dev/peps/pep-0257/.

  Args:
    docstring: str, a python docstring.

  Returns:
    str, docstring with indentation removed.
  r2   i    r   Nr   r   )
r   r   lstripro   r\   r   ra   rstrippopr   )	docstring
max_indentlinesr   linestrippedtrimmeds          r   trim_docstringr   o  s     
 * 


 
+
+
-% &ABid{{}H63t9s8}45f 
 1X^^'jab	nnT&']))+,  	KKM 	
KKN 	
 
7	r   c                     | j                         } t        j                  dt        j                        }|j	                  d|       } t        |       } t        j                  dd| t        j                        } | S )z0Takes a __doc__ string and reformats it as help.z^[ 	]+$r2   z(?<=\S)\n(?=\S) )flags)r   recompileMr:   r   )r>   whitespace_only_lines     r   doc_to_helpr     sd     			# J5  S)# 	s# 	!32448#	*r   c                 f    t         t        u rt        | t              S t        | t         t        f      S r~   )r+   r   r5   
basestring)maybe_strings    r   is_bytes_or_stringr     s'    E\lJ//lS%L11r   )Nr2   Nr~   ),r   
__future__r   r   r   collectionsrQ   r   rM   r   r   rI   ImportErrorrH   r3   	six.movesr   rJ   rO   r`   r   r9   setr!   r   r   r"   SPECIAL_FLAGSFLAGS_MODULE
namedtupler
   r   r'   r)   r/   rA   rX   rl   r^   r   r   r   r   r   r   r   r   <module>r      s%   A &  %  	 	  
      $(  
 &2::NP  2ckk(3456   K13GHM$.,:
48$:86r+'\'T42m  
%
  's"   B? C ?C	C	CC