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:
ArgumentParserKaldi-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:Creates a
logging.Formatterinstance that formats messages similarly to kaldi using the prog keyword as the program name.Sets the default help and usage locations to
sys.stderr(instead ofsys.stdout)Registers
'kaldi_bool','kaldi_rspecifier','kaldi_wspecifier','kaldi_wxfilename','kaldi_rxfilename','kaldi_config','kaldi_dtype', and'numpy_dtype'as argument typesRegisters
'kaldi_verbose'as an actionAdds logger, update_formatters, add_config, and add_verbose parameters to initialization (see below)
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 barand--foo=barare equivalent (assuming foo takes one optional argument), whereas, in Kaldi,--foo barwould be parsed as the boolean flag--foofollowed by a positional with valuebar. This ambiguity is the source of the next difference: boolean flags. Because kaldi command-line parsing splits around=, it can use--foo=trueand--foointerchangeably. To avoid gobbling up a positional argument,KaldiParserallows for only one type of boolean flag syntax. For the former, useaction='store_true'in add_argument. For the latter, usetype='kaldi_bool'.- Parameters:
prog (
Optional[str]) – Name of the program. Defaults tosys.argv[0]usage (
Optional[str]) – A usage message. Default: auto-generated from argumentsdescription (
Optional[str]) – A description of what the program doesepilog (
Optional[str]) – Text following the argument descriptionsparents (
Sequence[ArgumentParser]) – Parsers whose arguments should be copied into this oneformatter_class (
type) – Class for printing help messagesprefix_chars (
str) – Characters that prefix optional argumentsfromfile_prefix_chars (
Optional[str]) – Characters that prefix files containing additional argumentsargument_default (
Any) – The default value for all argumentsconflict_handler (
str) – String indicating how to handle conflictsadd_help (
bool) – Add a-h/--helpoptionadd_verbose (
bool) – Add a-v/--verboseoption. 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 parsedadd_config (
bool) – Whether to add the standard--configoption to the parser. IfTrue, 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-argsto the parser. IfTrue, a first-pass of the will search for the value of--print-argsand, ifTrue, 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 formatterlogger (
Logger) – Errors will be written to this logger when parse_args fails. If add_verbose has been set toTrue, 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 (
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
--configflag
- add_print_args
Whether this parser has a
--print-argsflag
- 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)
- class pydrobert.kaldi.io.argparse.KaldiVerbosityAction(option_strings, dest, default=20, required=False, help='Verbose level (higher->more logging)', metavar=None)[source]
Bases:
ActionRead 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 questionallow_spaces (
bool, optional) – IfTrue, treat the first space on a line as splitting key and value if no equals sign exists on the line. IfFalse, 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.