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

53 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-06 15:01 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Implementation of :app:`squirrel coverage`. 

8''' 

9 

10import time 

11 

12from pyrocko import util 

13from pyrocko.plot import terminal 

14from pyrocko.get_terminal_size import get_terminal_size 

15from pyrocko.squirrel.error import ToolError 

16 

17from ..common import ldq 

18 

19 

20headline = 'Report time spans covered.' 

21 

22 

23def make_subparser(subparsers): 

24 return subparsers.add_parser( 

25 'coverage', 

26 help=headline, 

27 description=headline + ''' 

28 

29Time spans covered by the given data selection are listed or plotted. 

30''') 

31 

32 

33def setup(parser): 

34 parser.add_squirrel_selection_arguments() 

35 parser.add_squirrel_query_arguments(without=['time']) 

36 

37 style_choices = ['visual', 'summary', 'yaml'] 

38 

39 parser.add_argument( 

40 '--style', 

41 dest='style', 

42 choices=style_choices, 

43 default='visual', 

44 help='Set style of presentation. Choices: %s' % ldq(style_choices)) 

45 

46 

47def run(parser, args): 

48 from pyrocko import squirrel as sq 

49 

50 squirrel = args.make_squirrel() 

51 kwargs = args.squirrel_query 

52 kinds = kwargs.pop('kind', sq.supported_content_kinds()) 

53 

54 tmin_g, tmax_g = squirrel.get_time_span(kinds) 

55 

56 sx, _ = get_terminal_size() 

57 

58 now = time.time() 

59 if tmax_g is None or tmax_g > now: 

60 tmax_g = now 

61 

62 codes = kwargs.pop('codes', None) 

63 tmin = kwargs.pop('tmin', tmin_g) 

64 tmax = kwargs.pop('tmax', tmax_g) 

65 if tmin is not None and tmax is not None: 

66 if not tmin < tmax: 

67 raise ToolError( 

68 'Invalid time span: %s - %s' % ( 

69 util.time_to_str(tmin), util.time_to_str(tmax))) 

70 

71 for kind in kinds: 

72 coverage = squirrel.get_coverage( 

73 kind, 

74 codes=codes if (codes is not None) else None, 

75 tmin=tmin, 

76 tmax=tmax, 

77 **kwargs) 

78 

79 if coverage: 

80 

81 if args.style == 'yaml': 

82 for entry in coverage: 

83 print(entry) 

84 elif args.style == 'summary': 

85 for entry in coverage: 

86 print(entry.summary) 

87 elif args.style == 'visual': 

88 slabels = [entry.labels for entry in coverage] 

89 

90 scs = [ 

91 max(len(s) for s in entries) 

92 for entries in zip(*slabels)] 

93 

94 label = 'kind: %s' % kind 

95 sc = max(18, len(label), sum(scs)) + 1 

96 si = (sx-sc) - 2 

97 sl = si // 2 

98 sr = si - sl 

99 print(''.join(( 

100 label.ljust(sc), 

101 terminal.ansi_dim, 

102 terminal.bar_right, 

103 util.time_to_str(tmin).ljust(sl), 

104 util.time_to_str(tmax).rjust(sr), 

105 terminal.bar_left, 

106 terminal.ansi_dim_reset))) 

107 

108 for (scodes, srate), entry in zip(slabels, coverage): 

109 line = \ 

110 (scodes.ljust(scs[0]) 

111 + ' ' + srate.rjust(scs[1])).ljust(sc) \ 

112 + terminal.bar( 

113 tmin, tmax, entry.changes, 

114 entry.tmin, entry.tmax, 

115 sx-sc) 

116 

117 print(line) 

118 

119 for line in terminal.time_axis(tmin, tmax, sx-sc): 

120 print(''.join(( 

121 ' '*sc, 

122 terminal.ansi_dim, 

123 line, 

124 terminal.ansi_dim_reset)))