B @`b9@srdZddlmZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z y ddl Z Wne k r|dZ YnXy ddl Z Wne k rdZ YnXddlZddlmZdZdZd Zed Zeee jegZdZdZGd d d ed d ZddZddZ ddZ!ddZ"ddZ#ddZ$ddZ%ddZ&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)rangeP(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)getsysmodulesrargv)Z globals_dictnamemoduler r rget_module_object_and_nameZs rcCsZxLtdtD]:}t|j}t|\}}t|tkr|dk rt||SqWt ddS)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) rrgetrecursionlimit _getframe f_globalsriddisclaim_module_idsrAssertionError)depthZglobals_for_framer module_namer r r"get_calling_module_object_and_namels   r cCstjS)z?Returns the name of the module that's calling into this module.)r rr r r rget_calling_modulesr!cCs&yt|Stk r t|SXdS)aConverts 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)strUnicodeEncodeErrorunicode)valuer r rstr_or_unicodesr&cCs`t|}tjr$t|ts$|dd}t|tr6|}t d|}| |}| | ||S)aReturns 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-8ignore) r&sixPY2 isinstancer$decodeboollower_ILLEGAL_XML_CHARS_REGEXsub createElement appendChildZcreateTextNode)docrr%ser r rcreate_xml_dom_elements     r6c Cs~tjrtdkstdkrtSy>ttjtjd}t d|d}|t krN|St t dtStttjfk rxtSXdS)zAReturns the integer width of help lines that is used in TextWrap.NZ1234hhrCOLUMNS)rstdoutisattytermiosfcntl_DEFAULT_HELP_WIDTHZioctl TIOCGWINSZstructunpack_MIN_HELP_WIDTHintosgetenv TypeErrorIOErrorerror)datacolumnsr r rget_help_widthsrJc stdks|sgSdd|D}fdd|D}||d\}}|ttkr\gSg}x&|D]\}}||kr||qfPqfW|S)z4Returns helpful similar matches for an invalid flag.cSsg|]}|ddqS)=r)split).0vr r r sz(get_flag_suggestions..cs&g|]}t|dt|fqS)r)_damerau_levenshteinlen)rNoption)attemptr rrPsr)rRsort _SUGGESTION_ERROR_RATE_THRESHOLDappend) rTZ longopt_list option_namesZ distancesZ least_errors_Z suggestionserrorsrr )rTrget_flag_suggestionss   r[csifdd||S)z6Returns Damerau-Levenshtein edit distance from a to b.cs||fkr||fS|s&t|}n|s4t|}nt|dd|d||ddd|dd|dd|d|dk}t|dkrt|dkr|d|dkr|d|dkr|dd|ddd}||kr|}|||f<|S)z5Recursively defined string distance with memoization.rNrrK)rRmin)xydt)distancememor rras     ,8 z&_damerau_levenshtein..distancer )abr )rarbrrQsrQr(cCs|dkrt}|dkrd}|dkr&|}t||kr:tdt||krNtd|d}g}tj|||d}tj|||d}x>dd|DD](}|r|||n | d|}qWd |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)widthinitial_indentsubsequent_indentcss|]}|VqdS)N)strip)rNpr r r 6sztext_wrap.. ) rJrR ValueError expandtabstextwrap TextWrapper splitlinesextendwraprWjoin)textlengthindentZfirstline_indentresultwrapperZsubsequent_wrapperZ paragraphr r r text_wraps,      rzc csxt|D]\}}|dkr(d|Vq t|trN|rBd|Vqd|Vq t|ttdfrpd||fVq yP|r||krx>|D]}d|t|fVqWnd|ddd|DfVWq tk rd||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")rNitemr r rrkisz$flag_dict_to_args..) r) iteritemsr+r-bytestyper"rtrE)Zflag_mapZ multi_flagskeyr%r|r r rflag_dict_to_argsAs       "rcCs|sdSd}|}|}x6|ddD]&}|}|r*t|t|t|}q*W|dg}||krx*|ddD]}|||dqxWx|r|ds|qWx|r|ds|dqWd |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 rNrrl) rnrqlstripr\rRrirWrstrippoprt)Z docstringZ max_indentlinesrwlinestrippedZtrimmedr r rtrim_docstringos$   rcCsB|}tdtj}|d|}t|}tjdd|tjd}|S)z0Takes a __doc__ string and reformats it as help.z^[ ]+$r(z(?<=\S)\n(?=\S) )flags)rirecompileMr0r)r3Zwhitespace_only_liner r r doc_to_helps  rcCs$ttkrt|tSt|ttfSdS)N)r"r~r+ basestring)Z maybe_stringr r ris_bytes_or_strings r)Nr(N)N),r __future__rrr collectionsrCrr?rror< ImportErrorr;r)Z six.movesrr=rArVrr/setrrr rZ SPECIAL_FLAGSZ FLAGS_MODULE namedtuplerrr r!r&r6rJr[rQrzrrrrr r r rsT            9 .*