1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import sys 

7 

8from pyrocko.squirrel.check import do_check, SquirrelCheckProblemType 

9 

10from ..common import csvtype, dq, ldq 

11 

12headline = 'Check dataset consistency.' 

13 

14 

15def make_subparser(subparsers): 

16 return subparsers.add_parser( 

17 'check', 

18 help=headline, 

19 description=headline + ''' 

20 

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

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

23 

24%s 

25''' % '\n'.join( 

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

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

28 

29 

30def setup(parser): 

31 parser.add_squirrel_selection_arguments() 

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

33 parser.add_argument( 

34 '--ignore', 

35 type=csvtype(SquirrelCheckProblemType.choices), 

36 metavar='TYPE,...', 

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

38 % ldq(SquirrelCheckProblemType.choices)) 

39 

40 parser.add_argument( 

41 '--verbose', '-v', 

42 action='count', 

43 dest='verbosity', 

44 default=0, 

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

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

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

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

49 

50 format_default = 'text' 

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

52 

53 parser.add_argument( 

54 '--out-format', 

55 choices=format_choices, 

56 default=format_default, 

57 dest='output_format', 

58 metavar='FMT', 

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

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

61 

62 

63def run(parser, args): 

64 squirrel = args.make_squirrel() 

65 

66 check = do_check( 

67 squirrel, 

68 ignore=args.ignore or [], 

69 **args.squirrel_query) 

70 

71 if args.output_format == 'text': 

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

73 elif args.output_format == 'yaml': 

74 print(check) 

75 else: 

76 assert False 

77 

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