Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/sparrow/cli.py: 0%
86 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
1# https://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import sys
7import logging
8from optparse import OptionParser
9from io import StringIO
11import pyrocko
12from pyrocko import util
13from pyrocko import deps
16logger = logging.getLogger('pyrocko.gui.sparrow.cli')
17km = 1e3
20def d2u(d):
21 if isinstance(d, dict):
22 return dict((k.replace('-', '_'), v) for (k, v) in d.items())
23 else:
24 return d.replace('-', '_')
27subcommand_descriptions = {
28 'view': 'open viewer (default)',
29 'version': 'print version number',
30}
32subcommand_usages = {
33 'view': 'view [options] <files> ...',
34 'version': 'version',
35}
37subcommands = list(subcommand_descriptions.keys())
39program_name = 'sparrow'
41usage_tdata = d2u(subcommand_descriptions)
42usage_tdata['program_name'] = program_name
43usage_tdata['version_number'] = pyrocko.__version__
46usage = '''%(program_name)s <subcommand> [options] [--] <arguments> ...
48A quick little bird to visualize geophysical data.
50The Sparrow is part of Pyrocko version %(version_number)s.
52Subcommands:
54 view %(view)s
55 version %(version)s
57To get further help and a list of available options for any subcommand run:
59 %(program_name)s <subcommand> --help
61Where does your journey go today?!
62''' % usage_tdata
65def main(args=None):
66 if not args:
67 args = sys.argv
69 help_triggers = ['--help', '-h', 'help']
71 args = list(args)
72 if len(args) < 2 or args[1] not in subcommands + help_triggers:
73 args[1:1] = ['view']
75 args.pop(0)
76 command = args.pop(0)
78 if command in subcommands:
79 globals()['command_' + d2u(command)](args)
81 elif command in help_triggers:
82 if command == 'help' and args:
83 acommand = args[0]
84 if acommand in subcommands:
85 globals()['command_' + acommand](['--help'])
87 sys.exit('Usage: %s' % usage)
89 else:
90 die('No such subcommand: %s' % command)
93def add_common_options(parser):
94 parser.add_option(
95 '--loglevel',
96 action='store',
97 dest='loglevel',
98 type='choice',
99 choices=('critical', 'error', 'warning', 'info', 'debug'),
100 default='info',
101 help='set logger level to '
102 '"critical", "error", "warning", "info", or "debug". '
103 'Default is "%default".')
106def process_common_options(command, parser, options):
107 util.setup_logging(program_name, options.loglevel)
110def cl_parse(command, args, setup=None, details=None):
111 usage = subcommand_usages[command]
112 descr = subcommand_descriptions[command]
114 if isinstance(usage, str):
115 usage = [usage]
117 susage = '%s %s' % (program_name, usage[0])
118 for s in usage[1:]:
119 susage += '\n%s%s %s' % (' '*7, program_name, s)
121 description = descr[0].upper() + descr[1:] + '.'
123 if details:
124 description = description + '\n\n%s' % details
126 parser = OptionParser(usage=susage, description=description)
128 if setup:
129 setup(parser)
131 add_common_options(parser)
132 (options, args) = parser.parse_args(args)
133 process_common_options(command, parser, options)
134 return parser, options, args
137def die(message, err='', prelude=''):
138 if prelude:
139 prelude = prelude + '\n'
141 if err:
142 err = '\n' + err
144 sys.exit('%s%s failed: %s%s' % (prelude, program_name, message, err))
147def help_and_die(parser, message):
148 sio = StringIO()
149 parser.print_help(sio)
150 die(message, prelude=sio.getvalue())
153def command_view(args):
154 def setup(parser):
155 parser.add_option(
156 '--no-depth-peeling',
157 dest='use_depth_peeling',
158 action='store_false',
159 default=True,
160 help='disable use of VTKs depth peeling (e.g. if the hardware '
161 'doesn\'t support it).')
163 parser.add_option(
164 '--snapshots',
165 dest='snapshots',
166 action='append',
167 default=[],
168 metavar='FILE',
169 help='load snapshots from FILE.')
171 parser, options, args = cl_parse('view', args, setup)
173 try:
174 pyrocko.sparrow(
175 use_depth_peeling=options.use_depth_peeling,
176 snapshots=options.snapshots)
178 except deps.MissingPyrockoDependency as e:
179 die(str(e))
182def command_version(args):
183 def setup(parser):
184 parser.add_option(
185 '--short', dest='short', action='store_true',
186 help='only print Pyrocko\'s version number')
188 parser, options, args = cl_parse('version', args, setup)
190 from pyrocko.print_version import print_version
191 print_version(not options.short)