1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from __future__ import absolute_import, print_function 

7 

8import sys 

9 

10from pyrocko.squirrel.check import do_check, SquirrelCheckProblemType 

11 

12from ..common import csvtype, dq, ldq 

13 

14headline = 'Check dataset consistency.' 

15 

16 

17def make_subparser(subparsers): 

18 return subparsers.add_parser( 

19 'check', 

20 help=headline, 

21 description=headline + ''' 

22 

23A report listing potential dataset/metadata problems for a given data 

24collection is printed to standard output. The following problems are detected: 

25 

26%s 

27''' % '\n'.join( 

28 ' [%s]: %s' % (k, v) 

29 for (k, v) in SquirrelCheckProblemType.types.items())) 

30 

31 

32def setup(parser): 

33 parser.add_squirrel_selection_arguments() 

34 parser.add_squirrel_query_arguments(without=['kinds']) 

35 parser.add_argument( 

36 '--ignore', 

37 type=csvtype(SquirrelCheckProblemType.choices), 

38 metavar='TYPE,...', 

39 help='Problem types to be ignored. Choices: %s.' 

40 % ldq(SquirrelCheckProblemType.choices)) 

41 

42 parser.add_argument( 

43 '--verbose', '-v', 

44 action='count', 

45 dest='verbosity', 

46 default=0, 

47 help='Verbose mode for textual output. Multiple ``-v`` options ' 

48 'increase the verbosity. The maximum is 2. At level 1, ``ok`` ' 

49 'indicators are printed for entries with no problem. At level 2, ' 

50 'availability information is shown for each entry.') 

51 

52 format_default = 'text' 

53 format_choices = ['text', 'yaml'] 

54 

55 parser.add_argument( 

56 '--out-format', 

57 choices=format_choices, 

58 default=format_default, 

59 dest='output_format', 

60 metavar='FMT', 

61 help='Specifies how output is presented. Choices: %s. ' 

62 'Default: %s.' % (ldq(format_choices), dq(format_default))) 

63 

64 

65def run(parser, args): 

66 squirrel = args.make_squirrel() 

67 

68 check = do_check( 

69 squirrel, 

70 ignore=args.ignore or [], 

71 **args.squirrel_query) 

72 

73 if args.output_format == 'text': 

74 print(check.get_text(verbosity=args.verbosity)) 

75 elif args.output_format == 'yaml': 

76 print(check) 

77 else: 

78 assert False 

79 

80 sys.exit(0 if check.get_nproblems() == 0 else 1)