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

35 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-03-07 11:54 +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 

66def make_subparser(subparsers): 

67 return subparsers.add_parser( 

68 'dataset', 

69 help=headline, 

70 subcommands=[List(), Show()], 

71 description=description) 

72 

73 

74def setup(parser): 

75 pass 

76 

77 

78def run(parser, args): 

79 parser.print_help()