o
    Si3                     @   s  U d Z ddlmZ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	m
Z
mZ ddlmZ dZdZdZed	Zeeje hZee ed
< dZe
ed< dZejdB ed< G dd deZdeee
f dedB fddZ defddZ!defddZ"dej#dede
dej$fddZ%defddZ&dedee de'e fd d!Z(d"d# Z)		$	d4d%ed&edB d'ed(edB def
d)d*Z*	d5d+eee
f d,ee dB dee fd-d.Z+d/edefd0d1Z,dedefd2d3Z-dS )6z:Internal helper functions for Abseil Python flags library.    )IterableSequenceN)Any
NamedTuple)minidomP   (   g      ?u    [ -----￾￿]disclaim_module_idsSPECIAL_FLAGSFLAGS_MODULEc                   @   s$   e Zd ZU dZejed< eed< dS )_ModuleObjectAndNamezkModule object and name.

  Fields:
  - module: object, module object.
  - module_name: str, module name.
  modulemodule_nameN)__name__
__module____qualname____doc__types
ModuleType__annotations__str r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/absl/flags/_helpers.pyr   F   s   
 
r   globals_dictreturnc                 C   sJ   z| d }t j| }W n
 ty   Y dS w t||dkr"t jd S |S )aF  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 if the module could not be identified.
  r   N__main__r   )sysmodulesKeyErrorr   argv)r   namer   r   r   r   get_module_object_and_nameQ   s   r!   c                  C   sN   t dt D ]} t| j}t|}|dur"t|jtvr"|  S q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.
     NzNo module was found)
ranger   getrecursionlimit	_getframe	f_globalsr!   idr   r	   AssertionError)depthglobals_for_framer   r   r   r   "get_calling_module_object_and_namei   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,   docr    valuec                 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.
   )	r   
isinstanceboollower_ILLEGAL_XML_CHARS_REGEXsubcreateElementappendChildcreateTextNode)r-   r    r.   ser   r   r   create_xml_dom_element   s   

r:   c                  C   s   t jtdfd} | jS )zAReturns the integer width of help lines that is used in TextWrap.r"   )fallback)shutilget_terminal_size_DEFAULT_HELP_WIDTHcolumns)sizer   r   r   get_help_width   s   rA   attemptlongopt_listc           	         s   t  dks|s
g S dd |D } fdd|D }|  |d \}}|tt   kr.g S g }|D ]\}}||kr@|| q2 |S |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)rG   optionrB   r   r   rI      s    r   )rK   sort _SUGGESTION_ERROR_RATE_THRESHOLDappend)	rB   rC   option_names	distancesleast_errors_suggestionserrorsr    r   rM   r   get_flag_suggestions   s    
rW   c                    s   i  fdd  | |S )z6Returns Damerau-Levenshtein edit distance from a to b.c                    s   | |fv r| |f S | st |}nc|st | }n\t | dd |d  | |dd d  | dd |dd | d |d k }t | dkrvt |dkrv| d |d krv| d |d krv | dd |dd d }||krv|}|| |f< |S )z5Recursively defined string distance with memoization.r"   Nr   rD   )rK   min)xydtdistancememor   r   r^      s"   

(8z&_damerau_levenshtein.<locals>.distancer   )abr   r]   r   rJ      s   
rJ   r/   textlengthindentfirstline_indentc                 C   s   |du rt  }|du rd}|du r|}t||krtdt||kr'td| d} g }tj|||d}tj|||d}dd |  D D ]}|rT||| n|	d |}qGd	
|S )
a:  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: Text to wrap.
    length: Maximum length of a line, includes indentation. If this is `None`
      then use `get_help_width()`.
    indent: Indent for all but first line.
    firstline_indent: Indent for first line. If `None`, fall back to `indent`.

  Returns:
    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)rG   pr   r   r   	<genexpr>      ztext_wrap.<locals>.<genexpr>
)rA   rK   
ValueError
expandtabstextwrapTextWrapper
splitlinesextendwraprP   join)rb   rc   rd   re   resultwrappersubsequent_wrapper	paragraphr   r   r   	text_wrap   s0   


r|   flag_mapmulti_flagsc              	   c   s    |   D ]f\}}|du rd| V  qt|tr&|r d| V  qd| V  qt|ttfr5d||f V  qz&|rK||v rK|D ]}d|t|f V  q>nd|ddd |D f V  W q tyk   d||f V  Y 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%sz--%s=%s,c                 s   s    | ]}t |V  qd S rj   )r   )rG   itemr   r   r   rm   L  rn   z$flag_dict_to_args.<locals>.<genexpr>)itemsr0   r1   bytesr   rw   	TypeError)r}   r~   keyr.   r   r   r   r   flag_dict_to_args  s*   
r   	docstringc                 C   s   | sdS d}|    }|}|dd D ]}| }|r't|t|t| }q|d  g}||k rG|dd D ]}|||d   q9|rW|d sW|  |rW|d rM|rh|d sh|d |rh|d r]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   ro   )
rq   rt   lstriprX   rK   rk   rP   rstrippoprw   )r   
max_indentlinesrd   linestrippedtrimmedr   r   r   trim_docstringR  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)rk   recompileMr4   r   )r-   whitespace_only_liner   r   r   doc_to_help|  s   r   )Nr/   Nrj   ).r   collections.abcr   r   r   r<   r   rr   r   typingr   r   xml.domr   r>   _MIN_HELP_WIDTHrO   r   r3   r'   r   r   r	   setintr   r
   r   r   r   dictr   r!   r+   r,   DocumentElementr:   rA   listrW   rJ   r|   r   r   r   r   r   r   r   <module>   s   




?


4*