pyrocko.squirrel.tool.common¶
Squirrel command line tool infrastructure and argument parsing.
Functions
|
Set up command line options commonly used in squirrel queries. |
|
Set up command line options commonly used to configure a |
|
Convert Exceptions in to ToolError. |
Create a |
|
Get common arguments to be used in squirrel queries from command line. |
Classes
|
Tweaks and extends the standard argument parser to simplify the generation of the online docs. |
|
Parser for CLI arguments with a some extras for Squirrel based apps. |
Base class for Squirrel-based CLI programs and subcommands. |
- class PyrockoArgumentParser(prog=None, usage=None, description=None, epilog=None, **kwargs)[source]¶
Bases:
ArgumentParserTweaks and extends the standard argument parser to simplify the generation of the online docs.
We want to convert the
--helpoutputs torstfor the html docs. Problem is that argparse’sHelpFormatterto date have no public interface which we could use to achieve this. The solution here is a bit clunky but works ok for our purposes. We allow markup like``code``which is kept when producingrst(by parsing the final--helpoutput) but stripped out when doing normal--help. This leads to a problem with the internal output wrapping of argparse which it does before the stripping. To solve, we render with argparse to a very wide width and do the wrapping in post-processing.``code``is replaced with justcodein normal output.```code```is replaced with'code'in normal output and with``code``inrstoutput.rstoutput is selected with environment variablePYROCKO_RST_HELP=1. The scriptmaintenance/argparse_help_to_rst.pyextracts thersthelp and generates therstfiles for the docs.
- class SquirrelArgumentParser(*args, command=None, subcommands=[], **kwargs)[source]¶
Bases:
PyrockoArgumentParserParser for CLI arguments with a some extras for Squirrel based apps.
- Parameters:
command (
SquirrelCommandor module providing the same interface) – Implementation of the command.subcommands (
listofSquirrelCommandor modules providing the same interface) – Implementations of subcommands.*args – Handed through to base class’s init.
**kwargs – Handed through to base class’s init.
- parse_args(args=None, namespace=None)[source]¶
Parse arguments given on command line.
Extends the functionality of
argparse.ArgumentParser.parse_args()to process and handle the standard options--loglevel,--progressand--help.
- dispatch(args)[source]¶
Dispatch execution to selected command/subcommand.
- Parameters:
args – Parsed arguments obtained from
parse_args().- Returns:
Trueif dispatching was successful,Falseotherwise.
If an exception of type
SquirrelErrororToolErroris caught, the error is logged and the program is terminated with exit code 1.
- run(args=None)[source]¶
Parse arguments and dispatch to selected command/subcommand.
This simply calls
parse_args()and thendispatch()with the obtainedargs. A usage message is printed if no command is selected.
- add_squirrel_selection_arguments()[source]¶
Set up command line options commonly used to configure a
Squirrelinstance.This will optional arguments
--add,--include,--exclude,--optimistic,--format,--add-only,--persistent, and--dataset.Call
args.make_squirrel()on the arguments returned fromparse_args()to finally instantiate and configure theSquirrelinstance.
- add_squirrel_query_arguments(without=[])[source]¶
Set up command line options commonly used in squirrel queries.
This will add optional arguments
--kinds,--codes,--tmin,--tmax, and--time.Once finished with parsing, the query arguments are available as
args.squirrel_queryon the arguments returned fromparse_args().
- add_squirrel_selection_arguments(parser)[source]¶
Set up command line options commonly used to configure a
Squirrelinstance.This will optional arguments
--add,--include,--exclude,--optimistic,--format,--add-only,--persistent, and--datasetto a given argument parser.Once finished with parsing, call
squirrel_from_selection_arguments()to finally instantiate and configure theSquirrelinstance.- Parameters:
parser (argparse.ArgumentParser) – The argument parser to be configured.
- squirrel_from_selection_arguments(args)[source]¶
Create a
Squirrelinstance from command line arguments.Use
add_squirrel_selection_arguments()to configure the parser with the necessary options.- Parameters:
args – Parsed command line arguments, as returned by
argparse.ArgumentParser.parse_args().- Returns:
pyrocko.squirrel.base.Squirrelinstance with paths, datasets and remote sources added.
- add_squirrel_query_arguments(parser, without=[])[source]¶
Set up command line options commonly used in squirrel queries.
This will add optional arguments
--kinds,--codes,--tmin,--tmax, and--time.Once finished with parsing, call
squirrel_query_from_arguments()to get the parsed values.- Parameters:
parser (argparse.ArgumentParser) – The argument parser to be configured.
- squirrel_query_from_arguments(args)[source]¶
Get common arguments to be used in squirrel queries from command line.
Use
add_squirrel_query_arguments()to configure the parser with the necessary options.- Parameters:
args – Parsed command line arguments, as returned by
argparse.ArgumentParser.parse_args().- Returns:
dictwith any parsed option values.
- class SquirrelCommand[source]¶
Bases:
objectBase class for Squirrel-based CLI programs and subcommands.
- fail(message)[source]¶
Raises
ToolError.SquirrelArgumentParser.run()catchesToolError, logs the error message and terminates with an error exit state.
- make_subparser(subparsers)[source]¶
To be implemented in subcommand. Create subcommand parser.
Must return a newly created parser obtained with
subparsers.add_parser(...), e.g.:def make_subparser(self, subparsers): return subparsers.add_parser( 'plot', help='Draw a nice plot.')
- setup(parser)[source]¶
To be implemented in subcommand. Configure parser.
- Parameters:
parser (argparse.ArgumentParser) – The argument parser to be configured.
Example:
def setup(self, parser): parser.add_squirrel_selection_arguments() parser.add_squirrel_query_arguments() parser.add_argument( '--fmin', dest='fmin', metavar='FLOAT', type=float, help='Corner of highpass [Hz].')
- run(parser, args)[source]¶
To be implemented in subcommand. Main routine of the command.
- Parameters:
parser (argparse.ArgumentParser) – The argument parser to be configured.
args – Parsed command line arguments, as returned by
argparse.ArgumentParser.parse_args().
Example:
def run(self, parser, args): print('User has selected fmin = %g Hz' % args.fmin) # args.make_squirrel() is available if # parser.add_squirrel_selection_arguments() was called during # setup(). sq = args.make_squirrel() # args.squirrel_query is available if # praser.add_squirrel_query_arguments() was called during # setup(). stations = sq.get_stations(**args.squirrel_query)