saltshaker.config.configparsing
Configuration parsing utilities for SALT3 training.
This module provides enhanced argument parsing that combines configuration files with command-line overrides, supporting environment variable expansion and type conversion.
Classes
- FullPaths
Argparse action that expands environment variables and ~ in paths.
- EnvAwareArgumentParser
ArgumentParser subclass with environment variable expansion.
- ConfigWithCommandLineOverrideParser
Parser that reads defaults from config files with CLI override support.
Functions
- boolean_string
Convert string to boolean for argparse.
- nonetype_or_int
Convert string to None or int for argparse.
- generateerrortolerantaddmethod
Wrapper for error-tolerant argument adding.
Functions
Convert string to boolean for argparse type conversion. |
|
Expand environment variables and ~ in comma-separated paths. |
|
|
Create error-tolerant wrapper for add_argument_with_config_default. |
Convert string to None or int for argparse type conversion. |
Classes
|
Parser combining config file defaults with command-line overrides. |
|
ArgumentParser subclass that expands environment variables in defaults. |
|
Argparse action that expands environment variables and ~ in path arguments. |
- saltshaker.config.configparsing.expandvariablesandhomecommaseparated(paths)
Expand environment variables and ~ in comma-separated paths.
- class saltshaker.config.configparsing.FullPaths(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)
Bases:
ActionArgparse action that expands environment variables and ~ in path arguments.
Use with
action=FullPathsin add_argument().
- class saltshaker.config.configparsing.EnvAwareArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)
Bases:
ArgumentParserArgumentParser subclass that expands environment variables in defaults.
Automatically expands $VAR and ~ in default values for arguments using the FullPaths action.
- add_argument(dest, ..., name=value, ...)
- add_argument(option_string, option_string, ..., name=value, ...)
- class saltshaker.config.configparsing.ConfigWithCommandLineOverrideParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)
Bases:
EnvAwareArgumentParserParser combining config file defaults with command-line overrides.
Reads default values from a ConfigParser object, allowing command-line arguments to override them. Supports multiple key aliases and automatic type conversion.
Examples
>>> config = configparser.ConfigParser() >>> config.read('training.conf') >>> parser = ConfigWithCommandLineOverrideParser() >>> parser.add_argument_with_config_default( ... config, 'trainparams', 'maxiter', type=int)
- addhelp()
- add_argument_with_config_default(config, section, *keys, **kwargs)
Add argument with default from config file.
- Parameters:
config (ConfigParser) – Configuration parser with loaded config file.
section (str) – Section name in config file.
*keys (str) – One or more key names to look for in the section.
**kwargs – Additional arguments passed to add_argument(). Special kwargs include
clargformat(str), which sets the format for CLI argument name (default: ‘–{key}’, use ‘prependsection’ for ‘–{section}_{key}’).
- Raises:
KeyError – If no matching key found and no default provided.
- saltshaker.config.configparsing.boolean_string(s)
Convert string to boolean for argparse type conversion.
- Parameters:
s (str) – String value (‘True’, ‘False’, ‘true’, ‘false’, ‘1’, ‘0’).
- Returns:
Converted boolean value.
- Return type:
- Raises:
ValueError – If string is not a valid boolean representation.
- saltshaker.config.configparsing.nonetype_or_int(s)
Convert string to None or int for argparse type conversion.
- saltshaker.config.configparsing.generateerrortolerantaddmethod(parser)
Create error-tolerant wrapper for add_argument_with_config_default.
Returns a function that catches exceptions when adding arguments, logging errors instead of raising them.
- Parameters:
parser (ConfigWithCommandLineOverrideParser) – Parser to wrap.
- Returns:
Wrapped add method that returns True on success, False on error.
- Return type:
callable