pypeit.scripts.scriptbase module

Implements base classes for use with PypeIt scripts.

class pypeit.scripts.scriptbase.ScriptBase[source]

Bases: object

Provides a base class for all scripts.

static _fill_parser_cwd(parser)[source]

Replace the default of any action that is exactly 'current working directory' with the value of os.getcwd().

The parser is edited in place.

Parameters:

parser (argparse.ArgumentParser) – The argument parsing object to edit.

classmethod entry_point()[source]

Defines the main script entry point.

classmethod get_parser(description=None, width=None, formatter=<class 'argparse.ArgumentDefaultsHelpFormatter'>)[source]

Construct the command-line argument parser.

Derived classes should override this. Ideally they should use this base-class method to instantiate the ArgumentParser object and then fill in the relevant parser arguments

Warning

Any argument that defaults to the string 'current working directory' will be replaced by the result of os.getcwd() when the script is executed. This means help dialogs will include this replacement, and parsing of the command line will use os.getcwd() as the default. This functionality is largely to allow for PypeIt’s automated documentation of script help dialogs without the “current working” directory being that of the developer that most recently compiled the docs.

Parameters:
  • description (str, optional) – A short description of the purpose of the script.

  • width (int, optional) – Restrict the width of the formatted help output to be no longer than this number of characters, if possible given the help formatter. If None, the width is the same as the terminal width.

  • formatter (argparse.HelpFormatter) – Class used to format the help output.

Returns:

Command-line interpreter.

Return type:

argparse.ArgumentParser

static main(args)[source]

Execute the script.

classmethod name()[source]

Provide the name of the script. By default, this is the name of the module with “pypeit” prepended.

classmethod parse_args(options=None)[source]

Parse the command-line arguments.

class pypeit.scripts.scriptbase.SmartFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: HelpFormatter

Enable a combination of both fixed-format and wrappable lines to be formatted for the help statements for command-line arguments used with argparse.ArgumentParser.

Borrows from https://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text

Help strings that use this formatter must begin with “R|”. If not, the help string is parsed by the base class.

When parsed by this formatter, the leading “R|” characters are stripped and the lines to be printed are parsed using str.splitlines. Each resulting line is wrapped using textwrap.wrap, unless it begins with the characters “F|”, which forces the line to remain unaltered (except for stripping the leading characters).

For example, if you add an argument like this:

parser.add_argument('-t', '--tell_file', type=str,
                    help='R|Configuration file to change default telluric parameters.  '
                         'Note that the parameters in this file will be overwritten if '
                         'you set argument in your terminal.  The --tell_file option '
                         'requires a .tell file with the following format:\n'
                         '\n'
                         'F|    [tellfit]\n'
                         'F|         objmodel = qso\n'
                         'F|         redshift = 7.6\n'
                         'F|         bal_wv_min_max = 10825,12060\n'
                         'OR\n'
                         'F|    [tellfit]\n'
                         'F|         objmodel = star\n'
                         'F|         star_type = A0\n'
                         'F|         star_mag = 8.\n'
                         'OR\n'
                         'F|    [tellfit]\n'
                         'F|         objmodel = poly\n'
                         'F|         polyorder = 3\n'
                         'F|         fit_wv_min_max = 9000.,9500.\n'
                         '\n')

The result will be (depending on the width of your console):

-t TELL_FILE, --tell_file TELL_FILE
                  Configuration file to change default telluric
                  parameters.  Note that the parameters in this file
                  will be overwritten if you set argument in your
                  terminal.  The --tell_file option requires a .tell
                  file with the following format:

                      [tellfit]
                           objmodel = qso
                           redshift = 7.6
                           bal_wv_min_max = 10825,12060
                  OR
                      [tellfit]
                           objmodel = star
                           star_type = A0
                           star_mag = 8.
                  OR
                      [tellfit]
                           objmodel = poly
                           polyorder = 3
                           fit_wv_min_max = 9000.,9500.
_split_lines(text, width)[source]

Split the provided text into width constrained lines.

See the class description for formatting instructions.