a <`^@sddlZddlZddlZddlZddlZddlZddlmZejdkrTddl m Z n ddl m Z ej ej ej ejejfZiZdddZd ddZGd d d ejejejZGd d d eZGd 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) OrderedDict)r)StringIOcKsX|dur d}|tvr(td|t|f|dtj|ddtfi|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 setdefaultargparseArgumentDefaultsHelpFormatterArgumentParsernamekwargsr0/usr/lib/python3/dist-packages/configargparse.pyinit_argument_parsers rcKs8|dur d}t|dks |tvr0t|fi|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)lenrrr rrrget_argument_parser*s rc@seZdZdZdS) ArgumentDefaultsRawHelpFormatterzCHelpFormatter that adds default values AND doesn't do line-wrappingN__name__ __module__ __qualname____doc__rrrrr9src@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 implementedNNotImplementedErrorselfrrrget_syntax_descriptionEsz'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 implementedNr)rstreamrrrparseIszConfigFileParser.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 implementedNr)ritemsrrr serializeZs zConfigFileParser.serializeNrrrrrr!r#rrrrrAsrc@seZdZdZdS)ConfigFileParserExceptionz'Raised when config file parsing failed.Nrrrrrr%isr%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).rrmsgrrrrsz.DefaultConfigFileParser.get_syntax_descriptionc Cst}t|D]\}}|}|r|ddvs|dr[^:=;#\s]+?)z[:=\s]z(?P.+?)z(?P\s[;#].*)?^$keytruevaluer+]cSsg|] }|qSrstrip).0elemrrr z1DefaultConfigFileParser.parse..,zUnexpected line {} in {}: {}r r ) r enumerater3 startswithrematchgroupendswithsplitr%formatgetattr) rr r"ilineZ white_spacer.r0ZcommentZkey_only_matchZkey_value_matchrrrr!s4    zDefaultConfigFileParser.parsecCsTt}|D]<\}}t|tr8ddtt|d}|d||q| S)Does the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents. r+, r1z{} = {} ) rr" isinstancelistjoinmapstrwriterBgetvalue)rr"rr.r0rrrr#s  z!DefaultConfigFileParser.serializeNr$rrrrr&ms#r&c@s(eZdZdZddZddZddZdS) ConfigparserConfigFileParserz,parses INI files using pythons configparser.cCsd}|S)NaUses configparser module to parse an INI file which allows multi-line values. Allowed syntax is that for a ConfigParser with the following options: allow_no_value = False, inline_comment_prefixes = ("#",) strict = True empty_lines_in_values = False See https://docs.python.org/3/library/configparser.html for details. Note: INI file sections names are still treated as comments. rr'rrrrsz3ConfigparserConfigFileParser.get_syntax_descriptionc Csddl}ddlm}|jddddddd}z||Wn0tyl}ztd |WYd}~n d}~00t}| D]t}|| D]b\}} | d d  d d } d | vrd| vr| d d} d| vr|| ||<q| ||<q| ||<qq||S)z3Parses the keys and values from an INI config file.rN) literal_eval)=:F)r)r*T)Z delimitersallow_no_valueZcomment_prefixesinline_comment_prefixesstrictempty_lines_in_valuesCouldn't parse config file: %s   r+r1{) configparserastrQ ConfigParserZ read_stringread Exceptionr%rZsectionsr"replacerA) rr r]rQconfigeresultZsectionkvZmultiLine2SingleLineZprelist_stringrrrr!s2 "   z"ConfigparserConfigFileParser.parsecCsXddl}ddl}|jddddd}d|i}|||}|||d|S)rFrNF)r)T)rTrUrVrWZDEFAULT)r]ior_Z read_dictrrMseekr`)rr"r]rhrcr rrrr#s   z&ConfigparserConfigFileParser.serializeNr$rrrrrPs"rPc@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).rr'rrrr sz+YAMLConfigFileParser.get_syntax_descriptioncCs,z ddl}Wnty&tdYn0|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%)rrkrrr _load_yamls   zYAMLConfigFileParser._load_yamlc Cs|}z||}Wn0tyF}ztd|WYd}~n d}~00t|tsptdt|ddt|jft }| D](\}}t|t r|||<q~t |||<q~|S)z.Parses the keys and values from a config file.rXNzThe config file doesn't appear to contain 'key: value' pairs (aka. a YAML mapping). yaml.load('%s') returned type '%s' instead of 'dict'.r r ) rmZ safe_loadrar%rHdictrCtyperrr"rIrL)rr rkZ parsed_objrdrer.r0rrrr!s "   zYAMLConfigFileParser.parseFcCs|}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)rmrndump)rr"rprkrrrr#5s zYAMLConfigFileParser.serializeN)F)rrrrrrmr!r#rrrrrjs  rjZ command_lineZenvironment_variables config_filedefaultsc@seZdZdZddZdddejfddZdddejfddZdd d Z d d Z ddZ ddZ ddZ ddZddZejfddZddZdS)r zDrop-in replacement for argparse.ArgumentParser that adds support for environment variables and .ini or .yaml-style config files. cOs|dd}|dd}|dd}|dg}|dd}|d t}|d g} |d d} |d d } |dg} |dd} |dt|_||_||_||_tjj |g|Ri||durt|_ n||_ ||_ ||_ | r|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_open_func: function used to open a config file for reading or writing. Needs to return a file-like object. 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. add_config_file_helpTadd_env_var_helpauto_env_var_prefixNdefault_config_filesignore_unknown_config_file_keysFconfig_file_parser_classargs_for_setting_config_pathconfig_arg_is_requiredconfig_arg_help_messagezconfig file path args_for_writing_out_config_file&write_out_config_file_arg_help_messagezftakes the current command line args and writes them out to a config file at the given path, then exitsZconfig_file_open_funcrr)destZrequiredhelpis_config_file_argZ"write_out_config_file_to_this_pathZCONFIG_OUTPUT_PATH)rmetavarris_write_out_config_file_arg)popr&open_config_file_open_func_add_config_file_help_add_env_var_help_auto_env_var_prefixr r __init___config_file_parser_default_config_files _ignore_unknown_config_file_keys add_argument)rargsrrtrurvrwrxryrzr{r|r}r~rrrrQsZ8      zArgumentParser.__init__NcCs2|j||||d\}}|r.|dd||S)TSupports 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. )r namespaceconfig_file_contentsenv_varszunrecognized arguments: %srZ)parse_known_argserrorrJ)rrrrrargvrrr parse_argss  zArgumentParser.parse_argsc sdurtjddnttr,ntjD]}|j |_q:t _ rpdtf}d|ij t <j durjD]j} |}|r|js|js|js|jst|tjst|tjs|dj}j |dd|_qg} d} fdd jD} | D]l} | j} | }| js8t| tjrhd } td |}|rhd d |dd D}| | | |7} q| r| n| | rt fdd | Dj t<tddjD}|odvpdv}fddjD}g}|durt |}d|_!|g}n|s,"}t#|D]b}zZzj$%|}Wn0t&y|}z'|WYd}~n d}~00Wt(|dr|)nt(|dr|)0g}d} |*D]\} }| |vr|| } t+| jj}n"d} j,pt+-| gj}|s|| | |7}dt.|j!f}|j vrHt j |<| |fj || <| rh| jsvt| tjrd } q| r|n|q4t }jD]} | j p| jt/t0fv}t+| jjs|r| j1dus| j1t2kst| t3rqn,| jr| jd} n| j4} | t| j1f|| <q|rD|j t5<tj6j7d\}fdd jD}dd |D}j8|d d|fS)rNr8r-_Fcs6g|].}|js|jr|jvrt|jjs|qSr)is_positional_argenv_varalready_on_command_lineoption_strings prefix_charsr4a)rrrrrr6sz3ArgumentParser.parse_known_args..Tz\[(.*)\]cSsg|]}|r|qSrr2)r4valrrrr6r7r:cs g|]}|j||jffqSr)rr)rrrr6scss|]}t|tjr|VqdSN)rHr _HelpActionrrrr sz2ArgumentParser.parse_known_args..z-hz--helpcs"i|]}|D] }||qqSr)get_possible_config_keys)r4actionZ config_keyrrr sz3ArgumentParser.parse_known_args..z method argclosez%s|%sr9)rrcs&g|]}t|ddrt|jdqS)rFN)rCrr)rrrr6is cSsg|]}|dur|qSrrrrrrr6kr7) exit_after)9sysrrHrLrArI_actionsrrr_source_to_settings_COMMAND_LINE_SOURCE_KEYrrrrrr Z_VersionActionrr3rrbuppernargs _AppendActionr=r>r? convert_item_to_command_line_arg_ENV_VAR_SOURCE_KEYanyrr _open_config_filesreversedrr!r%rhasattrrr"rr4get_command_line_key_for_unknown_config_file_setting_CONFIG_FILE_SOURCE_KEYOPTIONAL ZERO_OR_MORErSUPPRESS#ACTION_TYPES_THAT_DONT_NEED_A_VALUEr_DEFAULTS_SOURCE_KEYr rwrite_config_file)rrrrrrZa_v_pairconfig_file_keysZstripped_config_file_keyZ env_var_argsrZactions_with_env_var_valuesrr.r0Zelement_captureZsupports_help_argZskip_config_file_parsingZknown_config_keysZconfig_streamsr config_itemsrdZ config_argsZdiscard_this_keyZ source_keyZdefault_settingsZcares_about_default_valueZ unknown_argsoutput_file_pathsr)rrrrrrs                   "                 zArgumentParser.parse_known_argsFc Cs|D]j}z0||d}Wdn1s,0YWqtyl}ztd||WYd}~qd}~00q|r||j|}|j|}|D]:}||d}||Wdq1s0Yqdd |} |r| 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 {} for writing: {}zWrote config file to rGr) rIOErrorrrB get_items_for_config_file_outputrrr#rMrJexitprint) rparsed_namespacerrZoutput_file_pathZ output_filerdrZ file_contentsmessagerrrros(" *z ArgumentParser.write_config_filecCs"||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. r)r3r)rr.Zkey_without_prefix_charscommand_line_keyrrrrs zCArgumentParser.get_command_line_key_for_unknown_config_file_settingc Cslt}|D]V\}}|tkr|d\}}|jD]`}||} | r2|js2t||j|jr2t ||j d} | dur2t | t rt | } | || d<q2q|tkr|D]<\} \}} ||} | rt ||j d} | dur| || d<qq|tr|D]\} \}} | || <qq|tkr|D]B\} \}} ||} | r"t ||j d} | dur"| || d<q"q|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 rNr)rr"rrrrrrrrCrrHboolrLlowerrr<rr) rZsource_to_settingsrZconfig_file_itemssourcesettingsrZexisting_command_line_argsrrr0r.rrrrsF            z/ArgumentParser.get_items_for_config_file_outputc Csg}|dur||}n |jd}|durpt|trp|dvrL||n |dvrZn|d||fn:t|trv|duo|jduot|t j st|t j o|jdvpt|jt o|jdk}|dust|t j r6|D]R}|rt|tr|||D]}|t |qq|d|t |fqn>|rb|||D]}|t |qJn|d ||fn4t|t r|d||fntd t|||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 Nr9)r/Zyes1)ZfalseZno0zQUnexpected value for %s: '%s'. Expecting 'true', 'false', 'yes', 'no', '1' or '0')+*r8z%s=%szp%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 {} for value: {})rrrHrrappendrrIrr _StoreActionrintrLrrBro) rrr.r0rrZaccepts_list_and_has_nargsZ list_elemZsub_elemrrrrsN          z/ArgumentParser.convert_item_to_command_line_argcsPg}t|ddr|S|jD]0tfdd|jDr|ddg7}q|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. rNc3s|]}d|VqdS)rN)r<)r4cargrrrr7z:ArgumentParser.get_possible_config_keys..r)rCrrr)rrkeysrrrrs  z'ArgumentParser.get_possible_config_keysc Csg}ttjttjj|jD]}|D]}|||q$qdd|jD}|sT|S|D]}t j |j dd}| |dd}t |||_|j|d} | sqX| \} } t| |jd} | sqXtj| } tj| s|d | ||| g7}qX|S) aPTries 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 cSsg|]}t|ddr|qSrFrCrrrrr6(sz5ArgumentParser._open_config_files..F)rZadd_helpcSsdSrr)rrrrr error_method:sz7ArgumentParser._open_config_files..error_method)rNzFile not found: %s)rKglobospath expanduserrrrrr r rZ _add_actiontypes MethodTyperrrCrisfile) rZcommand_line_argsZ config_filesfilesfZuser_config_file_arg_actionsrZ arg_parserrZ parsed_argrrZuser_config_filerrrrs:     z!ArgumentParser._open_config_filescCstdtdtdtdi}t}|jD]\}}|d}||dt|dd}| ||D]`\}\}}|r| d |d |qdt |t r| d |qdt |t rd| d d |qdq$|S) 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: |rr8Nz {:<19}{} rSz %s rZ)rrrrrrr"rAtuplerMrBrHrLrIrJrN)rZsource_key_to_display_value_maprOrrr.rr0rrr format_valuesPs$    zArgumentParser.format_valuescCs||dS)zBPrints the format_values() string (to sys.stdout or another file).N)rMr)rfilerrr print_valueskszArgumentParser.print_valuesc sd}d}d}jr؈j}djd}fddjD}ddjD}|r|sV|rd_d}|d ||ddf7}d d d |D}|rd |}|s|r|dd t|ttd|g7}|dj7}j rDddjD} | D]N\} } | j t kr qd| } | j s d| _ | | j vr| j | 7_ d}d_ q|sP|rdg} |rfdg| } |rvdg| } |dd| 7}|rj pdd|_ t jS)NrFrrcs@g|]8}|jD],}|r|jdks|js|js||fqqS)r)rrrrr)r4rrrrrr6vs  z.ArgumentParser.format_help..cSsg|]}t|ddr|qSrrrrrrr6zs TzCArgs that start with '%s' (eg. %s) can also be set in a config filez or css|]}|jr|jdVqdS)rN)rrrrrrsz-ArgumentParser.format_help..zspecified via z (%s).rZcSs"g|]}t|ddr|j|fqS)rN)rCrrrrrr6s z [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 )rrrrrJrfilterrrrrrZ descriptionr r format_help)rr(Zadded_config_file_helpZadded_env_var_helprwZccZconfig_settable_argsZconfig_path_actionsZconfig_arg_stringZenv_var_actionsrrZenv_var_help_stringZ value_sourcesrrrrosh        zArgumentParser.format_help)F)rrrrrrenvironrrrrrrrrrrstdoutrrrrrrr Ls$h  $  /;9r cOs|dd}|ddp"|dd}|dd}|j|i|}|j |_||_||_||_|jrn|rntd|jrt|t j std|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 rNrZis_config_filerz*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) roriginal_add_argument_methodrrrrrrrHr r)rrrrrrrZ error_prefixrrrrs6      rcsbg|D]B}|r@|d|vr@d|vr@|dd\}}|q|qtfdd|DS)zsUtility method for checking if any of the potential_command_line_args is already present in existing_args. rrRr8c3s|]}|vVqdSrr)r4Z potential_arg arg_namesrrrsz*already_on_command_line..)rArr)Zexisting_args_listZpotential_command_line_argsrZ arg_stringZ option_stringZ explicit_argrrrrs   r)N)N)Dr rrr=rr collectionsr version_inforhrZ_StoreTrueActionZ_StoreFalseActionZ _CountActionZ_StoreConstActionZ_AppendConstActionrrrrr ZRawTextHelpFormatterZRawDescriptionHelpFormatterrobjectrrar%r&rPrjrrrrr rrZ_ActionsContainerrZ HelpFormatterZ ArgumentErrorZArgumentTypeErrorZActionZFileTypeZ NamespaceZ ONE_OR_MORErZ REMAINDERrrZinitArgumentParserZgetArgumentParserZ getArgParserZ getParserZget_arg_parserZ get_parserZ ArgParserZParserZadd_argaddrr!rZ parse_knownZ RawFormatterZDefaultsFormatterZDefaultsRawFormatterrrrrs      (QI?b6