B �@`b9ã@srdZddlmZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z y ddl Z Wne k r|dZ YnXy ddl Z Wne k r¢dZ YnXddlZddlmZdZdZd Ze d ¡Zeee jeƒgƒZdZdZGd d „d e d d ¡ƒZdd„Zdd„Z dd„Z!dd„Z"dd„Z#dd„Z$dd„Z%dd„Z&d)dd „Z'd*d!d"„Z(d#d$„Z)d%d&„Z*d'd(„Z+dS)+z:Internal helper functions for Abseil Python flags library.é)Úabsolute_import)Údivision)Úprint_functionN)ÚrangeéPé(gà?u [- --„†-ÂŸí €-í¿¿ï¿¾ï¿¿]c@seZdZdZdS)Ú_ModuleObjectAndNamezkModule object and name. Fields: - module: object, module object. - module_name: str, module name. N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©r r ú7/tmp/pip-unpacked-wheel-01biqzia/absl/flags/_helpers.pyrPsrzmodule module_namecCs6| dd¡}tj |d¡}t||dkr0tjdn|ƒS)aNReturns 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_nameZs rcCsZxLtdt ¡ƒD]:}t |¡j}t|ƒ\}}t|ƒtkr|dk rt||ƒSqWt dƒ‚dS)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. éNzNo module was found) rrÚgetrecursionlimitÚ _getframeÚ f_globalsrÚidÚdisclaim_module_idsrÚAssertionError)ÚdepthZglobals_for_framerÚ module_namer r rÚ"get_calling_module_object_and_namels   r cCstƒjS)z?Returns the name of the module that's calling into this module.)r rr r r rÚget_calling_module‚sr!cCs&yt|ƒStk r t|ƒSXdS)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. N)ÚstrÚUnicodeEncodeErrorÚunicode)Úvaluer r rÚstr_or_unicode‡sr&cCs`t|ƒ}tjr$t|tƒs$| dd¡}t|tƒr6| ¡}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. zutf-8ÚignoreÚ) r&ÚsixÚPY2Ú isinstancer$ÚdecodeÚboolÚlowerÚ_ILLEGAL_XML_CHARS_REGEXÚsubÚ createElementÚ appendChildZcreateTextNode)Údocrr%ÚsÚer r rÚcreate_xml_dom_element¡s     r6c Cs~tj ¡rtdkstdkrtSy>t tjtjd¡}t  d|¡d}|t krN|St t   dt¡ƒStttjfk rxtSXdS)zAReturns the integer width of help lines that is used in TextWrap.NZ1234ÚhhrÚCOLUMNS)rÚstdoutÚisattyÚtermiosÚfcntlÚ_DEFAULT_HELP_WIDTHZioctlÚ TIOCGWINSZÚstructÚunpackÚ_MIN_HELP_WIDTHÚintÚosÚgetenvÚ TypeErrorÚIOErrorÚerror)ÚdataÚcolumnsr r rÚget_help_width½srJc sŒtˆƒdks|sgSdd„|Dƒ}‡fdd„|Dƒ}| ¡|d\}}|ttˆƒkr\gSg}x&|D]\}}||kr‚| |¡qfPqfW|S)z4Returns helpful similar matches for an invalid flag.écSsg|]}| d¡d‘qS)ú=r)Úsplit)Ú.0Úvr r rú Õsz(get_flag_suggestions..cs&g|]}tˆ|dtˆƒ…ƒ|f‘qS)r)Ú_damerau_levenshteinÚlen)rNÚoption)Úattemptr rrPÙsr)rRÚsortÚ _SUGGESTION_ERROR_RATE_THRESHOLDÚappend) rTZ longopt_listÚ option_namesZ distancesZ least_errorsÚ_Z suggestionsÚerrorsrr )rTrÚget_flag_suggestionsÏs   r[csi‰‡‡fdd„‰ˆ||ƒS)z6Returns Damerau-Levenshtein edit distance from a to b.csü||fˆkrˆ||fS|s&t|ƒ}nÆ|s4t|ƒ}n¸tˆ|dd…|ƒdˆ||dd…ƒdˆ|dd…|dd…ƒ|d|dkƒ}t|ƒdkrìt|ƒdkrì|d|dkrì|d|dkrìˆ|dd…|dd…ƒd}||krì|}|ˆ||f<|S)z5Recursively defined string distance with memoization.rNrrK)rRÚmin)ÚxÚyÚdÚt)ÚdistanceÚmemor rraðs     ,8 z&_damerau_levenshtein..distancer )ÚaÚbr )rarbrrQìsrQr(cCsÆ|dkrtƒ}|dkrd}|dkr&|}t|ƒ|kr:tdƒ‚t|ƒ|krNtdƒ‚| d¡}g}tj|||d�}tj|||d�}x>dd„| ¡DƒD](}|rª| | |¡¡n |  d¡|}q�Wd   |¡S) aLWraps 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(zLength of indent exceeds lengthz*Length of first line indent exceeds lengthé)ÚwidthÚinitial_indentÚsubsequent_indentcss|]}| ¡VqdS)N)Ústrip)rNÚpr r rú 6sztext_wrap..Ú ) rJrRÚ ValueErrorÚ expandtabsÚtextwrapÚ TextWrapperÚ splitlinesÚextendÚwraprWÚjoin)ÚtextÚlengthÚindentZfirstline_indentÚresultÚwrapperZsubsequent_wrapperZ paragraphr r rÚ text_wraps,      rzc csìxæt |¡D]Ø\}}|dkr(d|Vq t|tƒrN|rBd|Vqäd|Vq t|ttdƒfƒrpd||fVq yP|r ||kr x>|D]}d|t|ƒfVq„Wnd|d dd„|Dƒ¡fVWq tk râd||fVYq Xq WdS) aConvert 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ú,css|]}t|ƒVqdS)N)r")rNÚitemr r rrkisz$flag_dict_to_args..) r)Ú iteritemsr+r-ÚbytesÚtyper"rtrE)Zflag_mapZ multi_flagsÚkeyr%r|r r rÚflag_dict_to_argsAs       "r�cCsÖ|sdSd}| ¡ ¡}|}x6|dd…D]&}| ¡}|r*t|t|ƒt|ƒƒ}q*W|d ¡g}||kr–x*|dd…D]}| ||d… ¡¡qxWx|r®|ds®| ¡q˜Wx|rÊ|dsÊ| d¡q²Wd  |¡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 rNréÿÿÿÿrl) rnrqÚlstripr\rRrirWÚrstripÚpoprt)Z docstringZ max_indentÚlinesrwÚlineÚstrippedZtrimmedr r rÚtrim_docstringos$   r‰cCsB| ¡}t dtj¡}| d|¡}t|ƒ}tjdd|tjd�}|S)z0Takes a __doc__ string and reformats it as help.z^[ ]+$r(z(?<=\S)\n(?=\S)ú )Úflags)riÚreÚcompileÚMr0r‰)r3Zwhitespace_only_liner r rÚ doc_to_help™s  r�cCs$ttkrt|tƒSt|ttfƒSdS)N)r"r~r+Ú basestring)Z maybe_stringr r rÚis_bytes_or_string³s r‘)Nr(N)N),r Ú __future__rrrÚ collectionsrCrŒr?rror<Ú ImportErrorr;r)Z six.movesrr=rArVr�r/Úsetrrr rZ SPECIAL_FLAGSZ FLAGS_MODULEÚ namedtuplerrr r!r&r6rJr[rQrzr�r‰r�r‘r r r rÚsT            9 .*