3 `;\֠@sddlZddlZddlZddlZddlZddlZejdkrHddlmZn ddlmZejd krlddl m Z n ddl m Z ej ej ejejejfZiZd!ddZd"d d ZGd d d ejejejZGd ddeZGdddeZGdddeZGdddeZdZdZ dZ!dZ"Gdddej#Z#ddZ$ddZ%ej&j$ej&_'e$ej&_$ej(Z(ejZejZejZej)Z)ej*Z*ej+Z+ej,Z,ej-Z-ej.Z.ej/Z/ej0Z0ej1Z1ej2Z2eZ3eZ4eZ5eZ6eZ7eZ8e#Z9e#Z:ej&j$ej&_;ej&j$ej&_e#j?e#_@eZAeZBeZCdS)#N)StringIO) OrderedDictcKsT|dkr d}|tkr(td|t|f|jdtj|jddtf|t|<dS)zCreates a global ArgumentParser instance with the given name, passing any args other than "name" to the ArgumentParser constructor. This instance can then be retrieved using get_argument_parser(..) Ndefaultzbkwargs besides 'name' can only be passed in the first time. '%s' ArgumentParser already exists: %sZformatter_classZconflict_handlerZresolve)_parsers ValueError setdefaultargparseArgumentDefaultsHelpFormatterArgumentParser)namekwargsr$/usr/lib/python3.6/configargparse.pyinit_argument_parsers rcKs4|dkr d}t|dks |tkr,t|f|t|S)a Returns the global ArgumentParser instance with the given name. The 1st time this function is called, a new ArgumentParser instance will be created for the given name, and any args other than "name" will be passed on to the ArgumentParser constructor. Nrr)lenrr)rrrrrget_argument_parser.s  rc@seZdZdZdS) ArgumentDefaultsRawHelpFormatterzCHelpFormatter that adds default values AND doesn't do line-wrappingN)__name__ __module__ __qualname____doc__rrrrr=src@s(eZdZdZddZddZddZdS) ConfigFileParserzRThis abstract class can be extended to add support for new config file formatscCs tddS)z3Returns a string describing the config file syntax.z*get_syntax_description(..) not implementedN)NotImplementedError)selfrrrget_syntax_descriptionIsz'ConfigFileParser.get_syntax_descriptioncCs tddS)aNParses the keys and values from a config file. NOTE: For keys that were specified to configargparse as action="store_true" or "store_false", the config file value must be one of: "yes", "no", "true", "false". Otherwise an error will be raised. Args: stream: A config file input stream (such as an open file object). Returns: OrderedDict of items where the keys have type string and the values have type either string or list (eg. to support config file formats like YAML which allow lists). zparse(..) not implementedN)r)rstreamrrrparseMszConfigFileParser.parsecCs tddS)aDoes the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents. Args: items: an OrderedDict of items to be converted to the config file format. Keys should be strings, and values should be either strings or lists. Returns: Contents of config file as a string zserialize(..) not implementedN)r)ritemsrrr serialize^s zConfigFileParser.serializeN)rrrrrrr!rrrrrEsrc@seZdZdZdS)ConfigFileParserExceptionz'Raised when config file parsing failed.N)rrrrrrrrr"msr"c@s(eZdZdZddZddZddZdS) DefaultConfigFileParseraBased on a simplified subset of INI and YAML formats. Here is the supported syntax: # this is a comment ; this is also a comment (.ini style) --- # lines that start with --- are ignored (yaml style) ------------------- [section] # .ini-style section names are treated as comments # how to specify a key-value pair (all of these are equivalent): name value # key is case sensitive: "Name" isn't "name" name = value # (.ini style) (white space is ignored, so name = value same as name=value) name: value # (yaml style) --name value # (argparse style) # how to set a flag arg (eg. arg which has action="store_true") --name name name = True # "True" and "true" are the same # how to specify a list arg (eg. arg which has action="append") fruit = [apple, orange, lemon] indexes = [1, 12, 35 , 40] cCsd}|S)NzrConfig file syntax allows: key=value, flag=true, stuff=[a,b,c] (for details, see syntax at https://goo.gl/R74nmi).r)rmsgrrrrsz.DefaultConfigFileParser.get_syntax_descriptionc Cs&t}xt|D] \}}|j}| s|ddks|jdrBqd}d}|d|d }|d }tjd ||d |} | r| jd }d||<qtjd |||d |} | r| jd }| jd}|jdo|jdrdd|ddjdD}|||<qt d|t |dd|fqW|S)z,Parses the keys + values from a config file.r#;[z---z\s*z(?P[^:=;#\s]+?)z[:=\s]z(?P.+?)z(?P\s[;#].*)?^$keytruevalue]cSsg|] }|jqSr)strip).0elemrrr sz1DefaultConfigFileParser.parse..,zUnexpected line %s in %s: %srr)r%r&r') r enumerater. startswithrematchgroupendswithsplitr"getattr) rrr ilineZ white_spacer*r,ZcommentZkey_only_matchZkey_value_matchrrrrs2   zDefaultConfigFileParser.parsecCsXt}xH|jD]<\}}t|tr:ddjtt|d}|jd||fqW|jS)zDoes the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents. r'z, r-z%s = %s ) rr isinstancelistjoinmapstrwritegetvalue)rr rr*r,rrrr!s  z!DefaultConfigFileParser.serializeN)rrrrrrr!rrrrr#qs#r#c@s2eZdZdZddZddZddZd d d Zd S) YAMLConfigFileParserzdParses YAML config files. Depends on the PyYAML module. https://pypi.python.org/pypi/PyYAML cCsd}|S)NzThe config file uses YAML syntax and must represent a YAML 'mapping' (for details, see http://learn.getgrav.org/advanced/yaml).r)rr$rrrrsz+YAMLConfigFileParser.get_syntax_descriptionc Cs.y ddl}Wntk r(tdYnX|S)zmlazy-import PyYAML so that configargparse doesn't have to dependend on it unless this parser is used.rNzJCould not import yaml. It can be installed by running 'pip install PyYAML')yaml ImportErrorr")rrHrrr _load_yamls  zYAMLConfigFileParser._load_yamlcCs|j}y|j|}Wn.tk rD}ztd|WYdd}~XnXt|tsntdt|ddt|jft }x4|j D](\}}t|t r|||<q~t |||<q~W|S)z.Parses the keys and values from a config file.zCouldn't parse config file: %sNzThe config file doesn't appear to contain 'key: value' pairs (aka. a YAML mapping). yaml.load('%s') returned type '%s' instead of 'dict'.rr) rJZ safe_load Exceptionr"r?dictr<typerrr r@rC)rrrHZ parsed_objeresultr*r,rrrrs   zYAMLConfigFileParser.parseFcCs|j}t|}|j||dS)zDoes the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents. Args: default_flow_style: defines serialization format (see PyYAML docs) )default_flow_style)rJrLdump)rr rPrHrrrr!s zYAMLConfigFileParser.serializeN)F)rrrrrrJrr!rrrrrGs  rGZ command_lineZenvironment_variables config_filedefaultsc @seZdZdZdddgdegddgdf ddZdddejfd d Zdddejfd d Z dd dZ ddZ ddZ ddZ ddZddZddZejfddZddZdS) r zDrop-in replacement for argparse.ArgumentParser that adds support for environment variables and .ini or .yaml-style config files. TNFzconfig file pathzftakes the current command line args and writes them out to a config file at the given path, then exitsc Ks||_||_||_tjj|f| |dkr4t|_n||_||_||_ |rb|j |d|| dd| r||j | dd| dddS)a Supports args of the argparse.ArgumentParser constructor as **kwargs, as well as the following additional args. Additional Args: add_config_file_help: Whether to add a description of config file syntax to the help message. add_env_var_help: Whether to add something to the help message for args that can be set through environment variables. auto_env_var_prefix: If set to a string instead of None, all config- file-settable options will become also settable via environment variables whose names are this prefix followed by the config file key, all in upper case. (eg. setting this to "foo_" will allow an arg like "--my-arg" to also be set via the FOO_MY_ARG environment variable) default_config_files: When specified, this list of config files will be parsed in order, with the values from each config file taking precedence over pervious ones. This allows an application to look for config files in multiple standard locations such as the install directory, home directory, and current directory. Also, shell * syntax can be used to specify all conf files in a directory. For exmaple: ["/etc/conf/app_config.ini", "/etc/conf/conf-enabled/*.ini", "~/.my_app_config.ini", "./app_config.txt"] ignore_unknown_config_file_keys: If true, settings that are found in a config file but don't correspond to any defined configargparse args will be ignored. If false, they will be processed and appended to the commandline like other args, and can be retrieved using parse_known_args() instead of parse_args() config_file_parser_class: configargparse.ConfigFileParser subclass which determines the config file format. configargparse comes with DefaultConfigFileParser and YAMLConfigFileParser. args_for_setting_config_path: A list of one or more command line args to be used for specifying the config file path (eg. ["-c", "--config-file"]). Default: [] config_arg_is_required: When args_for_setting_config_path is set, set this to True to always require users to provide a config path. config_arg_help_message: the help message to use for the args listed in args_for_setting_config_path. args_for_writing_out_config_file: A list of one or more command line args to use for specifying a config file output path. If provided, these args cause configargparse to write out a config file with settings based on the other provided commandline args, environment variants and defaults, and then to exit. (eg. ["-w", "--write-out-config-file"]). Default: [] write_out_config_file_arg_help_message: The help message to use for the args in args_for_writing_out_config_file. NrRT)destZrequiredhelpis_config_file_argZ"write_out_config_file_to_this_pathZCONFIG_OUTPUT_PATH)rTmetavarrUis_write_out_config_file_arg) _add_config_file_help_add_env_var_help_auto_env_var_prefixr r __init__r#_config_file_parser_default_config_files _ignore_unknown_config_file_keys add_argument) rZadd_config_file_helpZadd_env_var_helpZauto_env_var_prefixdefault_config_filesZignore_unknown_config_file_keysZconfig_file_parser_classZargs_for_setting_config_pathZconfig_arg_is_requiredZconfig_arg_help_messageZ args_for_writing_out_config_fileZ&write_out_config_file_arg_help_messagerrrrr\ s&B  zArgumentParser.__init__cCs2|j||||d\}}|r.|jddj||S)aTSupports all the same args as the ArgumentParser.parse_args(..), as well as the following additional args. Additional Args: args: a list of args as in argparse, or a string (eg. "-x -y bla") config_file_contents: String. Used for testing. env_vars: Dictionary. Used for testing. )args namespaceconfig_file_contentsenv_varszunrecognized arguments: %s )parse_known_argserrorrA)rrbrcrdreargvrrr parse_argshs  zArgumentParser.parse_argscsdkrtjddnttr,jntt}xVD]N}|r|djkrd|kr|jdd\}}|j||j|q@|j|q@W|xjD]} | j | _ qWt _ rdtf} d| ij t <jdk rdxjD]v} j| } | r| jp4| j p4| jp4| jp4t| tjp4t| tj r| djj} j| jddj| _qWg} fdd jD}xp|D]h}|j}|}|jst|tjrtjd |}|rd d |jdjd D}| j|||7} qW| | rt fd d |Dj t<t ddjD}|oDdkpDdk}t!fddjD}g}|dk rt"|}d|_#|g}n|sj$}x"t%|D]}zDyj&j'|}Wn.t(k r}zj)|WYdd}~XnXWdt*|dr|j+Xg}x|j,D]\}}||kr6||}t-|j }nd}j.pRt-j/|g}|s|j|||7}dt0|j#f}|j krt j |<||fj ||<qW|qWt }xjD]}|j p|jt1t2gk}t-|j s| s|j3dks|j3t4kst|t5r"qn,|j r6|j d}n|j6}|t|j3f||<qW|rd|j t7<tj8j9d\}fdd jD}dd |D}j:|dd|fS)aTSupports all the same args as the ArgumentParser.parse_args(..), as well as the following additional args. Additional Args: args: a list of args as in argparse, or a string (eg. "-x -y bla") config_file_contents: String. Used for testing. env_vars: Dictionary. Used for testing. Nr2r=-_cs6g|].}|j r|jr|jkrt|j r|qSr)is_positional_argenv_varalready_on_command_lineoption_strings)r/a)rbrerrr1sz3ArgumentParser.parse_known_args..z\[(.*)\]cSsg|]}|jr|jqSr)r.)r/valrrrr1sr3cs g|]}|j||jffqSr)rp)r/rs)rerrr1scss|]}t|tjr|VqdS)N)r?r _HelpAction)r/rsrrr sz2ArgumentParser.parse_known_args..z-hz--helpc3s&|]}j|D]}||fVqqdS)N)get_possible_config_keys)r/actionZ config_key)rrrrvsz method argclosez%s|%s)rbrccs&g|]}t|ddrt|jdqS)rXFN)r<rT)r/rs)rcrrr1scSsg|]}|dk r|qS)Nr)r/rsrrrr1sT) exit_afterr4);sysrir?rCr;r@ prefix_charsappend_actionsrrror_source_to_settings_COMMAND_LINE_SOURCE_KEYr[rwrprVrXr Z_VersionActionrur.replaceuppernargs _AppendActionr7r8r9 convert_item_to_command_line_arg_ENV_VAR_SOURCE_KEYanyrLrr_open_config_filesreversedr]rr"rhhasattrryr rqr_4get_command_line_key_for_unknown_config_file_setting_CONFIG_FILE_SOURCE_KEYOPTIONAL ZERO_OR_MORErSUPPRESS#ACTION_TYPES_THAT_DONT_NEED_A_VALUErT_DEFAULTS_SOURCE_KEYr rgwrite_config_file)rrbrcrdreZnormalized_argsargr*r,rsZa_v_pairconfig_file_keysZstripped_config_file_keyZ env_var_argsZactions_with_env_var_valuesrxZelement_captureZsupports_help_argZskip_config_file_parsingZknown_config_keysZconfig_streamsr config_itemsrNZ config_argsZdiscard_this_keyZ source_keyZdefault_settingsZcares_about_default_valueZ unknown_argsoutput_file_pathsr)rbrercrrrg{s                               zArgumentParser.parse_known_argsc CsxZ|D]R}yt|d}WdQRXWqtk rV}ztd||fWYdd}~XqXqW|r|j|j|}|jj|}x,|D]$}t|d}|j|WdQRXqWddj|} |r|j d| nt | dS)a4Write the given settings to output files. Args: parsed_namespace: namespace object created within parse_known_args() output_file_paths: any number of file paths to write the config to exit_after: whether to exit the program after writing the config files wNz Couldn't open %s for writing: %szWrote config file to z, r) openIOErrorr get_items_for_config_file_outputrr]r!rDrAexitprint) rparsed_namespacerrzZoutput_file_pathZ output_filerNrZ file_contentsmessagerrrrs$  "    z ArgumentParser.write_config_filecCs"|j|j}|jdd|}|S)aCompute a commandline arg key to be used for a config file setting that doesn't correspond to any defined configargparse arg (and so doesn't have a user-specified commandline arg key). Args: key: The config file key that was being set. rr)r.r|)rr*Zkey_without_prefix_charscommand_line_keyrrrr=s zCArgumentParser.get_command_line_key_for_unknown_config_file_settingc Cst}xt|jD]f\}}|tkr|d\}}xh|jD]^}|j|} | r8|j r8t||jr8t||j d} | dk r8t | t rt | j } | || d<q8Wq|tkrx|jD]<\} \}} |j|} | rt||j d} | dk r| || d<qWq|jtr"x||jD]\} \}} | || <qWq|tkrxN|jD]B\} \}} |j|} | r4t||j d} | dk r4| || d<q4WqW|S)aConverts the given settings back to a dictionary that can be passed to ConfigFormatParser.serialize(..). Args: source_to_settings: the dictionary described in parse_known_args() parsed_namespace: namespace object created within parse_known_args() Returns: an OrderedDict where keys are strings and values are either strings or lists rlNr)rr rr~rwrorqrrr<rTr?boolrClowerrr6rr) rZsource_to_settingsrZconfig_file_itemssourcesettingsrnZexisting_command_line_argsrxrr,r*rrrrJs@           z/ArgumentParser.get_items_for_config_file_outputcCs\g}|dkr|j|}n |jd}|dk rnt|trn|jdkrL|j|n |jdkrZn|jd ||fnt|tr"|dkst|tj rx|D]}|j||jt |qWnjt|tj r|j dkst|j t o|j dkr|j|x0|D]}|jt |qWn|jd ||fn6t|t rD|j||j|ntd t||f|S)aConverts a config file or env var key + value to a list of commandline args to append to the commandline. Args: action: The argparse Action object for this setting, or None if this config file setting doesn't correspond to any defined configargparse arg. key: string (config file key or env var name) value: parsed value of type string or list Nr2r+yes1falseno0zQUnexpected value for %s: '%s'. Expecting 'true', 'false', 'yes', 'no', '1' or '0'+*zp%s can't be set to a list '%s' unless its action type is changed to 'append' or nargs is set to '*', '+', or > 1z&Unexpected value type %s for value: %sr4)r+rr)rrr)rr)rrrr?rrr}rhr@r rrC _StoreActionrintr rM)rrxr*r,rbrZ list_elemrrrrxs:              z/ArgumentParser.convert_item_to_command_line_argcsTg}t|ddr|Sx:|jD]0tfdd|jDr|ddg7}qW|S)zThis method decides which actions can be set in a config file and what their keys will be. It returns a list of 0 or more config keys that can be used to set the given action's value in a config file. rXNcsg|]}jd|qS)r)r6)r/c)rrrr1sz;ArgumentParser.get_possible_config_keys..r)r<rrrr|)rrxkeysr)rrrws  z'ArgumentParser.get_possible_config_keysc Csddttjttjj|jD}dd|jD}|s:|Sx|D]}tj|j dd}|j |dd}t j |||_ |j|d}|sq@|\}} t||jd } | sq@tjj| } tjj| s|j d | |t| g7}q@W|S) ajTries to parse config file path(s) from within command_line_args. Returns a list of opened config files, including files specified on the commandline as well as any default_config_files specified in the constructor that are present on disk. Args: command_line_args: List of all args (already split on spaces) cSsg|]}|D] }t|q qSr)r)r/filesfrrrr1sz5ArgumentParser._open_config_files..cSsg|]}t|ddr|qS)rVF)r<)r/rsrrrr1sF)r|Zadd_helpcSsdS)Nr)rrrrr error_methodsz7ArgumentParser._open_config_files..error_method)rbNzFile not found: %s)rBglobospath expanduserr^r~r r r|Z _add_actiontypes MethodTyperhrgr<rTisfiler) rZcommand_line_argsZ config_filesZuser_config_file_arg_actionsrxZ arg_parserrZ parsed_argrcrnZuser_config_filerrrrs. "     z!ArgumentParser._open_config_filescCstdtdtdtdi}t}x|jjD]\}}|jd}||dt|dd}|j |xl|jD]`\}\}}|r|j d |d |fqht |t r|j d |qht |t rh|j d d j |qhWq&W|jS) zReturns a string with all args and settings and where they came from (eg. commandline, config file, enviroment variable or default) zCommand Line Args: zEnvironment Variables: zConfig File (%s): z Defaults: |rr2Nz %-19s%s :z %s rf)rrrrrrr r;tuplerDr?rCr@rArE)rZsource_key_to_display_value_maprFrrr*rxr,rrr format_valuess"    zArgumentParser.format_valuescCs|j|jdS)zBPrints the format_values() string (to sys.stdout or another file).N)rDr)rfilerrr print_valuesszArgumentParser.print_valuescsd}d}d}jr؈j}djd}fddjD}ddjD}|r|sV|rd_d}|d ||ddf7}d jd d |D}|rd |}|s|r|dd jt|ttd|g7}|djj7}j r:ddjD} xH| D]@\} } d| } | j sd| _ | | j kr| j | 7_ d}d_ qW|sF|r~dg} |r\dg| } |rldg| } |ddj| 7}|rj pdd|_ t j jS)NrlFrrcsBg|]:}|jD].}j|r|jdkp0|jp0|j r||fqqS)rU)rrrwrTrVrX)r/rsr)rrrr1sz.ArgumentParser.format_help..cSsg|]}t|ddr|qS)rVF)r<)r/rsrrrr1sTzCArgs that start with '%s' (eg. %s) can also be set in a config filez or css|]}|jr|jdVqdS)rN)rr)r/rsrrrrv(sz-ArgumentParser.format_help..zspecified via z (%s).rfcSs"g|]}t|ddr|j|fqS)rpN)r<rp)r/rsrrrr12sz [env var: %s]rSzconfig file valueszenvironment variableszT If an arg is specified in more than one place, then commandline values override %s.z which override )rYr^r|r~rArfilterr]rrZrU descriptionr r format_help)rr$Zadded_config_file_helpZadded_env_var_helpraZccZconfig_settable_argsZconfig_path_actionsZconfig_arg_stringZenv_var_actionsrprsZenv_var_help_stringZ value_sourcesr)rrrsX        zArgumentParser.format_help)F)rrrrr#r\renvironrjrgrrrrrwrrr{stdoutrrrrrrr s4 Q#  .37r cOs|jdd}|jddp"|jdd}|jdd}|j||}|j |_||_||_||_|jrj|rjtd|jrt|t j  rtd|jrd}t|t j st|d |rt|d |S) a  This method supports the same args as ArgumentParser.add_argument(..) as well as the additional args below. Additional Args: env_var: If set, the value of this environment variable will override any config file or default values for this arg (but can itself be overriden on the commandline). Also, if auto_env_var_prefix is set in the constructor, this env var name will be used instead of the automatic name. is_config_file_arg: If True, this arg is treated as a config file path This provides an alternative way to specify config files in place of the ArgumentParser(fromfile_prefix_chars=..) mechanism. Default: False is_write_out_config_file_arg: If True, this arg will be treated as a config file path, and, when it is specified, will cause configargparse to write all current commandline args to this file as config options and then exit. Default: False rpNrVZis_config_filerXz*env_var can't be set for a positional arg.z9arg with is_config_file_arg=True must have action='store'z+arg with is_write_out_config_file_arg=True zmust have action='store'z'can't also have is_config_file_arg=True) poporiginal_add_argument_methodrrrorprVrXr r?r r)rrbrrprVrXrxZ error_prefixrrrr`Ls,        r`cstfdd|DS)zsUtility method for checking if any of the potential_command_line_args is already present in existing_args. c3s|]}|kVqdS)Nr)r/Z potential_arg)existing_args_listrrrvsz*already_on_command_line..)r)rZpotential_command_line_argsr)rrrqs rq)rr)rr)N)N)Dr rrr7r{r version_infoiorZ ordereddictr collectionsZ_StoreTrueActionZ_StoreFalseActionZ _CountActionZ_StoreConstActionZ_AppendConstActionrrrrr ZRawTextHelpFormatterZRawDescriptionHelpFormatterrobjectrrKr"r#rGrrrrr r`rqZ_ActionsContainerrZ HelpFormatterZ ArgumentErrorZArgumentTypeErrorZActionZFileTypeZ NamespaceZ ONE_OR_MORErZ REMAINDERrrZinitArgumentParserZgetArgumentParserZ getArgParserZ getParserZget_arg_parserZ get_parserZ ArgParserZParserZadd_argaddrjrrgZ parse_knownZ RawFormatterZDefaultsFormatterZDefaultsRawFormatterrrrrs        (Q?I6