blib/lib/oEdtk/RecordParser.pm

filter: Statementstmt 14.9% 16 / 107 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 20.0% 4 / 20 Podpod 100% 14 / 14 totaltotal 16.9% 34 / 201 CC 24 SCAR 42.6
CC
24
Cov
10%
next
182.0
new
56.0
_kill_non_latin_chars
42.0
CRAP 70.7
« blib/lib/oEdtk/SchemaScan.pm ↑ Summary lib/oEdtk/TexDoc.pm »
1 package oEdtk::RecordParser;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = 2.1131;
8  
9 # FINE-TUNE PARAMETRIZATION
10 2 my $_denormalized_record = "OPTION";
11  
12 # my $_denormalized_split_motif=;
13  
14 0 ▶sub dumper {
Subroutine dumper: not called CC=1 SCAR=6.9
Pod documented
15 0 my $self = shift;
16  
17 0 return Dumper(shift);
18 }
19  
20 # Build the seek plan from the seek-key table: the keys in lexicographic order,
21 # each paired with its compiled match. next() iterates this instead of sorting
22 # and re-compiling the keys on every record. The pattern is identical to the
23 # one next() used to build inline (/^$key(.*)$/), so the captures ($1 the key --
24 # or its innermost group -- $2 the remainder) are unchanged.
25 0 ▶sub _seek_plan {
Subroutine _seek_plan: not called CC=1 SCAR=6.9
26 0 my ($seek_keys) = @_;
27 0 return [ map { [ $_, qr/^$_(.*)$/ ] } sort keys %$seek_keys ];
28 }
29  
30 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
31 0 my ( $class, $fh, %records ) = @_;
32 0 ▶ defined($fh) or die "ERROR: not defined filhandle $fh : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or die "ERROR: not defined filhandle $fh : $!\n" 0 0 0
33 0 my %seek_keys;
34  
35 # foreach (values %records) {
36 # if (defined($_) && (!blessed($_) || !$_->isa('oEdtk::Record'))) {
37 # die "ERROR: oEdtk::RecordParser::new only accepts oEdtk::Record objects in the hash\n";
38 # }
39 # $seek_keys{$_->{'seek_key'}} .= $_->{'id_key'};
40 # $seek_keys{$_->{'seek_key'}} .= keys ($records->{$id});
41 # warn "INFO: seek_key = ". $_->{'seek_key'} ." = ".$seek_keys{$_->{'seek_key'}}."\n";
42 # }
43  
44 0 foreach my $key ( keys %records ) {
45 0 my $object = $records{$key};
46 0 ▶ if ( defined($object) && ( !blessed($object) || !$object->isa('oEdtk::Record') ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $object and not builtin::blessed($object) && $object->isa('oEdtk::Record')) { } 0 0 0
elsif (defined $object) { } 0 0 0
Condition0%0/6
defined $object and not builtin::blessed($object) && $object->isa("oEdtk::Record")
!ll&&!rl&&r
000
builtin::blessed($object) && $object->isa('oEdtk::Record')
!ll&&!rl&&r
000
MC/DC0%0/3
defined $object and not builtin::blessed($object) && $object->isa("oEdtk::Record")
defined $object builtin::blessed($object) $object->isa('oEdtk::Record')
Truth table0%0/4
A and not(B && C)
A= defined $objectB= builtin::blessed($object)C= $object->isa('oEdtk::Record')
ABCresult
0XX0
1110
10X1
1101
47 0 die "ERROR: oEdtk::RecordParser::new only accepts oEdtk::Record objects in the hash\n";
48  
49 }
50 elsif ( defined($object) ) { # ignore undef records
51  
52 # warn "DEBUG: $key = ". $object->{'seek_key'} ." - isa('oEdtk::Record') ? ". $object->isa('oEdtk::Record') ." \n";
53 # $seek_keys{$object->{'seek_key'}} .= $object->{'id_key'};
54 0 $seek_keys{ $object->{'seek_key'} } .= $key;
55  
56 # INFO: seek_key = LIGNE.{153}(.{10}) = L5L6T3L7L4C1T6T4T7F5L3T5
57 # warn "INFO: seek_key = ". $object->{'seek_key'} ." = ".$seek_keys{$object->{'seek_key'}}."\n";
58 }
59 }
60  
61 0 my $self = {
62 input => $fh,
63 records => \%records,
64 seek_keys => \%seek_keys,
65 seek_plan => _seek_plan( \%seek_keys ),
66 line => '',
67 skip_line => 'FLUX',
68 mute_record => 'ENTETE',
69 mute_id => 'ENT',
70 line_record => 'LIGNE',
71 key_offset => 153,
72 key_size => 10,
73 denormalized => 'OPTION',
74 denormalized_option => 'OPTION',
75 denormalized_on => 'ON',
76 denormalized_split_motif => "\x{0}|\x{1}|\x{2}"
77 };
78  
79 0 bless $self, $class;
80 0 return $self;
81 }
82  
83 0 ▶sub set_skip_line {
Subroutine set_skip_line: not called CC=1 SCAR=6.9
Pod documented
84 0 my ( $self, $value ) = @_;
85  
86 0 $self->{'skip_line'} .= $value;
87 }
88  
89 0 ▶sub set_mute_record {
Subroutine set_mute_record: not called CC=1 SCAR=6.9
Pod documented
90 0 my ( $self, $value ) = @_;
91  
92 0 $self->{'mute_record'} .= $value;
93 }
94  
95 0 ▶sub set_mute_id {
Subroutine set_mute_id: not called CC=1 SCAR=6.9
Pod documented
96 0 my ( $self, $value ) = @_;
97  
98 0 $self->{'mute_id'} .= $value;
99 }
100  
101 0 ▶sub set_line_record {
Subroutine set_line_record: not called CC=1 SCAR=6.9
Pod documented
102 0 my ( $self, $value ) = @_;
103  
104 0 $self->{'line_record'} .= $value;
105 }
106  
107 0 ▶sub set_key_offset {
Subroutine set_key_offset: not called CC=1 SCAR=6.9
Pod documented
108 0 my ( $self, $value ) = @_;
109  
110 0 $self->{'key_offset'} .= $value;
111 }
112  
113 0 ▶sub set_key_size {
Subroutine set_key_size: not called CC=1 SCAR=6.9
Pod documented
114 0 my ( $self, $value ) = @_;
115  
116 0 $self->{'key_size'} .= $value;
117 }
118  
119 # Set the denormalized-record options. The mainline keeps a single
120 # "denormalized" key that next() reads; the fork splits it into an
121 # "option" and an "on" key. set_denormalized_record feeds both fork
122 # keys so the mainline and fork nomenclatures stay equivalent.
123 0 ▶sub set_denormalized_record {
Subroutine set_denormalized_record: not called CC=1 SCAR=6.9
Pod documented
124 0 my ( $self, $value ) = @_;
125  
126 0 $self->{'denormalized'} .= $value;
127 0 $self->{'denormalized_option'} .= $value;
128 0 $self->{'denormalized_on'} .= $value;
129 }
130  
131 # Fork accessors (S42): each writes only its own key, faithfully to F.
132 0 ▶sub set_denormalized_option_record {
Subroutine set_denormalized_option_record: not called CC=1 SCAR=6.9
Pod documented
133 0 my ( $self, $value ) = @_;
134  
135 0 $self->{'denormalized_option'} .= $value;
136 }
137  
138 0 ▶sub set_denormalized_on_record {
Subroutine set_denormalized_on_record: not called CC=1 SCAR=6.9
Pod documented
139 0 my ( $self, $value ) = @_;
140  
141 0 $self->{'denormalized_on'} .= $value;
142 }
143  
144 0 ▶sub add_motif_to_denormalized_split {
Subroutine add_motif_to_denormalized_split: not called CC=1 SCAR=6.9
Pod documented
145 0 my ( $self, $motif ) = @_;
146  
147 0 $self->{'denormalized_split_motif'} .= "|" . $motif;
148 }
149  
150 0 ▶sub set_motif_to_denormalized_split {
Subroutine set_motif_to_denormalized_split: not called CC=1 SCAR=6.9
Pod documented
151 0 my ( $self, $motif ) = @_;
152  
153 0 $self->{'denormalized_split_motif'} = $motif;
154 }
155  
156 # True when the configured cleanup of non-Latin-1 bytes (the DEL character and
157 # the C1 controls) is on. EDTK_RP_KILL_NON_LATIN is a COMPO key, off by
158 # default: the mainline assumes its input is sane. config_read() re-parses the
159 # ini on every call, so the switch is read once per process and cached; a
160 # missing or unreadable configuration leaves it off.
161 2 my $KILL_NON_LATIN;
162  
163 0 ▶sub _kill_non_latin_chars {
Subroutine _kill_non_latin_chars: not called CC=6 SCAR=37.4
164 0 my $cfg = shift;
165 0 ▶ if ( !defined $cfg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 0 0
166 0 ▶ unless ( defined $KILL_NON_LATIN ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $KILL_NON_LATIN) 0 0 0
167 0 ▶ my $read = eval { require oEdtk::Config; oEdtk::Config::config_read('COMPO') } || {};
Condition0%0/2
eval { do { require oEdtk::Config; oEdtk::Config::config_read('COMPO') } } || {}
l!l
00
MC/DC0%0/1
eval { do { require oEdtk::Config; oEdtk::Config::config_read('COMPO') } } || {}
eval { do { require oEdtk::Config; oEdtk::Config::config_read('COMPO') } }
Truth table0%0/2
A
A= eval { do { require oEdtk::Config; oEdtk::Config::config_read('COMPO') } }
Aresult
11
00
168 0 $KILL_NON_LATIN = _kill_non_latin_chars($read);
169 }
170 0 return $KILL_NON_LATIN;
171 }
172 0 ▶ return ( ref $cfg && $cfg->{'EDTK_RP_KILL_NON_LATIN'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $cfg && $cfg->{'EDTK_RP_KILL_NON_LATIN'} ? : 0 0 0
Condition0%0/3
ref $cfg and $cfg->{"EDTK_RP_KILL_NON_LATIN"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $cfg and $cfg->{"EDTK_RP_KILL_NON_LATIN"}
ref $cfg $cfg->{"EDTK_RP_KILL_NON_LATIN"}
Truth table0%0/3
A and B
A= ref $cfgB= $cfg->{"EDTK_RP_KILL_NON_LATIN"}
ABresult
0X0
100
111
173 }
174  
175 # Parse and return the next record in the stream.
176 0 ▶sub next {
Subroutine next: not called CC=13 SCAR=52.0
Pod documented
177 0 my ($self) = @_;
178  
179 0 my $denormalized_split_motif = $self->{'denormalized_split_motif'};
180 0 my $denormalized = $self->{'denormalized'};
181 0 my $records = $self->{'records'};
182 0 my $seek_keys = $self->{'seek_keys'};
183 0 ▶ my $seek_plan = $self->{'seek_plan'} // _seek_plan($seek_keys);
Condition0%0/3
$self->{"seek_plan"} // _seek_plan($seek_keys)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"seek_plan"} // _seek_plan($seek_keys)
$self->{"seek_plan"} _seek_plan($seek_keys)
Truth table0%0/3
A // B
A= $self->{"seek_plan"}B= _seek_plan($seek_keys)
ABresult
1X1
011
000
184 0 my $skip_line = $self->{'skip_line'};
185 0 my $mute_record = $self->{'mute_record'};
186 0 my $line_record = $self->{'line_record'};
187 0 my $key_offset = $self->{'key_offset'};
188 0 my $key_size = $self->{'key_size'};
189 0 my $fh = $self->{'input'};
190 0 ▶ defined($fh) or die "ERROR: not defined filhandle $fh : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or die "ERROR: not defined filhandle $fh : $!\n" 0 0 0
191  
192 0 my ( $id, $data ) = ( "", "" );
193 do {
194 0 my $line = <$fh>;
195  
196 # Skip lines starting with FLUX.
197 0 ▶ while ( defined($line) && $line =~ /^$skip_line/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ /^$skip_line/) 0 0 0
Condition0%0/3
defined $line and $line =~ /^$skip_line/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line and $line =~ /^$skip_line/
defined $line $line =~ /^$skip_line/
Truth table0%0/3
A and B
A= defined $lineB= $line =~ /^$skip_line/
ABresult
0X0
100
111
198 0 $line = <$fh>;
199 }
200 0 ▶ return () unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
201  
202 # Strip the non-Latin-1 bytes a mis-encoded input may carry (the fork
203 # does this unconditionally; the mainline gates it on a key, off by
204 # default).
205 0 ▶ if ( _kill_non_latin_chars() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kill_non_latin_chars()) 0 0 0
206 0 require oEdtk::Utils;
207 0 oEdtk::Utils::kill_non_latin_1_9_chars_in_place( \$line );
208 }
209  
210 0 chomp $line;
211 0 $self->{'line'} = $line;
212  
213 SEEK: {
214 0 ▶ if ( $line =~ /^$denormalized(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$denormalized(.*)$/) { } 0 0 0
215 0 $data = $1;
216 0 $id = $denormalized;
217  
218 }
219 else {
220 0 foreach my $entry ( @{$seek_plan} ) {
221  
222 # warn "DEBUG: seek => $entry->[0] line $.\n";
223 0 ▶ if ( $line =~ $entry->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$entry->[1]/) 0 0 0
224 0 ( $id, $data ) = ( $1, $2 );
225 0 $id =~ s/\s*//g;
226  
227 # if (exists $records->{$key}){
228 # ($id, $data) = ($key, $right);
229 # warn "DEBUG: success search_key = $entry->[0], for keys $id line $.\n";
230 0 last SEEK;
231  
232 # } else {
233  
234 }
235 }
236  
237 # warn "DEBUG: search_key = $entry->[0], for keys : ". $seek_keys->{$entry->[0]} ."\n";
238 }
239 0 warn "INFO: UNKNOWN RECORD (line $.)=>$line\n";
240 0 warn "INFO: IGNORING UNKNOWN RECORD (line $.)\n";
241  
242 # if ($line =~ /^$mute_record.(.*)/) {
243 # ($id, $data) = ($self->{'mute_id'}, " ".$1);
244 #
245 # } elsif ($line =~ /^$line_record.{$key_offset}(.{$key_size})(.*)$/) { # xxxxx to evolve here to take record keys on 2 chars / 4 chars or more + revisit the parametrizable length of headers and keys
246 # # set the record identifier and pass the record, key included:
247 # # the fields_offset is handled in the record object
248 # $data = $1.$2;
249 # $id = $1;
250 # $id =~s/\s*//g;
251 # if (!exists $records->{$id}) {
252 # die "ERROR: Unexpected record identifier: $id\n";
253 # }
254 #
255 # } else {
256 # die "ERROR: Unexpected line format (line $.): $line\n";
257 # }
258 # }
259 }
260 0 ▶ } while ( $id ne $denormalized && !defined( $records->{$id} ) );
Branch: 1 branch
BranchTrueFalseTotal
$id ne $denormalized and not defined $records->{$id} and do { do { my $line = readline $fh; while (defined $line and $line =~ /^$skip_line/) { $line = readline $fh; } ; return () unless defined $line; if (_kill_non_latin_chars()) { require oEdtk::Utils; oEdtk::Utils::kill_non_latin_1_9_chars_in_place(\$line); } ; chomp $line; $self->{"line"} = $line; SEEK: { if ($line =~ /^$denormalized(.*)$/) { $data = $1; $id = $denormalized; } else { foreach my $entry (@{$seek_plan;}) { if ($line =~ /$entry->[1]/) { () = ($1, $2); $id =~ s/\s*//g; last SEEK; } ; } ; } ; warn "INFO: UNKNOWN RECORD (line $.)=>$line\n"; warn "INFO: IGNORING UNKNOWN RECORD (line $.)\n"; } } } 0 0 0
Condition0%0/3
$id ne $denormalized and not defined $records->{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$id ne $denormalized and not defined $records->{$id}
$id ne $denormalized not defined $records->{$id}
Truth table0%0/3
A and not(B)
A= $id ne $denormalizedB= not defined $records->{$id}
ABresult
0X0
100
111
261  
262 # DENORMALIZED RECORD SHOULD BE AT THE END OF DATA STREAM
263 # TO REVIEW
264  
265 0 ▶ if ( $id eq $denormalized ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $denormalized) 0 0 0
266  
267 # my @data = split(/(?:$denormalized_split_motif)+/, $data);
268 0 my @data = split( /(?:$denormalized_split_motif)/, $data );
269  
270 # my @data = split(/(?:\x{0}|\x{1}|\x{2})+/, $data);
271 # my @data = split(/(?:\x{0}|\x{1}|\x{2}|\x{20})+/, $data);
272 # my @data = split(/(?:\(?:\x{0}|\x{20})+(?:\x{1}|\x{2})+/, $data);
273 # my @data = split($_denormalized_split_motif, $data);
274 0 return ( $id, \@data );
275 }
276  
277 0 my $rec = $records->{$id};
278 0 my %vals = $rec->parse($data);
279 0 return ( $id, \%vals );
280 }
281  
282 2 1;
283  
284 =head1 NAME
285  
286 oEdtk::RecordParser - stream reader and dispatcher over an Infinite flux
287  
288 =head1 SYNOPSIS
289  
290 use oEdtk::RecordParser;
291  
292 my $p = oEdtk::RecordParser->new($fh, %records);
293 while ( my ($id, $vals) = $p->next ) {
294 # $id is the record identifier, $vals a hashref (or an arrayref
295 # for the denormalized record)
296 }
297  
298 =head1 DESCRIPTION
299  
300 Reads records from an Infinite flux and dispatches each line to the matching
301 C<oEdtk::Record>. A compiled seek plan (the keys in lexicographic order, each
302 paired with its compiled match) is built once in L</new>, so L</next> does not
303 re-sort or re-compile the keys on every record.
304  
305 =head1 METHODS
306  
307 =head2 new
308  
309 my $p = oEdtk::RecordParser->new($fh, %records);
310  
311 Build a parser over the file handle C<$fh> and the C<%records> table
312 (identifier to C<oEdtk::Record>). Undef records are ignored; a non-record value
313 is an error.
314  
315 =head2 next
316  
317 my ($id, $vals) = $p->next;
318  
319 Read and dispatch the next record. C<FLUX> lines are skipped; the denormalized
320 record returns an array reference of split fields, any other record returns a
321 hash reference of parsed values. An exhausted stream returns an empty list.
322  
323 =head2 dumper
324  
325 my $text = $p->dumper($value);
326  
327 Return C<Data::Dumper::Dumper> of C<$value> (debug helper).
328  
329 =head2 set_skip_line
330  
331 $p->set_skip_line($value);
332  
333 Append C<$value> to the prefix that marks a line to skip.
334  
335 =head2 set_mute_record
336  
337 $p->set_mute_record($value);
338  
339 Append C<$value> to the muted-record name.
340  
341 =head2 set_mute_id
342  
343 $p->set_mute_id($value);
344  
345 Append C<$value> to the muted-record identifier.
346  
347 =head2 set_line_record
348  
349 $p->set_line_record($value);
350  
351 Append C<$value> to the line-record name.
352  
353 =head2 set_key_offset
354  
355 $p->set_key_offset($value);
356  
357 Append C<$value> to the key offset.
358  
359 =head2 set_key_size
360  
361 $p->set_key_size($value);
362  
363 Append C<$value> to the key size.
364  
365 =head2 set_denormalized_record
366  
367 $p->set_denormalized_record($value);
368  
369 Set the denormalized-record name on both the mainline and the fork keys.
370  
371 =head2 set_denormalized_option_record
372  
373 $p->set_denormalized_option_record($value);
374  
375 Set the fork's denormalized-option key only.
376  
377 =head2 set_denormalized_on_record
378  
379 $p->set_denormalized_on_record($value);
380  
381 Set the fork's denormalized-on key only.
382  
383 =head2 add_motif_to_denormalized_split
384  
385 $p->add_motif_to_denormalized_split($motif);
386  
387 Append C<$motif> to the split pattern used for the denormalized record.
388  
389 =head2 set_motif_to_denormalized_split
390  
391 $p->set_motif_to_denormalized_split($motif);
392  
393 Replace the split pattern used for the denormalized record.
394  
395 The private helpers C<_seek_plan> and C<_kill_non_latin_chars> build the seek
396 plan and gate the non-Latin-1 cleanup; they are not part of the public API.
397  
398 =head1 AUTHOR
399  
400 The oEdtk team.
401  
402 =cut
« blib/lib/oEdtk/SchemaScan.pm ↑ Summary lib/oEdtk/TexDoc.pm »