pydrobert.kaldi.io.argparse

Contains a custom ArgumentParser, KaldiParser, and a number of arg types

class pydrobert.kaldi.io.argparse.KaldiParser(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, add_verbose=True, add_config=True, update_formatters=True, add_print_args=True, logger=None, version=None)[source]

Bases: ArgumentParser

Kaldi-compatible wrapper for argument parsing

KaldiParser intends to make command-line entry points in python more compatible with kaldi command-line scripts. It makes the following changes to argparse.ArgumentParser:

  1. Creates a logging.Formatter instance that formats messages similarly to kaldi using the prog keyword as the program name.

  2. Sets the default help and usage locations to sys.stderr (instead of sys.stdout)

  3. Registers 'kaldi_bool', 'kaldi_rspecifier', 'kaldi_wspecifier', 'kaldi_wxfilename', 'kaldi_rxfilename', 'kaldi_config', 'kaldi_dtype', and 'numpy_dtype' as argument types

  4. Registers 'kaldi_verbose' as an action

  5. Adds logger, update_formatters, add_config, and add_verbose parameters to initialization (see below)

  6. Wraps parse_args and parse_known_args with kaldi_vlog_level_cmd_decorator (so loggers use the right level names on error)

KaldiParser differs from kaldi’s command line parsing in a few key ways. First, though ‘=’ syntax is supported, the parser will also group using the command-line splitting (on unquoted whitespace). For the KaldiParser, --foo bar and --foo=bar are equivalent (assuming foo takes one optional argument), whereas, in Kaldi, --foo bar would be parsed as the boolean flag --foo followed by a positional with value bar. This ambiguity is the source of the next difference: boolean flags. Because kaldi command-line parsing splits around =, it can use --foo=true and --foo interchangeably. To avoid gobbling up a positional argument, KaldiParser allows for only one type of boolean flag syntax. For the former, use action='store_true' in add_argument. For the latter, use type='kaldi_bool'.

Parameters
  • prog (Optional[str]) – Name of the program. Defaults to sys.argv[0]

  • usage (Optional[str]) – A usage message. Default: auto-generated from arguments

  • description (Optional[str]) – A description of what the program does

  • epilog (Optional[str]) – Text following the argument descriptions

  • parents (Sequence[ArgumentParser]) – Parsers whose arguments should be copied into this one

  • formatter_class (type) – Class for printing help messages

  • prefix_chars (str) – Characters that prefix optional arguments

  • fromfile_prefix_chars (Optional[str]) – Characters that prefix files containing additional arguments

  • argument_default (Optional[Any]) – The default value for all arguments

  • conflict_handler (str) – String indicating how to handle conflicts

  • add_help (bool) – Add a -h/--help option

  • add_verbose (bool) – Add a -v/--verbose option. The option requires an integer argument specifying a verbosiy level at the same degrees as Kaldi. The level will be converted to the appropriate python level when parsed

  • add_config (bool) – Whether to add the standard --config option to the parser. If True, a first-pass will extract all config file options and put them at the beginning of the argument string to be re-parsed.

  • add_print_args (bool) – Whether to add the standard --print-args to the parser. If True, a first-pass of the will search for the value of --print-args and, if True, will print that value to stderr (only on parse_args, not parse_known_args)

  • update_formatters (bool) – If logger is set, the logger’s handlers’ formatters will be set to a kaldi-style formatter

  • logger (Optional[Logger]) – Errors will be written to this logger when parse_args fails. If add_verbose has been set to True, the logger will be set to the appropriate python level if verbose is set (note: the logger will be set to the default level - INFO - on initialization)

  • version (Optional[str]) – A version string to use for logs. If not set, pydrobert.kaldi.__version__ will be used by default

logger

The logger this parse was printing out to

formatter

A log formatter that formats with kaldi-style headers

add_config

Whether this parser has a --config flag

add_print_args

Whether this parser has a --print-args flag

version

Version string used by this parser and logger

error(message)[source]

Prints a usage message incorporating the message to stderr and exits.

If you override this in a subclass, it should not return – it should either exit or raise an exception.

parse_known_args(**kwargs)
print_help(file=None)[source]
print_usage(file=None)[source]
class pydrobert.kaldi.io.argparse.KaldiVerbosityAction(option_strings, dest, default=20, required=False, help='Verbose level (higher->more logging)', metavar=None)[source]

Bases: Action

Read kaldi-style verbosity levels, setting logger to python level

Kaldi verbosities tend to range from [-3, 9]. This action takes in a kaldi verbosity level and converts it to python logging levels with pydrobert.kaldi.logging.kaldi_lvl_to_logging_lvl()

If the parser has a logger attribute, the logger will be set to the new level.

pydrobert.kaldi.io.argparse.kaldi_bool_arg_type(string)[source]

argument type for bool strings of “true”,”t”,”false”, or “f”

pydrobert.kaldi.io.argparse.kaldi_config_arg_type(string)[source]

Encapsulate parse_kaldi_config_file as an argument type

pydrobert.kaldi.io.argparse.kaldi_dtype_arg_type(string)[source]

argument type for string reps of KaldiDataType

pydrobert.kaldi.io.argparse.kaldi_rspecifier_arg_type(string)[source]

argument type to make sure string is a valid rspecifier

pydrobert.kaldi.io.argparse.kaldi_rxfilename_arg_type(string)[source]

argument type to make sure string is a valid extended readable file

pydrobert.kaldi.io.argparse.kaldi_wspecifier_arg_type(string)[source]

argument type to make sure string is a valid wspecifier

pydrobert.kaldi.io.argparse.kaldi_wxfilename_arg_type(string)[source]

argument type to make sure string is a valid extended readable file

pydrobert.kaldi.io.argparse.numpy_dtype_arg_type(string)[source]

argument type for string reps of numpy dtypes

pydrobert.kaldi.io.argparse.parse_kaldi_config_file(file_path, allow_space=True)[source]

Return a list of arguments from a kaldi config file

Parameters
  • file_path (str) – Points to the config file in question

  • allow_spaces (bool, optional) – If True, treat the first space on a line as splitting key and value if no equals sign exists on the line. If False, no equals sign will chunk the whole line (as if a boolean flag). Kaldi does not split on spaces, but python does. Note that allow_spaces does not split the entire line on spaces, unlike shell arguments.