B
    dv4                 @   s*  d 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yddlZW n ek
rX   dZY nX yddl	Z	W n ek
r~   dZ	Y nX dZ
dZdZedZeeeje gZdZdZG dd dedd	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zd!ddZd"ddZ dd Z!dd  Z"dS )#z:Internal helper functions for Abseil Python flags library.    NP   (   g      ?u    [ -----￾￿]c               @   s   e Zd ZdZdS )_ModuleObjectAndNamezkModule object and name.

  Fields:
  - module: object, module object.
  - module_name: str, module name.
  N)__name__
__module____qualname____doc__ r	   r	   E/var/www/html/venv/lib/python3.7/site-packages/absl/flags/_helpers.pyr   J   s   r   zmodule module_namec             C   s6   |  dd}tj |d}t||dkr0tjd n|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)Zglobals_dictnamemoduler	   r	   r
   get_module_object_and_nameT   s    r   c              C   sZ   xLt dt D ]:} t| j}t|\}}t|tkr|dk	rt||S qW t	ddS )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.
     NzNo module was found)
ranger   getrecursionlimit	_getframe	f_globalsr   iddisclaim_module_idsr   AssertionError)depthZglobals_for_framer   module_namer	   r	   r
   "get_calling_module_object_and_namef   s    r   c               C   s   t  jS )z?Returns the name of the module that's calling into this module.)r   r   r	   r	   r	   r
   get_calling_module|   s    r   c             C   sD   t |}t|tr| }td|}| |}|| | |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.
   )	str
isinstanceboollower_ILLEGAL_XML_CHARS_REGEXsubZcreateElementZappendChildZcreateTextNode)docr   valueser	   r	   r
   create_xml_dom_element   s    

r*   c           
   C   s~   t j rtdkstdkrtS y>tt jtjd} t	d| d }|t
krN|S ttdtS  tttjfk
rx   tS X dS )zAReturns the integer width of help lines that is used in TextWrap.N1234hhr   COLUMNS)r   stdoutisattytermiosfcntl_DEFAULT_HELP_WIDTHZioctl
TIOCGWINSZstructunpack_MIN_HELP_WIDTHintosgetenv	TypeErrorIOErrorerror)datacolumnsr	   r	   r
   get_help_width   s    r?   c       	         s   t  dks|sg S dd |D } fdd|D }|  |d \}}|tt   kr\g S g }x&|D ]\}}||kr|| qfP qfW |S )z4Returns helpful similar matches for an invalid flag.   c             S   s   g | ]}| d d qS )=r   )split).0vr	   r	   r
   
<listcomp>   s    z(get_flag_suggestions.<locals>.<listcomp>c                s&   g | ]}t  |d t  |fqS )r   )_damerau_levenshteinlen)rC   option)attemptr	   r
   rE      s   r   )rG   sort _SUGGESTION_ERROR_RATE_THRESHOLDappend)	rI   Zlongopt_listZoption_namesZ	distancesZleast_errors_Zsuggestionserrorsr   r	   )rI   r
   get_flag_suggestions   s    
rO   c                s   i  fdd  | |S )z6Returns Damerau-Levenshtein edit distance from a to b.c                s   | |fkr| |f S | s&t |}n|s4t | }nt | dd |d  | |dd d  | dd |dd | d |d k }t | dkrt |dkr| d |d kr| d |d kr | dd |dd d }||kr|}|| |f< |S )z5Recursively defined string distance with memoization.r   Nr   r@   )rG   min)xydt)distancememor	   r
   rU      s     

,8z&_damerau_levenshtein.<locals>.distancer	   )abr	   )rU   rV   r
   rF      s    rF   r   c             C   s   |dkrt  }|dkrd}|dkr&|}t||kr:tdt||krNtd| d} g }tj|||d}tj|||d}x>dd |  D D ](}|r||| n
|	d |}qW d	
|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.
  Nr   zLength of indent exceeds lengthz*Length of first line indent exceeds length   )widthinitial_indentsubsequent_indentc             s   s   | ]}|  V  qd S )N)strip)rC   pr	   r	   r
   	<genexpr>  s    ztext_wrap.<locals>.<genexpr>
)r?   rG   
ValueError
expandtabstextwrapTextWrapper
splitlinesextendwraprL   join)textlengthindentZfirstline_indentresultwrapperZsubsequent_wrapperZ	paragraphr	   r	   r
   	text_wrap   s,    

rn   c          	   c   s   x|   D ]\}}|dkr&d| V  q
t|trL|r@d| V  qd| V  q
t|ttdfrnd||f V  q
yP|r||krx>|D ]}d|t|f V  qW nd|ddd |D f V  W q
 tk
r   d||f V  Y q
X q
W dS )	a3  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%sr   z--%s=%s,c             s   s   | ]}t |V  qd S )N)r    )rC   itemr	   r	   r
   r_   J  s    z$flag_dict_to_args.<locals>.<genexpr>)itemsr!   r"   bytestyper    rh   r:   )Zflag_mapZmulti_flagskeyr'   rp   r	   r	   r
   flag_dict_to_args  s     

"ru   c             C   s   | sdS d}|    }|}x6|dd D ]&}| }|r*t|t|t| }q*W |d  g}||k rx*|dd D ]}|||d   qxW x|r|d s|  qW x|r|d s|d qW d	|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.
  r   i    r   Nr   r`   )
rb   re   lstriprP   rG   r]   rL   rstrippoprh   )Z	docstringZ
max_indentlinesrk   linestrippedZtrimmedr	   r	   r
   trim_docstringP  s$    r}   c             C   sB   |   } tdtj}|d| } t| } tjdd| tjd} | S )z0Takes a __doc__ string and reformats it as help.z^[ 	]+$r   z(?<=\S)\n(?=\S) )flags)r]   recompileMr%   r}   )r&   Zwhitespace_only_liner	   r	   r
   doc_to_helpz  s    r   )Nr   N)N)#r   collectionsr8   r   r4   r   rc   r1   ImportErrorr0   r2   r6   rK   r   r$   setr   r   r   r   ZSPECIAL_FLAGSZFLAGS_MODULE
namedtupler   r   r   r   r*   r?   rO   rF   rn   ru   r}   r   r	   r	   r	   r
   <module>   sF   


	
9
2*