B s `RCã@sÚdZddlmZddlmZddlmZddlZddlZddlmZddl m Z ddl m Z dd l m Z ddl Z ejGd d „d eƒƒZGd d „d eƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)zºContains Flag class - information about single command-line flag. Do NOT import this module directly. Import the flags package and use the aliases defined at the package level instead. é)Úabsolute_import)Údivision)Úprint_functionN)Úabc)Ú_argument_parser)Ú _exceptions)Ú_helpersc@sÆeZdZdZd.dd„Zedd„ƒZejd d„ƒZd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd/d(d)„Zd*d+„Zd,d-„ZdS)0ÚFlaga‡ Information about a command-line flag. 'Flag' objects define the following fields: .name - the name for this flag; .default - the default value for this flag; .default_unparsed - the unparsed default value for this flag. .default_as_str - default value as repr'd string, e.g., "'true'" (or None); .value - the most recent parsed value of this flag; set by parse(); .help - a help string or None if no help is available; .short_name - the single letter alias for this flag (or None); .boolean - if 'true', this flag does not accept arguments; .present - true if this flag was parsed from command line flags; .parser - an ArgumentParser object; .serializer - an ArgumentSerializer object; .allow_override - the flag may be redefined without raising an error, and newly defined flag overrides the old one. .allow_override_cpp - use the flag from C++ if available; the flag definition is replaced by the C++ flag after init; .allow_hide_cpp - use the Python flag despite having a C++ flag with the same name (ignore the C++ flag); .using_default_value - the flag value has not been set by user; .allow_overwrite - the flag may be parsed more than once without raising an error, the last set value will be used; .allow_using_method_names - whether this flag can be defined even if it has a name that conflicts with a FlagValues method. The only public method of a 'Flag' object is parse(), but it is typically only called by a 'FlagValues' object. The parse() method is a thin wrapper around the 'ArgumentParser' parse() method. The parsed value is saved in .value, and the .present attribute is updated. If this flag was already present, an Error is raised. parse() is also called during __init__ to parse the default value and initialize the .value attribute. This enables other python modules to safely use flags even if the __main__ module neglects to parse the command line arguments. The .present attribute is cleared after __init__ parsing. If the default value is set to None, then the __init__ parsing step is skipped and the .value attribute is initialized to None. Note: The default value is also presented to the user in the help string, so it is important that it be a legal value for this flag. NFTc Cs†||_|sd}||_||_||_d|_||_||_||_| |_| |_ | |_ | |_ d|_ d|_ g|_|j rx|jrxt d¡‚| |¡dS)Nz(no help available)rTzsCan't have both allow_hide_cpp (means use Python flag) and allow_override_cpp (means use C++ flag after InitGoogle))ÚnameÚhelpÚ short_nameÚbooleanÚpresentÚparserÚ serializerÚallow_overrideÚallow_override_cppÚallow_hide_cppÚallow_overwriteÚallow_using_method_namesÚusing_default_valueÚ_valueZ validatorsrÚErrorÚ _set_default) Úselfrrr ÚdefaultÚ help_stringr r rrrrr©rú4/tmp/pip-unpacked-wheel-00lyeop_/absl/flags/_flag.pyÚ__init__Qs* z Flag.__init__cCs|jS)N)r)rrrrÚvaluepsz Flag.valuecCs ||_dS)N)r)rr rrrr tscCs tt|ƒƒS)N)ÚhashÚid)rrrrÚ__hash__xsz Flag.__hash__cCs||kS)Nr)rÚotherrrrÚ__eq__{sz Flag.__eq__cCst|tƒrt|ƒt|ƒkStS)N)Ú isinstancer r"ÚNotImplemented)rr$rrrÚ__lt__~s z Flag.__lt__cCs tdƒ‚dS)Nzcan't pickle Flag objects)Ú TypeError)rrrrÚ __getstate__ƒszFlag.__getstate__cCstdt|ƒjƒ‚dS)Nz>%s does not support shallow copies. Use copy.deepcopy instead.)r)ÚtypeÚ__name__)rrrrÚ__copy__†sz Flag.__copy__cCs"t t|ƒ¡}t |j|¡|_|S)N)ÚobjectÚ__new__r+ÚcopyÚdeepcopyÚ__dict__)rÚmemoÚresultrrrÚ __deepcopy__ŠszFlag.__deepcopy__cCsJ|dkr dS|jr"t|j |¡ƒS|jr<|r4tdƒStdƒStt |¡ƒS)z$Returns parsed flag value as string.NÚtrueÚfalse)rÚreprÚ serializer rZstr_or_unicode)rr rrrÚ_get_parsed_value_as_string�sz Flag._get_parsed_value_as_stringcCsB|jr$|js$t d|j||jf¡‚| |¡|_|jd7_dS)z}Parses string and sets flag value. Args: argument: str or the correct flag value type, argument to be parsed. z#flag --%s=%s: already defined as %séN)rrrÚIllegalFlagValueErrorr r Ú_parse)rÚargumentrrrÚparseœs   z Flag.parsec CsNy |j |¡Sttfk rH}zt d|j||f¡‚Wdd}~XYnXdS)zÝInternal parse function. It returns the parsed value, and does not modify class states. Args: argument: str or the correct flag value type, argument to be parsed. Returns: The parsed value. zflag --%s=%s: %sN)rr?r)Ú ValueErrorrr<r )rr>Úerrrr=©s  z Flag._parsecCs|j|_d|_d|_dS)NTr)rr rr)rrrrÚunparseºsz Flag.unparsecCs | |j¡S)zSerializes the flag.)Ú _serializer )rrrrr9¿szFlag.serializecCs\|dkr dS|jr,|r d|jSd|jSn,|jsBt d|j¡‚d|j|j |¡fSdS)zInternal serialize function.NÚz--%sz--no%sz"Serializer not present for flag %sz--%s=%s)r r rrrr9)rr rrrrCÃs   zFlag._serializecCsB||_|dkrd|_n | |¡|_| |j¡|_|jr>|j|_dS)z@Changes the default value (and current value too) for this Flag.N)Zdefault_unparsedrÚ_parse_from_defaultr:Zdefault_as_strrr )rr rrrrÒs zFlag._set_defaultcCs | |¡S)N)r=)rr rrrrEßszFlag._parse_from_defaultcCs |j ¡S)aReturns a str that describes the type of the flag. NOTE: we use strings, and not the types.*Type constants because our flags can have more exotic types, e.g., 'comma separated list of strings', 'whitespace separated list of strings', etc. )rÚ flag_type)rrrrrFâszFlag.flag_typecCs,| d¡}|r"| t |dd¡¡| t |d|¡¡| t |d|j¡¡|jrh| t |d|j¡¡|jr„| t |d|j¡¡|jr¶t|j t ƒs¶|j dk r°|j  |j ¡}q¼d }n|j }| t |d |¡¡|  |j ¡}| t |d |¡¡| t |d | ¡¡¡x| |¡D]}| |¡�qW|S) a6Returns an XML element that contains this flag's information. This is information that is relevant to all flags (e.g., name, meaning, etc.). If you defined a flag that has some other pieces of info, then please override _ExtraXMLInfo. Please do NOT override this method. Args: doc: minidom.Document, the DOM document it should create nodes from. module_name: str,, the name of the module that defines this flag. is_key: boolean, True iff this flag is key for main module. Returns: A minidom.Element instance. ÚflagÚkeyÚyesÚfiler r ZmeaningNrDrÚcurrentr+)Ú createElementÚ appendChildrÚcreate_xml_dom_elementr r r rr&rÚstrr9Ú_serialize_value_for_xmlr rFÚ_extra_xml_dom_elements)rÚdocÚ module_nameZis_keyÚelementZdefault_serializedÚvalue_serializedrArrrÚ_create_xml_dom_elementës6      zFlag._create_xml_dom_elementcCs|S)z:Returns the serialized value, for use in an XML help text.r)rr rrrrPszFlag._serialize_value_for_xmlcCs |j |¡S)aReturns extra info about this flag in XML. "Extra" means "not already included by _create_xml_dom_element above." Args: doc: minidom.Document, the DOM document it should create nodes from. Returns: A list of minidom.Element. )rZ_custom_xml_dom_elements)rrRrrrrQ#s zFlag._extra_xml_dom_elements)NFFFFTF)F)r,Ú __module__Ú __qualname__Ú__doc__rÚpropertyr Úsetterr#r%r(r*r-r5r:r?r=rBr9rCrrErFrVrPrQrrrrr #s0,      4r cs"eZdZdZd‡fdd„ Z‡ZS)Ú BooleanFlaga¬Basic boolean flag. Boolean flags do not take any arguments, and their value is either True (1) or False (0). The false value is specified on the command line by prepending the word 'no' to either the long or the short flag name. For example, if a Boolean flag was created whose long name was 'update' and whose short name was 'x', then this flag could be explicitly unset through either --noupdate or --nox. Nc s,t ¡}tt|ƒj|d||||df|ŽdS)Nr;)rZ BooleanParserÚsuperr\r)rr rr r ÚargsÚp)Ú __class__rrr@s zBooleanFlag.__init__)N)r,rWrXrYrÚ __classcell__rr)r`rr\3s r\cs*eZdZdZd‡fdd„ Zdd„Z‡ZS) ÚEnumFlagzFBasic enum flag; its value can be any string from list of enum_values.NTc  sLt ||¡}t ¡} tt|ƒj|| ||||f|Ždd |¡|jf|_dS)Nz<%s>: %sú|)rZ EnumParserZArgumentSerializerr]rbrÚjoinr ) rr rr Ú enum_valuesr Úcase_sensitiver^r_Úg)r`rrrIs   zEnumFlag.__init__cCs.g}x$|jjD]}| t |d|¡¡qW|S)NÚ enum_value)rreÚappendrrN)rrRÚelementsrhrrrrQQs z EnumFlag._extra_xml_dom_elements)NT)r,rWrXrYrrQrarr)r`rrbFsrbcs*eZdZdZd‡fdd„ Zdd„Z‡ZS) Ú EnumClassFlagz5Basic enum flag; its value is an enum class's member.NFc  sVtj||d�}tj| d�} tt|ƒj|| ||||f|Ždd |j¡|jf|_dS)N)rf)Ú lowercasez<%s>: %src) rÚEnumClassParserZEnumClassSerializerr]rkrrdÚ member_namesr ) rr rr Ú enum_classr rfr^r_rg)r`rrr\s   zEnumClassFlag.__init__cCs4g}x*|jjj ¡D]}| t |d|¡¡qW|S)Nrh)rroÚ __members__ÚkeysrirrN)rrRrjrhrrrrQls z%EnumClassFlag._extra_xml_dom_elements)NF)r,rWrXrYrrQrarr)r`rrkYs rkcsPeZdZdZ‡fdd„Zdd„Z‡fdd„Z‡fdd „Zd d „Zd d „Z ‡Z S)Ú MultiFlagaeA flag that can appear multiple time on the command-line. The value of such a flag is a list that contains the individual values from all the appearances of that flag on the command-line. See the __doc__ for Flag for most behavior of this class. Only differences in behavior are described here: * The default value may be either a single value or an iterable of values. A single value is transformed into a single-item list of that value. * The value of the flag is always a list, even if the option was only supplied once, and even if the default value is a single value cs$tt|ƒj||Ž|jd7_dS)Nz4; repeat this option to specify a list of values)r]rrrr )rr^Úkwargs)r`rrr…szMultiFlag.__init__cCs:| |¡}|jr|j |¡n||_|jt|ƒ7_dS)aParses one or more arguments with the installed parser. Args: arguments: a single argument or a list of arguments (typically a list of default values); a single argument is converted internally into a list containing one item. N)r=rr ÚextendÚlen)rÚ argumentsZ new_valuesrrrr?‰s  zMultiFlag.parsecsDt|tjƒr t|tjƒs t|ƒ}t|tƒs0|g}‡‡fdd„|DƒS)Ncsg|]}ttˆƒ |¡‘qSr)r]rrr=)Ú.0Úitem)r`rrrú £sz$MultiFlag._parse..)r&rÚIterableÚsixÚ string_typesÚlist)rrv)r`)rrr=˜s    zMultiFlag._parsecs@ˆjst dˆj¡‚|dkr"dS‡‡fdd„|Dƒ}d |¡S)zSee base class.z"Serializer not present for flag %sNrDcsg|]}ttˆƒ |¡‘qSr)r]rrrC)rwZ value_item)r`rrrry®sz(MultiFlag._serialize..Ú )rrrr rd)rr Zserialized_items)r`)rrrC¥s zMultiFlag._serializecCsd|j ¡S)zSee base class.zmulti )rrF)rrrrrF³szMultiFlag.flag_typecCs:g}t|jdƒr6x$|jjD]}| t |d|¡¡qW|S)Nrerh)ÚhasattrrrerirrN)rrRrjrhrrrrQ·s  z!MultiFlag._extra_xml_dom_elements) r,rWrXrYrr?r=rCrFrQrarr)r`rrrts  rrcs2eZdZdZd ‡fdd„ Zdd„Zdd„Z‡ZS) ÚMultiEnumClassFlagzÁA multi_enum_class flag. See the __doc__ for MultiFlag for most behaviors of this class. In addition, this class knows how to handle enum.Enum instances as values for this flag type. Fc  sXtj||d�}tjd| d�}tt|ƒj|||||f|Ždd |j¡|pLdf|_dS)N)rfú,)Zlist_seprlz<<%s>: %s; repeat this option to specify a list of valuesrcz(no help available)) rrmZEnumClassListSerializerr]r€rrdrnr ) rr rrrorfr^r_rg)r`rrrÈs   zMultiEnumClassFlag.__init__cCs4g}x*|jjj ¡D]}| t |d|¡¡qW|S)Nrh)rrorprqrirrN)rrRrjrhrrrrQÙs z*MultiEnumClassFlag._extra_xml_dom_elementscCs|dk r|j |¡}nd}|S)zSee base class.NrD)rr9)rr rUrrrrPàsz+MultiEnumClassFlag._serialize_value_for_xml)F)r,rWrXrYrrQrPrarr)r`rr€Às r€)rYÚ __future__rrrr0Ú functoolsZabsl._collections_abcrZ absl.flagsrrrr{Útotal_orderingr.r r\rbrkrrr€rrrrÚs&       L