Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/tool/commands/dataset.py: 55%

44 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-09-24 11:43 +0000

1import logging 

2 

3from pyrocko.util import glob_filter 

4from pyrocko import squirrel 

5from ..common import SquirrelCommand 

6 

7logger = logging.getLogger('psq.cli.dataset') 

8 

9headline = 'Dataset managment.' 

10 

11description = '''%s 

12 

13Get information about built-in datasets. 

14''' % headline 

15 

16 

17def add_argument_dataset_names(parser, nargs): 

18 parser.add_argument( 

19 dest='dataset_names', 

20 nargs=nargs, 

21 metavar='NAMES', 

22 help='List only datasets with names matching given (glob-style) ' 

23 'patterns.') 

24 

25 

26def get_matching_builtin_datasets(name_patterns): 

27 datasets = squirrel.dataset.get_builtin_datasets() 

28 return [ 

29 (name, datasets[name]) 

30 for name in sorted(glob_filter(name_patterns, datasets.keys()))] 

31 

32 

33class List(SquirrelCommand): 

34 

35 def make_subparser(self, subparsers): 

36 return subparsers.add_parser( 

37 'list', 

38 help='List builtin datasets.', 

39 description='List builtin datasets.') 

40 

41 def setup(self, parser): 

42 add_argument_dataset_names(parser, '*') 

43 

44 def run(self, parser, args): 

45 for (name, ds) in get_matching_builtin_datasets(args.dataset_names): 

46 print('%-25s %s' % (name, ds.comment or '')) 

47 

48 

49class Show(SquirrelCommand): 

50 

51 def make_subparser(self, subparsers): 

52 return subparsers.add_parser( 

53 'show', 

54 help='Print dataset description.', 

55 description='Print dataset description.') 

56 

57 def setup(self, parser): 

58 add_argument_dataset_names(parser, '+') 

59 

60 def run(self, parser, args): 

61 for (name, ds) in get_matching_builtin_datasets(args.dataset_names): 

62 print('# %s' % name) 

63 print(ds) 

64 

65 

66class Sources(SquirrelCommand): 

67 

68 def make_subparser(self, subparsers): 

69 return subparsers.add_parser( 

70 'sources', 

71 help='List sources attached to Squirrel through datasets.', 

72 description='List sources attached to Squirrel through datasets.') 

73 

74 def setup(self, parser): 

75 parser.add_squirrel_selection_arguments() 

76 

77 def run(self, parser, args): 

78 squirrel = args.make_squirrel() 

79 for source in squirrel.get_sources(): 

80 print(source.info()) 

81 

82 

83def make_subparser(subparsers): 

84 return subparsers.add_parser( 

85 'dataset', 

86 help=headline, 

87 subcommands=[List(), Show(), Sources()], 

88 description=description) 

89 

90 

91def setup(parser): 

92 pass 

93 

94 

95def run(parser, args): 

96 parser.print_help()