1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6import logging 

7 

8from . import common 

9from .commands import command_modules 

10 

11 

12logger = logging.getLogger('psq.cli') 

13 

14 

15g_program_name = 'squirrel' 

16 

17 

18def main(args=None): 

19 run( 

20 args=args, 

21 prog=g_program_name, 

22 subcommands=command_modules, 

23 description=''' 

24Pyrocko Squirrel - Prompt seismological data access with a fluffy tail. 

25 

26This is ```squirrel```, a command-line front-end to the Squirrel data access 

27infrastructure. The Squirrel infrastructure offers meta-data caching, blazingly 

28fast data lookup for large datasets and transparent online data download to 

29applications building on it. 

30 

31In most cases, the Squirrel does its business discretely under the hood and 

32does not require human interaction or awareness. However, using this tool, some 

33aspects can be configured for the benefit of greater performance or 

34convenience, including (1) using a separate (isolated, local) environment for a 

35specific project, (2) using named selections to speed up access to very large 

36datasets, (3), pre-scanning/indexing of file collections. It can also be used 

37to inspect various aspects of a data collection. 

38 

39This tool's functionality is available through several subcommands. Run 

40```squirrel SUBCOMMAND --help``` to get further help.''') 

41 

42 

43def run( 

44 args=None, 

45 command=None, 

46 subcommands=[], 

47 description=''' 

48Pyrocko Squirrel based script. 

49 

50Run with --help to get further help.''', 

51 **kwargs): 

52 

53 ''' 

54 Setup and run Squirrel-based application. 

55 

56 The implementation of the tool can be provided by one or multiple 

57 :py:class:`~pyrocko.squirrel.tool.SquirrelCommand` instances. This driver 

58 function sets up a 

59 :py:class:`~pyrocko.squirrel.tool.SquirrelArgumentParser`, and processes 

60 command line arguments, and dispatches execution to the selected command or 

61 subcommand. The program is set up to provide and automatically handle 

62 ``--help``, ``--loglevel``, and ``--progress``. If an exception of type 

63 :py:exc:`pyrocko.squirrel.error.SquirrelError` or 

64 :py:exc:`pyrocko.squirrel.error.ToolError` is caught, the error is logged 

65 and the program is terminated with exit code 1. 

66 

67 :param args: 

68 Arguments passed to 

69 :py:meth:`~pyrocko.squirrel.tool.SquirrelArgumentParser.parse_args. 

70 By default uses py:attr:`sys.argv`. 

71 :type args: 

72 :py:class:`list` of :py:class:`str` 

73 

74 :param command: 

75 Implementation of the command. It must provide ``setup(parser)`` and 

76 ``run(parser, args)``. 

77 :type command: 

78 :py:class:`~pyrocko.squirrel.tool.SquirrelCommand` or module 

79 

80 :param subcommands: 

81 Configures the program to offer multiple subcommands. The command to 

82 execute is selected with the first argument passed to ``args``. 

83 Implementations must provide ``make_subparser(subparsers)``, 

84 ``setup(parser)``, and ``run(parser, args)``. 

85 :type subcommands: 

86 :py:class:`list` of 

87 :py:class:`~pyrocko.squirrel.tool.SquirrelCommand` instances or 

88 modules 

89 

90 :param description: 

91 Description of the program. 

92 :type description: 

93 str 

94 ''' 

95 

96 parser = common.SquirrelArgumentParser( 

97 command=command, 

98 subcommands=subcommands, 

99 description=description, **kwargs) 

100 

101 return parser.run(args) 

102 

103 

104__all__ = [ 

105 'main', 

106 'run', 

107]