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 2024-03-07 11:54 +0000

1from __future__ import print_function, absolute_import 

2 

3import sys 

4import logging 

5from optparse import OptionParser 

6from io import StringIO 

7 

8import pyrocko 

9from pyrocko import deps 

10from pyrocko import util 

11 

12 

13logger = logging.getLogger('pyrocko.gui.drum.cli') 

14km = 1e3 

15 

16 

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('-', '_') 

22 

23 

24subcommand_descriptions = { 

25 'view': 'open viewer (default)', 

26 'version': 'print version number', 

27} 

28 

29subcommand_usages = { 

30 'view': 'view [options] <files> ...', 

31 'version': 'version', 

32} 

33 

34subcommands = list(subcommand_descriptions.keys()) 

35 

36program_name = 'drum' 

37 

38usage_tdata = d2u(subcommand_descriptions) 

39usage_tdata['program_name'] = program_name 

40usage_tdata['version_number'] = pyrocko.__version__ 

41 

42 

43usage = '''%(program_name)s <subcommand> [options] [--] <arguments> ... 

44 

45A helicorder and datalogger. 

46 

47The Drum is part of Pyrocko version %(version_number)s. 

48 

49Subcommands: 

50 

51 view %(view)s 

52 version %(version)s 

53 

54To get further help and a list of available options for any subcommand run: 

55 

56 %(program_name)s <subcommand> --help 

57 

58''' % usage_tdata 

59 

60 

61def main(args=None): 

62 if not args: 

63 args = sys.argv 

64 

65 help_triggers = ['--help', '-h', 'help'] 

66 

67 args = list(args) 

68 if len(args) < 2 or args[1] not in subcommands + help_triggers: 

69 args[1:1] = ['view'] 

70 

71 args.pop(0) 

72 command = args.pop(0) 

73 

74 if command in subcommands: 

75 globals()['command_' + d2u(command)](args) 

76 

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']) 

82 

83 sys.exit('Usage: %s' % usage) 

84 

85 else: 

86 die('No such subcommand: %s' % command) 

87 

88 

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".') 

100 

101 

102def process_common_options(command, parser, options): 

103 util.setup_logging(program_name, options.loglevel) 

104 

105 

106def cl_parse(command, args, setup=None, details=None): 

107 usage = subcommand_usages[command] 

108 descr = subcommand_descriptions[command] 

109 

110 if isinstance(usage, str): 

111 usage = [usage] 

112 

113 susage = '%s %s' % (program_name, usage[0]) 

114 for s in usage[1:]: 

115 susage += '\n%s%s %s' % (' '*7, program_name, s) 

116 

117 description = descr[0].upper() + descr[1:] + '.' 

118 

119 if details: 

120 description = description + '\n\n%s' % details 

121 

122 parser = OptionParser(usage=susage, description=description) 

123 

124 if setup: 

125 setup(parser) 

126 

127 add_common_options(parser) 

128 (options, args) = parser.parse_args(args) 

129 process_common_options(command, parser, options) 

130 return parser, options, args 

131 

132 

133def die(message, err='', prelude=''): 

134 if prelude: 

135 prelude = prelude + '\n' 

136 

137 if err: 

138 err = '\n' + err 

139 

140 sys.exit('%s%s failed: %s%s' % (prelude, program_name, message, err)) 

141 

142 

143def help_and_die(parser, message): 

144 sio = StringIO() 

145 parser.print_help(sio) 

146 die(message, prelude=sio.getvalue()) 

147 

148 

149def command_view(args): 

150 def setup(parser): 

151 pass 

152 

153 parser, options, args = cl_parse('view', args, setup) 

154 

155 try: 

156 pyrocko.drum() 

157 except deps.MissingPyrockoDependency as e: 

158 die(str(e)) 

159 

160 

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') 

166 

167 parser, options, args = cl_parse('version', args, setup) 

168 

169 from pyrocko.print_version import print_version 

170 print_version(not options.short) 

171 

172 

173if __name__ == '__main__': 

174 main()