blib/lib/App/RecordStream/Operation/frommultire.pm

filter: Statementstmt 98.1% 106 / 108 Branchbran 100% 26 / 26 Conditioncond 88.2% 15 / 17 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 12 totaltotal 89.8% 176 / 196 CC 27 SCAR 13.4
CC
27
Cov
97%
accept_line
9.0
get_field_value_pairs
6.0
add_keep
3.0
CRAP 3.8
« blib/lib/App/RecordStream/Aggregator/PercentileMap.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Internal/ForField2.pm »
1 package App::RecordStream::Operation::frommultire;
2  
3 3 our $VERSION = "4.0.23";
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub init {
10 9 ▶ my $this = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
11 9 my $args = shift;
12 my %options = (
13 13 ▶ "no-flush-regex|regex|re=s" => sub { $this->add_regex($_[1], 0, 0); },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 0 ▶ "pre-flush-regex|pre=s" => sub { $this->add_regex($_[1], 1, 0); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 2 ▶ "post-flush-regex|post=s" => sub { $this->add_regex($_[1], 0, 1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 0 ▶ "double-flush-regex|double=s" => sub { $this->add_regex($_[1], 1, 1); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 2 ▶ "clobber" => sub { $this->_set_clobber(1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ "keep-all" => sub { $this->_set_keep_all(1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ "keep=s" => sub { $this->add_keep(split(/,/, $_[1])); },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 9 );
21  
22 9 $this->parse_options($args, \%options);
23  
24 8 $this->{'RECORD'} = App::RecordStream::Record->new();
25 }
26  
27 sub add_regex {
28 15 ▶ my ($this, $string, $pre_flush, $post_flush) = @_;
Subroutine add_regex: called CC=3 SCAR=11.0
Pod undocumented
29  
30 15 ▶ $this->{'REGEXES'} ||= [];
Condition100%2/2
$this->{"REGEXES"} ||= []
l!l
78
MC/DC100%1/1
$this->{"REGEXES"} ||= []
$this->{"REGEXES"}
Truth table100%2/2
A
A= $this->{"REGEXES"}
Aresult
11
00
31  
32 15 my $fields = [];
33 15 ▶ if($string =~ /^([^=]*)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([^=]*)=(.*)$/) 14 1 15
34 14 $fields = [split(/,/, $1)];
35 14 $string = $2;
36 }
37  
38 15 push @{$this->{'REGEXES'}}, [$string, $fields, $pre_flush, $post_flush]
39 }
40  
41 sub _get_regexes {
42 42 ▶ my ($this) = @_;
Subroutine _get_regexes: called CC=2 SCAR=7.2
43 42 ▶ return $this->{'REGEXES'} || [];
Condition50%1/2
$this->{'REGEXES'} || []
l!l
420
MC/DC0%0/1
$this->{'REGEXES'} || []
$this->{'REGEXES'}
Truth table50%1/2
A
A= $this->{'REGEXES'}
Aresult
11
00
44 }
45  
46 sub _set_clobber {
47 2 ▶ my ($this, $value) = @_;
Subroutine _set_clobber: called CC=1 SCAR=0.0
48 2 $this->{'CLOBBER'} = $value;
49 }
50  
51 sub get_clobber {
52 46 ▶ my ($this) = @_;
Subroutine get_clobber: called CC=2 SCAR=6.9
Pod undocumented
53 46 ▶ return $this->{'CLOBBER'} || 0;
Condition100%2/2
$this->{'CLOBBER'} || 0
l!l
1828
MC/DC100%1/1
$this->{'CLOBBER'} || 0
$this->{'CLOBBER'}
Truth table100%2/2
A
A= $this->{'CLOBBER'}
Aresult
11
00
54 }
55  
56 sub _set_keep_all {
57 1 ▶ my ($this, $value) = @_;
Subroutine _set_keep_all: called CC=1 SCAR=0.0
58 1 $this->{'KEEP_ALL'} = $value;
59 }
60  
61 sub get_keep_all {
62 63 ▶ my ($this) = @_;
Subroutine get_keep_all: called CC=2 SCAR=6.9
Pod undocumented
63 63 ▶ return $this->{'KEEP_ALL'} || 0;
Condition100%2/2
$this->{'KEEP_ALL'} || 0
l!l
1647
MC/DC100%1/1
$this->{'KEEP_ALL'} || 0
$this->{'KEEP_ALL'}
Truth table100%2/2
A
A= $this->{'KEEP_ALL'}
Aresult
11
00
64 }
65  
66 sub add_keep {
67 1 ▶ my $this = shift;
Subroutine add_keep: called CC=3 SCAR=11.1
Pod undocumented
68 1 ▶ $this->{'KEEP'} ||= {};
Condition50%1/2
$this->{"KEEP"} ||= {}
l!l
01
MC/DC0%0/1
$this->{"KEEP"} ||= {}
$this->{"KEEP"}
Truth table50%1/2
A
A= $this->{"KEEP"}
Aresult
11
00
69 1 for my $field (@_) {
70 1 $this->{'KEEP'}->{$field} = 1;
71 }
72 }
73  
74 sub check_keep {
75 63 ▶ my ($this, $field) = @_;
Subroutine check_keep: called CC=3 SCAR=11.0
Pod undocumented
76  
77 63 ▶ $this->{'KEEP'} ||= {};
Condition100%2/2
$this->{"KEEP"} ||= {}
l!l
567
MC/DC100%1/1
$this->{"KEEP"} ||= {}
$this->{"KEEP"}
Truth table100%2/2
A
A= $this->{"KEEP"}
Aresult
11
00
78 63 ▶ return $this->get_keep_all() || exists($this->{'KEEP'}->{$field});
Condition100%3/3
$this->get_keep_all || exists $this->{'KEEP'}{$field}
l!l&&r!l&&!r
16443
MC/DC100%2/2
$this->get_keep_all || exists $this->{'KEEP'}{$field}
$this->get_keep_all exists $this->{'KEEP'}{$field}
Truth table100%3/3
A || B
A= $this->get_keep_allB= exists $this->{'KEEP'}{$field}
ABresult
1X1
011
000
79 }
80  
81 sub accept_line {
82 42 ▶ my $this = shift;
Subroutine accept_line: called CC=9 SCAR=22.0
Pod undocumented
83 42 my $line = shift;
84  
85 42 my $regex_index = 0;
86 42 for my $regex (@{$this->_get_regexes()}) {
87 84 my ($string, $fields, $pre_flush, $post_flush) = @$regex;
88 84 my $field_prefix = "$regex_index-";
89  
90 84 ▶ if(my @groups = ($line =~ $string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@groups) = $line =~ /$string/) 38 46 84
91 38 my $pairs = $this->get_field_value_pairs(\@groups, $fields, $field_prefix);
92 38 ▶ if(!$this->get_clobber()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->get_clobber) 22 16 38
93 22 foreach my $pair ( @$pairs ) {
94 33 my ($name, $value) = @$pair;
95 33 ▶ if(defined ${$this->{'RECORD'}->guess_key_from_spec($name)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$this->{"RECORD"}->guess_key_from_spec($name);}) 12 21 33
96 12 $pre_flush = 1;
97 }
98 }
99 }
100  
101 38 ▶ if($pre_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_flush) 7 31 38
102 7 $this->flush_record();
103 }
104  
105 38 foreach my $pair ( @$pairs ) {
106 57 my ($name, $value) = @$pair;
107 57 ${$this->{'RECORD'}->guess_key_from_spec($name)} = $value;
108 }
109  
110 38 ▶ if($post_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_flush) 8 30 38
111 8 $this->flush_record();
112 }
113 }
114 84 ++$regex_index;
115 }
116  
117 42 return 1;
118 }
119  
120 sub stream_done {
121 8 ▶ my $this = shift;
Subroutine stream_done: called CC=3 SCAR=11.0
Pod undocumented
122 8 my $record = $this->{'RECORD'};
123  
124 8 ▶ if(!$this->get_clobber() && scalar($record->keys())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->get_clobber and scalar $record->keys) 6 2 8
Condition100%2/2
not $this->get_clobber and scalar $record->keys
!ll
26
MC/DC100%1/1
not $this->get_clobber and scalar $record->keys
not $this->get_clobber
Truth table100%2/2
not(A)
A= not $this->get_clobber
Aresult
00
11
125 6 $this->flush_record();
126 }
127 }
128  
129 sub get_field_value_pairs {
130 38 ▶ my ($this, $groups, $fields, $prefix) = @_;
Subroutine get_field_value_pairs: called CC=6 SCAR=17.9
Pod undocumented
131  
132 38 my @field_names;
133 my %groups_used;
134  
135 38 ▶ for(my $i = 0; $i < @$fields; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields) 51 38 89
136 51 my $field = $fields->[$i];
137 51 my $field_name;
138 51 ▶ if($field =~ /^\$(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^\$(\d+)$/) { } 10 41 51
139 10 my $n = $1 - 1;
140 10 $field_name = $groups->[$n];
141 10 $groups_used{$n} = 1;
142 }
143 else {
144 41 $field_name = $field;
145 }
146 51 push @field_names, $field_name;
147 }
148  
149 38 my @pairs;
150 38 my $pair_index = 0;
151 38 ▶ for(my $i = 0; $i < @$groups; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$groups) 67 38 105
152 67 ▶ if($groups_used{$i}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groups_used{$i}) 10 57 67
153 10 next;
154 }
155 57 ▶ my $field_name = ($pair_index < @field_names) ? $field_names[$pair_index] : ($prefix . $pair_index);
Branch: 1 branch
BranchTrueFalseTotal
$pair_index < @field_names ? : 51 6 57
156 57 push @pairs, [$field_name, $groups->[$i]];
157 57 $pair_index++;
158 }
159  
160 38 return \@pairs;
161 }
162  
163 sub flush_record {
164 21 ▶ my $this = shift;
Subroutine flush_record: called CC=3 SCAR=11.0
Pod undocumented
165 21 my $record = $this->{'RECORD'};
166 21 my $record2 = App::RecordStream::Record->new();
167 21 for my $field ($record->keys()) {
168 63 ▶ if($this->check_keep($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->check_keep($field)) 20 43 63
169 20 $record2->set($field, $record->get($field));
170 }
171 }
172 21 $this->push_record($record);
173 21 $this->{'RECORD'} = $record2;
174 }
175  
176 sub add_help_types {
177 9 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
178 9 $this->use_help_type('keyspecs');
179 }
180  
181 sub usage {
182 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
183  
184 1 my $options = [
185 [ 'no-flush-regex|--regex|--re <regex>', 'Add a normal regex.'],
186 [ 'pre-flush-regex|--pre <regex>', 'Add a regex that flushes before interpretting fields when matched.'],
187 [ 'post-flush-regex|--post <regex>', 'Add a regex that flushes after interpretting fields when matched.'],
188 [ 'double-flush-regex|--double <regex>', 'Add a regex that flushes both before and after interprettying fields when matched.'],
189 [ 'clobber', 'Do not flush records when a field from a match would clobber an already existing field and do not flush at EOF.'],
190 [ 'keep-all', 'Do not clear any fields on a flush.'],
191 [ 'keep <fields>', 'Do not clear this comma separated list of fields on a flush.'],
192 ];
193  
194 1 my $args_string = $this->options_string($options);
195  
196 1 return <<USAGE;
197 Usage: recs-frommultire <args> [<files>]
198 __FORMAT_TEXT__
199 Match multiple regexes against each line of input (or lines of <files>).
200 Various parameters control when the accumulated fields are flushed to output
201 as a record and which, if any, fields are cleared when the record is
202 flushed.
203  
204 By default regexes do not necessarily flush on either side, would-be field
205 collisions cause a flush, EOF causes a flush if any fields are set, and all
206 fields are cleared on a flush.
207 __FORMAT_TEXT__
208  
209 Arguments:
210 $args_string
211  
212 __FORMAT_TEXT__
213 <regex> - Syntax is: '<KEY1>,<KEY2>=REGEX'. KEY field names are optional.
214 The key names may be key specs, see '--help-keyspecs' for more. Field
215 names may not be keygroups. If field matches \$NUM, then that match number
216 in the regex will be used as the field name
217 __FORMAT_TEXT__
218  
219 Examples:
220 Typical use case one: parse several fields on separate lines
221 recs-frommultire --re 'fname,lname=^Name: (.*) (.*)\$' --re 'addr=^Address: (.*)\$'
222 Typical use case two: some fields apply to multiple records ("department" here)
223 recs-frommultire --post 'fname,lname=^Name: (.*) (.*)\$' --re 'department=^Department: (.*)\$' --clobber --keep team
224 USAGE
225 }
226  
227 3 1;
« blib/lib/App/RecordStream/Aggregator/PercentileMap.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Internal/ForField2.pm »