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-04 09:52 +0000

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import sys 

7import logging 

8from optparse import OptionParser 

9from io import StringIO 

10 

11import pyrocko 

12from pyrocko import util 

13from pyrocko import deps 

14 

15 

16logger = logging.getLogger('pyrocko.gui.sparrow.cli') 

17km = 1e3 

18 

19 

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

25 

26 

27subcommand_descriptions = { 

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

29 'version': 'print version number', 

30} 

31 

32subcommand_usages = { 

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

34 'version': 'version', 

35} 

36 

37subcommands = list(subcommand_descriptions.keys()) 

38 

39program_name = 'sparrow' 

40 

41usage_tdata = d2u(subcommand_descriptions) 

42usage_tdata['program_name'] = program_name 

43usage_tdata['version_number'] = pyrocko.__version__ 

44 

45 

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

47 

48A quick little bird to visualize geophysical data. 

49 

50The Sparrow is part of Pyrocko version %(version_number)s. 

51 

52Subcommands: 

53 

54 view %(view)s 

55 version %(version)s 

56 

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

58 

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

60 

61Where does your journey go today?! 

62''' % usage_tdata 

63 

64 

65def main(args=None): 

66 if not args: 

67 args = sys.argv 

68 

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

70 

71 args = list(args) 

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

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

74 

75 args.pop(0) 

76 command = args.pop(0) 

77 

78 if command in subcommands: 

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

80 

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

86 

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

88 

89 else: 

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

91 

92 

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

104 

105 

106def process_common_options(command, parser, options): 

107 util.setup_logging(program_name, options.loglevel) 

108 

109 

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

111 usage = subcommand_usages[command] 

112 descr = subcommand_descriptions[command] 

113 

114 if isinstance(usage, str): 

115 usage = [usage] 

116 

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

118 for s in usage[1:]: 

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

120 

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

122 

123 if details: 

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

125 

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

127 

128 if setup: 

129 setup(parser) 

130 

131 add_common_options(parser) 

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

133 process_common_options(command, parser, options) 

134 return parser, options, args 

135 

136 

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

138 if prelude: 

139 prelude = prelude + '\n' 

140 

141 if err: 

142 err = '\n' + err 

143 

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

145 

146 

147def help_and_die(parser, message): 

148 sio = StringIO() 

149 parser.print_help(sio) 

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

151 

152 

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

162 

163 parser.add_option( 

164 '--snapshots', 

165 dest='snapshots', 

166 action='append', 

167 default=[], 

168 metavar='FILE', 

169 help='load snapshots from FILE.') 

170 

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

172 

173 try: 

174 pyrocko.sparrow( 

175 use_depth_peeling=options.use_depth_peeling, 

176 snapshots=options.snapshots) 

177 

178 except deps.MissingPyrockoDependency as e: 

179 die(str(e)) 

180 

181 

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

187 

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

189 

190 from pyrocko.print_version import print_version 

191 print_version(not options.short)