Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/drum/cli.py: 0%
86 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
1from __future__ import print_function, absolute_import
3import sys
4import logging
5from optparse import OptionParser
6from io import StringIO
8import pyrocko
9from pyrocko import deps
10from pyrocko import util
13logger = logging.getLogger('pyrocko.gui.drum.cli')
14km = 1e3
17def d2u(d):
18 if isinstance(d, dict):
19 return dict((k.replace('-', '_'), v) for (k, v) in d.items())
20 else:
21 return d.replace('-', '_')
24subcommand_descriptions = {
25 'view': 'open viewer (default)',
26 'version': 'print version number',
27}
29subcommand_usages = {
30 'view': 'view [options] <files> ...',
31 'version': 'version',
32}
34subcommands = list(subcommand_descriptions.keys())
36program_name = 'drum'
38usage_tdata = d2u(subcommand_descriptions)
39usage_tdata['program_name'] = program_name
40usage_tdata['version_number'] = pyrocko.__version__
43usage = '''%(program_name)s <subcommand> [options] [--] <arguments> ...
45A helicorder and datalogger.
47The Drum is part of Pyrocko version %(version_number)s.
49Subcommands:
51 view %(view)s
52 version %(version)s
54To get further help and a list of available options for any subcommand run:
56 %(program_name)s <subcommand> --help
58''' % usage_tdata
61def main(args=None):
62 if not args:
63 args = sys.argv
65 help_triggers = ['--help', '-h', 'help']
67 args = list(args)
68 if len(args) < 2 or args[1] not in subcommands + help_triggers:
69 args[1:1] = ['view']
71 args.pop(0)
72 command = args.pop(0)
74 if command in subcommands:
75 globals()['command_' + d2u(command)](args)
77 elif command in help_triggers:
78 if command == 'help' and args:
79 acommand = args[0]
80 if acommand in subcommands:
81 globals()['command_' + acommand](['--help'])
83 sys.exit('Usage: %s' % usage)
85 else:
86 die('No such subcommand: %s' % command)
89def add_common_options(parser):
90 parser.add_option(
91 '--loglevel',
92 action='store',
93 dest='loglevel',
94 type='choice',
95 choices=('critical', 'error', 'warning', 'info', 'debug'),
96 default='info',
97 help='set logger level to '
98 '"critical", "error", "warning", "info", or "debug". '
99 'Default is "%default".')
102def process_common_options(command, parser, options):
103 util.setup_logging(program_name, options.loglevel)
106def cl_parse(command, args, setup=None, details=None):
107 usage = subcommand_usages[command]
108 descr = subcommand_descriptions[command]
110 if isinstance(usage, str):
111 usage = [usage]
113 susage = '%s %s' % (program_name, usage[0])
114 for s in usage[1:]:
115 susage += '\n%s%s %s' % (' '*7, program_name, s)
117 description = descr[0].upper() + descr[1:] + '.'
119 if details:
120 description = description + '\n\n%s' % details
122 parser = OptionParser(usage=susage, description=description)
124 if setup:
125 setup(parser)
127 add_common_options(parser)
128 (options, args) = parser.parse_args(args)
129 process_common_options(command, parser, options)
130 return parser, options, args
133def die(message, err='', prelude=''):
134 if prelude:
135 prelude = prelude + '\n'
137 if err:
138 err = '\n' + err
140 sys.exit('%s%s failed: %s%s' % (prelude, program_name, message, err))
143def help_and_die(parser, message):
144 sio = StringIO()
145 parser.print_help(sio)
146 die(message, prelude=sio.getvalue())
149def command_view(args):
150 def setup(parser):
151 pass
153 parser, options, args = cl_parse('view', args, setup)
155 try:
156 pyrocko.drum()
157 except deps.MissingPyrockoDependency as e:
158 die(str(e))
161def command_version(args):
162 def setup(parser):
163 parser.add_option(
164 '--short', dest='short', action='store_true',
165 help='only print Pyrocko\'s version number')
167 parser, options, args = cl_parse('version', args, setup)
169 from pyrocko.print_version import print_version
170 print_version(not options.short)
173if __name__ == '__main__':
174 main()