Progress/Parser.pm

filter: Statementstmt 94.4% 68 / 72 Branchbran 73.8% 31 / 42 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 84.4% 119 / 141 CC 30 SCAR 30.0
CC
30
Cov
86%
parse
26.3
step_state
7.3
BEGIN
1.0
CRAP 20.2
↑ Summary Progress/RenderTTY.pm »
1 package Log::Progress::Parser;
2 5 $Log::Progress::Parser::VERSION = '0.13';
3 5 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Parse progress data from a file
7  
8  
9 5 has input => ( is => 'rw' );
10 5 has input_pos => ( is => 'rw' );
11 10 ▶has state => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 5 *status= *state; # alias, since I changed the API
13 5 has sticky_message => ( is => 'rw' );
14 5 has on_data => ( is => 'rw' );
15  
16  
17 28 ▶sub parse {
Subroutine parse: called CC=24 SCAR=32.7
Pod documented
18 28 my $self= shift;
19 28 my $fh= $self->input;
20 28 ▶ if (!ref $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 19 9 28
21 19 my $input= $fh;
22 19 undef $fh;
23 19 ▶ open $fh, '<', \$input or die "open(scalar): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", \$input or die "open(scalar): $!" 19 0 19
24 }
25 28 ▶ if ($self->input_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input_pos) 18 10 28
26 18 ▶ seek($fh, $self->input_pos, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $self->input_pos, 0 or die "seek: $!" 18 0 18
27 or die "seek: $!";
28 }
29 # TODO: If input is seekable, then seek to end and work backward
30 # Substeps will make that rather complicated.
31 28 my $pos;
32 my %parent_cleanup;
33 28 local $_;
34 28 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 170 27 197
35 170 ▶ last unless substr($_,-1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($_, -1) eq "\n" 1 169 170
36 169 $pos= tell($fh);
37 169 ▶ next unless $_ =~ /^progress: (([[:alpha:]][\w.]*) )?(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^progress: (([[:alpha:]][\w.]*) )?(.*)/ 3 166 169
38 166 my ($step_id, $remainder)= ($2, $3);
39 166 my $state= $self->step_state($step_id, 1, \my @state_parent);
40 # First, check for progress number followed by optional message
41 166 ▶ if ($remainder =~ m,^([\d.]+)(/(\d+))?( (.*))?,) {
Branch: 3 branches
BranchTrueFalseTotal
if ($remainder =~ m[^([\d.]+)(/(\d+))?( (.*))?]) { } 151 15 166
elsif ($remainder =~ /^\(([\d.]+)\) (.*)/) { } 15 0 15
elsif ($remainder =~ /^\{/) { } 0 0 0
42 151 my ($num, $denom, $message)= ($1, $3, $5);
43 151 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 20 131 151
44 20 $message =~ s/^- //; # "- " is optional syntax
45 20 $state->{message}= $message;
46 } else {
47 131 ▶ $state->{message}= '' if !defined $state->{message} or !$self->sticky_message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state->{"message"} and $self->sticky_message 130 1 131
Condition100%3/3
defined $state->{"message"} and $self->sticky_message
!ll&&!rl&&r
161141
MC/DC100%2/2
defined $state->{"message"} and $self->sticky_message
defined $state->{"message"} $self->sticky_message
Truth table100%3/3
A and B
A= defined $state->{"message"}B= $self->sticky_message
ABresult
0X0
100
111
48 }
49 151 $state->{progress}= $num+0;
50 151 ▶ if (defined $denom) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $denom) 120 31 151
51 120 $state->{current}= $num;
52 120 $state->{total}= $denom;
53 120 ▶ $state->{progress} /= $denom if $denom;
Branch: 1 branch
BranchTrueFalseTotal
if $denom 120 0 120
54 }
55 151 ▶ $state->{progress}= 1 if $state->{progress} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"progress"} > 1 0 151 151
56 151 ▶ if ($state->{contribution}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"contribution"}) 139 12 151
57 # Need to apply progress to parent nodes at end
58 $parent_cleanup{$state_parent[$_]}= [ $_, $state_parent[$_] ]
59 139 for 0..$#state_parent;
60 }
61 }
62 elsif ($remainder =~ m,^\(([\d.]+)\) (.*),) {
63 15 my ($contribution, $title)= ($1, $2);
64 15 $title =~ s/^- //; # "- " is optional syntax
65 15 $state->{title}= $title;
66 15 $state->{contribution}= $contribution+0;
67 }
68 elsif ($remainder =~ /^\{/) {
69 0 my $data= JSON->new->decode($remainder);
70 0 ▶ $state->{data}= !defined $self->on_data? $data
Branch: 1 branch
BranchTrueFalseTotal
!defined($self->on_data) ? : 0 0 0
71 : $self->on_data->($self, $step_id, $data);
72 }
73 else {
74 0 warn "can't parse progress message \"$remainder\"\n";
75 }
76 }
77 # Mark file position for next call
78 28 $self->input_pos($pos);
79 # apply child progress contributions to parent nodes
80 0 for (sort { $b->[0] <=> $a->[0] } values %parent_cleanup) {
81 13 my $state= $_->[1];
82 13 $state->{progress}= 0;
83 13 for (values %{$state->{step}}) {
84 $state->{progress} += $_->{progress} * $_->{contribution}
85 31 ▶ if $_->{progress} && $_->{contribution};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"progress"} and $_->{"contribution"} 29 2 31
Condition66%2/3
$_->{"progress"} and $_->{"contribution"}
!ll&&!rl&&r
2029
MC/DC50%1/2
$_->{"progress"} and $_->{"contribution"}
$_->{"progress"} $_->{"contribution"}
Truth table66%2/3
A and B
A= $_->{"progress"}B= $_->{"contribution"}
ABresult
0X0
100
111
86 }
87 }
88 28 return $self->state;
89 }
90  
91  
92 166 ▶sub step_state {
Subroutine step_state: called CC=7 SCAR=19.8
Pod documented
93 166 my ($self, $step_id, $create, $path)= @_;
94 166 my $state= $self->state;
95 166 ▶ if (defined $step_id and length $step_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step_id and length $step_id) 154 12 166
Condition66%2/3
defined $step_id and length $step_id
!ll&&!rl&&r
120154
MC/DC50%1/2
defined $step_id and length $step_id
defined $step_id length $step_id
Truth table66%2/3
A and B
A= defined $step_idB= length $step_id
ABresult
0X0
100
111
96 154 for (split /\./, $step_id) {
97 154 ▶ push @$path, $state if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 154 0 154
98 154 ▶ $state= ($state->{step}{$_} or do {
Condition66%2/3
$state->{'step'}{$_} || do { return undef unless $create; my $idx = scalar keys %{$$state{'step'};}; $state->{'step'}{$_} = {'idx', $idx} }
l!l&&r!l&&!r
139150
MC/DC0%0/2
$state->{'step'}{$_} || do { return undef unless $create; my $idx = scalar keys %{$$state{'step'};}; $state->{'step'}{$_} = {'idx', $idx} }
$state->{'step'}{$_} do { return undef unless $create; my $idx = scalar keys %{$$state{'step'};}; $state->{'step'}{$_} = {'idx', $idx} }
Truth table66%2/3
A || B
A= $state->{'step'}{$_}B= do { return undef unless $create; my $idx = scalar keys %{$$state{'step'};}; $state->{'step'}{$_} = {'idx', $idx} }
ABresult
1X1
011
000
99 15 ▶ return undef unless $create;
Branch: 1 branch
BranchTrueFalseTotal
unless $create 0 15 15
100 15 my $idx= scalar(keys %{$state->{step}});
101 15 $state->{step}{$_}= { idx => $idx };
102 });
103 }
104 }
105 166 $state;
106 }
107  
108 5 1;
109  
110 __END__
↑ Summary Progress/RenderTTY.pm »