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

boolean_string(s)

Convert string to boolean for argparse type conversion.

expandvariablesandhomecommaseparated(paths)

Expand environment variables and ~ in comma-separated paths.

generateerrortolerantaddmethod(parser)

Create error-tolerant wrapper for add_argument_with_config_default.

nonetype_or_int(s)

Convert string to None or int for argparse type conversion.

Classes

ConfigWithCommandLineOverrideParser([prog, ...])

Parser combining config file defaults with command-line overrides.

EnvAwareArgumentParser([prog, usage, ...])

ArgumentParser subclass that expands environment variables in defaults.

FullPaths(option_strings, dest[, nargs, ...])

Argparse action that expands environment variables and ~ in path arguments.

saltshaker.config.configparsing.expandvariablesandhomecommaseparated(paths)

Expand environment variables and ~ in comma-separated paths.

Parameters:

paths (str) – Comma-separated list of paths.

Returns:

Comma-separated paths with variables expanded.

Return type:

str

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: Action

Argparse action that expands environment variables and ~ in path arguments.

Use with action=FullPaths in 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: ArgumentParser

ArgumentParser 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: EnvAwareArgumentParser

Parser 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:

bool

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.

Parameters:

s (str) – String value (‘None’ or integer string).

Returns:

None if s == ‘None’, otherwise int(s).

Return type:

None or int

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