16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.16 |
Version 0.18 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.16'; |
our $VERSION = '0.18'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
64 |
encoding => 'ISO-8859-2', |
encoding => 'ISO-8859-2', |
65 |
recode => 'char pairs', |
recode => 'char pairs', |
66 |
no_progress_bar => 1, |
no_progress_bar => 1, |
67 |
|
input_config => { |
68 |
|
mapping => [ 'foo', 'bar', 'baz' ], |
69 |
|
}, |
70 |
); |
); |
71 |
|
|
72 |
C<module> is low-level file format module. See L<WebPAC::Input::ISIS> and |
C<module> is low-level file format module. See L<WebPAC::Input::ISIS> and |
236 |
|
|
237 |
my $ll_db = $class->new( |
my $ll_db = $class->new( |
238 |
path => $arg->{path}, |
path => $arg->{path}, |
239 |
|
input_config => $arg->{input_config}, |
240 |
# filter => sub { |
# filter => sub { |
241 |
# my ($l,$f_nr) = @_; |
# my ($l,$f_nr) = @_; |
242 |
# return unless defined($l); |
# return unless defined($l); |
286 |
$log->debug("position: $pos\n"); |
$log->debug("position: $pos\n"); |
287 |
|
|
288 |
my $rec = $ll_db->fetch_rec($pos, sub { |
my $rec = $ll_db->fetch_rec($pos, sub { |
289 |
my ($l,$f_nr) = @_; |
my ($l,$f_nr,$debug) = @_; |
290 |
# return unless defined($l); |
# return unless defined($l); |
291 |
# return $l unless ($rec_regex && $f_nr); |
# return $l unless ($rec_regex && $f_nr); |
292 |
|
|
293 |
|
return unless ( defined($l) && defined($f_nr) ); |
294 |
|
|
295 |
|
warn "-=> $f_nr ## |$l|\n" if ($debug); |
296 |
$log->debug("-=> $f_nr ## $l"); |
$log->debug("-=> $f_nr ## $l"); |
297 |
|
|
298 |
# codepage conversion and recode_regex |
# codepage conversion and recode_regex |
305 |
my $c = 0; |
my $c = 0; |
306 |
foreach my $r (@{ $rec_regex->{$f_nr} }) { |
foreach my $r (@{ $rec_regex->{$f_nr} }) { |
307 |
my $old_l = $l; |
my $old_l = $l; |
308 |
eval '$l =~ ' . $r; |
$log->logconfess("expected regex in ", dump( $r )) unless defined($r->{regex}); |
309 |
|
eval '$l =~ ' . $r->{regex}; |
310 |
if ($old_l ne $l) { |
if ($old_l ne $l) { |
311 |
$log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]"); |
my $d = "|$old_l| -> |$l| "; # . $r->{regex}; |
312 |
|
$d .= ' +' . $r->{line} . ' ' . $r->{file} if defined($r->{line}); |
313 |
|
$d .= ' ' . $r->{debug} if defined($r->{debug}); |
314 |
|
$log->debug("MODIFY $d"); |
315 |
|
warn "*** $d\n" if ($debug); |
316 |
|
|
317 |
} |
} |
318 |
$log->error("error applying regex: $r") if ($@); |
$log->error("error applying regex: $r") if ($@); |
319 |
} |
} |
320 |
} |
} |
321 |
|
|
322 |
$log->debug("<=- $f_nr ## $l"); |
$log->debug("<=- $f_nr ## |$l|"); |
323 |
|
warn "<=- $f_nr ## $l\n" if ($debug); |
324 |
return $l; |
return $l; |
325 |
}); |
}); |
326 |
|
|
483 |
|
|
484 |
sub seek { |
sub seek { |
485 |
my $self = shift; |
my $self = shift; |
486 |
my $pos = shift || return; |
my $pos = shift; |
487 |
|
|
488 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
489 |
|
|
490 |
|
$log->logconfess("called without pos") unless defined($pos); |
491 |
|
|
492 |
if ($pos < 1) { |
if ($pos < 1) { |
493 |
$log->warn("seek before first record"); |
$log->warn("seek before first record"); |
494 |
$pos = 1; |
$pos = 1; |
523 |
|
|
524 |
my $out = join("\n", |
my $out = join("\n", |
525 |
map { |
map { |
526 |
my $f = $_ || die "no field"; |
my $f = $_; |
527 |
|
die "no field in ", dump( $s->{fld} ) unless defined( $f ); |
528 |
my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}"; |
my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}"; |
529 |
$max_fld = $v if ($v > $max_fld); |
$max_fld = $v if ($v > $max_fld); |
530 |
|
|
544 |
} |
} |
545 |
|
|
546 |
$o; |
$o; |
547 |
} sort { $a cmp $b } keys %{ $s->{fld} } |
} sort { $a <=> $b } keys %{ $s->{fld} } |
548 |
); |
); |
549 |
|
|
550 |
$log->debug( sub { dump($s) } ); |
$log->debug( sub { dump($s) } ); |
563 |
|
|
564 |
return unless $self->{ll_db}; |
return unless $self->{ll_db}; |
565 |
|
|
566 |
if ($self->{ll_db}->can('dump_rec')) { |
if ($self->{ll_db}->can('dump_ascii')) { |
567 |
return $self->{ll_db}->dump_ascii( $self->{pos} ); |
return $self->{ll_db}->dump_ascii( $self->{pos} ); |
568 |
} else { |
} else { |
569 |
return dump( $self->{ll_db}->fetch_rec( $self->{pos} ) ); |
return dump( $self->{ll_db}->fetch_rec( $self->{pos} ) ); |
570 |
} |
} |
571 |
} |
} |
572 |
|
|
573 |
=head2 modify_record_regexps |
=head2 _get_regex |
574 |
|
|
575 |
Generate hash with regexpes to be applied using l<filter>. |
Helper function called which create regexps to be execute on code. |
576 |
|
|
577 |
my $regexpes = $input->modify_record_regexps( |
_get_regex( 900, 'regex:[0-9]+' ,'numbers' ); |
578 |
900 => { '^a' => { ' : ' => '^b' } }, |
_get_regex( 900, '^b', ' : ^b' ); |
579 |
901 => { '*' => { '^b' => ' ; ' } }, |
|
580 |
); |
It supports perl regexps with C<regex:> prefix to from value and has |
581 |
|
additional logic to skip empty subfields. |
582 |
|
|
583 |
=cut |
=cut |
584 |
|
|
585 |
sub _get_regex { |
sub _get_regex { |
586 |
my ($sf,$from,$to) = @_; |
my ($sf,$from,$to) = @_; |
587 |
|
|
588 |
|
# protect / |
589 |
|
$from =~ s!/!\\/!gs; |
590 |
|
$to =~ s!/!\\/!gs; |
591 |
|
|
592 |
if ($from =~ m/^regex:(.+)$/) { |
if ($from =~ m/^regex:(.+)$/) { |
593 |
$from = $1; |
$from = $1; |
594 |
} else { |
} else { |
595 |
$from = '\Q' . $from . '\E'; |
$from = '\Q' . $from . '\E'; |
596 |
} |
} |
597 |
if ($sf =~ /^\^/) { |
if ($sf =~ /^\^/) { |
598 |
|
my $need_subfield_data = '*'; # no |
599 |
|
# if from is also subfield, require some data in between |
600 |
|
# to correctly skip empty subfields |
601 |
|
$need_subfield_data = '+' if ($from =~ m/^\\Q\^/); |
602 |
return |
return |
603 |
's/\Q'. $sf .'\E([^\^]*?)'. $from .'([^\^]*?)/'. $sf .'$1'. $to .'$2/'; |
's/\Q'. $sf .'\E([^\^]' . $need_subfield_data . '?)'. $from .'([^\^]*?)/'. $sf .'$1'. $to .'$2/'; |
604 |
} else { |
} else { |
605 |
return |
return |
606 |
's/'. $from .'/'. $to .'/g'; |
's/'. $from .'/'. $to .'/g'; |
607 |
} |
} |
608 |
} |
} |
609 |
|
|
610 |
|
|
611 |
|
=head2 modify_record_regexps |
612 |
|
|
613 |
|
Generate hash with regexpes to be applied using L<filter>. |
614 |
|
|
615 |
|
my $regexpes = $input->modify_record_regexps( |
616 |
|
900 => { '^a' => { ' : ' => '^b' } }, |
617 |
|
901 => { '*' => { '^b' => ' ; ' } }, |
618 |
|
); |
619 |
|
|
620 |
|
=cut |
621 |
|
|
622 |
sub modify_record_regexps { |
sub modify_record_regexps { |
623 |
my $self = shift; |
my $self = shift; |
624 |
my $modify_record = {@_}; |
my $modify_record = {@_}; |
636 |
foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { |
foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { |
637 |
my $to = $modify_record->{$f}->{$sf}->{$from}; |
my $to = $modify_record->{$f}->{$sf}->{$from}; |
638 |
#die "no field?" unless defined($to); |
#die "no field?" unless defined($to); |
639 |
$log->debug("transform: |$from| -> |$to|"); |
my $d = "|$from| -> |$to|"; |
640 |
|
$log->debug("transform: $d"); |
641 |
|
|
642 |
my $regex = _get_regex($sf,$from,$to); |
my $regex = _get_regex($sf,$from,$to); |
643 |
push @{ $regexpes->{$f} }, $regex; |
push @{ $regexpes->{$f} }, { regex => $regex, debug => $d }; |
644 |
$log->debug("regex: $regex"); |
$log->debug("regex: $regex"); |
645 |
} |
} |
646 |
} |
} |
651 |
|
|
652 |
=head2 modify_file_regexps |
=head2 modify_file_regexps |
653 |
|
|
654 |
Generate hash with regexpes to be applied using l<filter> from |
Generate hash with regexpes to be applied using L<filter> from |
655 |
pseudo hash/yaml format for regex mappings. |
pseudo hash/yaml format for regex mappings. |
656 |
|
|
657 |
It should be obvious: |
It should be obvious: |
701 |
$log->debug("transform: |$from| -> |$to|"); |
$log->debug("transform: |$from| -> |$to|"); |
702 |
|
|
703 |
my $regex = _get_regex($sf,$from,$to); |
my $regex = _get_regex($sf,$from,$to); |
704 |
push @{ $regexpes->{$f} }, $regex; |
push @{ $regexpes->{$f} }, { |
705 |
|
regex => $regex, |
706 |
|
file => $modify_path, |
707 |
|
line => $., |
708 |
|
}; |
709 |
$log->debug("regex: $regex"); |
$log->debug("regex: $regex"); |
710 |
} |
} |
711 |
} |
} |