| 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
| |||||||||||||||||||||||||||||
| 21 | 19 | my $input= $fh; | |||||||||||||||||||||||||||
| 22 | 19 | undef $fh; | |||||||||||||||||||||||||||
| 23 | 19 | ▶ | open $fh, '<', \$input or die "open(scalar): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 28 | ▶ | if ($self->input_pos) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 18 | ▶ | seek($fh, $self->input_pos, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 170 | ▶ | last unless substr($_,-1) eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 169 | $pos= tell($fh); | |||||||||||||||||||||||||||
| 37 | 169 | ▶ | next unless $_ =~ /^progress: (([[:alpha:]][\w.]*) )?(.*)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 42 | 151 | my ($num, $denom, $message)= ($1, $3, $5); | |||||||||||||||||||||||||||
| 43 | 151 | ▶ | if (defined $message) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $state->{"message"} and $self->sticky_message
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
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 151 | $state->{progress}= $num+0; | |||||||||||||||||||||||||||
| 50 | 151 | ▶ | if (defined $denom) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 120 | $state->{current}= $num; | |||||||||||||||||||||||||||
| 52 | 120 | $state->{total}= $denom; | |||||||||||||||||||||||||||
| 53 | 120 | ▶ | $state->{progress} /= $denom if $denom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 151 | ▶ | $state->{progress}= 1 if $state->{progress} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 151 | ▶ | if ($state->{contribution}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$_->{"progress"} and $_->{"contribution"}
MC/DC50%1/2
$_->{"progress"} and $_->{"contribution"}
$_->{"progress"}
$_->{"contribution"}
Truth table66%2/3
A and B
A= $_->{"progress"}B= $_->{"contribution"}
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $step_id and length $step_id
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
| |||||||||||||||||||||||||||||
| 96 | 154 | for (split /\./, $step_id) { | |||||||||||||||||||||||||||
| 97 | 154 | ▶ | push @$path, $state if $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}
}
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}
}
| |||||||||||||||||||||||||||||
| 99 | 15 | ▶ | return undef unless $create; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||