r1362@llin: dpavlin | 2007-10-30 02:46:05 +0100
[webpac2] / lib / WebPAC / Input.pm
index 8955cf5..430d197 100644 (file)
@@ -7,7 +7,7 @@ use blib;
 
 use WebPAC::Common;
 use base qw/WebPAC::Common/;
-use Data::Dumper;
+use Data::Dump qw/dump/;
 use Encode qw/from_to/;
 
 =head1 NAME
@@ -16,11 +16,11 @@ WebPAC::Input - read different file formats into WebPAC
 
 =head1 VERSION
 
-Version 0.14
+Version 0.18
 
 =cut
 
-our $VERSION = '0.14';
+our $VERSION = '0.18';
 
 =head1 SYNOPSIS
 
@@ -64,6 +64,9 @@ Create new input database object.
        encoding => 'ISO-8859-2',
        recode => 'char pairs',
        no_progress_bar => 1,
+       input_config => {
+               mapping => [ 'foo', 'bar', 'baz' ],
+       },
   );
 
 C<module> is low-level file format module. See L<WebPAC::Input::ISIS> and
@@ -102,11 +105,6 @@ sub new {
 
        require $module_path;
 
-       # check if required subclasses are implemented
-       foreach my $subclass (qw/open_db fetch_rec init dump_rec/) {
-               # FIXME
-       }
-
        $self->{'encoding'} ||= 'ISO-8859-2';
 
        $self ? return $self : return undef;
@@ -229,15 +227,16 @@ sub open {
                $log->debug("using modify_file $p");
                $rec_regex = $self->modify_file_regexps( $p );
        } elsif (my $h = $arg->{modify_records}) {
-               $log->debug("using modify_records ", Dumper( $h ));
+               $log->debug("using modify_records ", sub { dump( $h ) });
                $rec_regex = $self->modify_record_regexps(%{ $h });
        }
-       $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex);
+       $log->debug("rec_regex: ", sub { dump($rec_regex) }) if ($rec_regex);
 
        my $class = $self->{module} || $log->logconfess("can't get low-level module name!");
 
        my $ll_db = $class->new(
                path => $arg->{path},
+               input_config => $arg->{input_config} || $self->{input_config},
 #              filter => sub {
 #                      my ($l,$f_nr) = @_;
 #                      return unless defined($l);
@@ -287,10 +286,13 @@ sub open {
                $log->debug("position: $pos\n");
 
                my $rec = $ll_db->fetch_rec($pos, sub {
-                               my ($l,$f_nr) = @_;
+                               my ($l,$f_nr,$debug) = @_;
 #                              return unless defined($l);
 #                              return $l unless ($rec_regex && $f_nr);
 
+                               return unless ( defined($l) && defined($f_nr) );
+
+                               warn "-=> $f_nr ## |$l|\n" if ($debug);
                                $log->debug("-=> $f_nr ## $l");
 
                                # codepage conversion and recode_regex
@@ -303,19 +305,26 @@ sub open {
                                        my $c = 0;
                                        foreach my $r (@{ $rec_regex->{$f_nr} }) {
                                                my $old_l = $l;
-                                               eval '$l =~ ' . $r;
+                                               $log->logconfess("expected regex in ", dump( $r )) unless defined($r->{regex});
+                                               eval '$l =~ ' . $r->{regex};
                                                if ($old_l ne $l) {
-                                                       $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};
+                                                       $d .= ' +' . $r->{line} . ' ' . $r->{file} if defined($r->{line});
+                                                       $d .= ' ' . $r->{debug} if defined($r->{debug});
+                                                       $log->debug("MODIFY $d");
+                                                       warn "*** $d\n" if ($debug);
+
                                                }
                                                $log->error("error applying regex: $r") if ($@);
                                        }
                                }
 
-                               $log->debug("<=- $f_nr ## $l");
+                               $log->debug("<=- $f_nr ## |$l|");
+                               warn "<=- $f_nr ## $l\n" if ($debug);
                                return $l;
                });
 
-               $log->debug(sub { Dumper($rec) });
+               $log->debug(sub { dump($rec) });
 
                if (! $rec) {
                        $log->warn("record $pos empty? skipping...");
@@ -474,10 +483,12 @@ First record in database has position 1.
 
 sub seek {
        my $self = shift;
-       my $pos = shift || return;
+       my $pos = shift;
 
        my $log = $self->_get_logger();
 
+       $log->logconfess("called without pos") unless defined($pos);
+
        if ($pos < 1) {
                $log->warn("seek before first record");
                $pos = 1;
@@ -512,19 +523,27 @@ sub stats {
 
        my $out = join("\n",
                map {
-                       my $f = $_ || die "no field";
+                       my $f = $_;
+                       die "no field in ", dump( $s->{fld} ) unless defined( $f );
                        my $v = $s->{fld}->{$f} || die "no s->{fld}->{$f}";
                        $max_fld = $v if ($v > $max_fld);
 
                        my $o = sprintf("%4s %d ~", $f, $v);
 
                        if (defined($s->{sf}->{$f})) {
+                               my @subfields = keys %{ $s->{sf}->{$f} };
+warn "$f has subfields ",dump( @subfields );
                                map {
                                        $o .= sprintf(" %s:%d%s", $_, 
                                                $s->{sf}->{$f}->{$_}->{count},
                                                $s->{sf}->{$f}->{$_}->{repeatable} ? '*' : '',
                                        );
-                               } sort keys %{ $s->{sf}->{$f} };
+                               } (
+                                       # first indicators
+                                       sort( grep(  /^i[12]$/, @subfields ) ),
+                                       # then subfileds
+                                       sort( grep( !/^i[12]$/, @subfields ) ),
+                               );
                        }
 
                        if (my $v_r = $s->{repeatable}->{$f}) {
@@ -532,49 +551,81 @@ sub stats {
                        }
 
                        $o;
-               } sort { $a cmp $b } keys %{ $s->{fld} }
+               } sort { $a <=> $b } keys %{ $s->{fld} }
        );
 
-       $log->debug( sub { Dumper($s) } );
+       $log->debug( sub { dump($s) } );
 
        return $out;
 }
 
-=head2 dump
+=head2 dump_ascii
 
 Display humanly readable dump of record
 
 =cut
 
-sub dump {
+sub dump_ascii {
        my $self = shift;
 
-       return $self->{ll_db}->dump_rec( $self->{pos} );
+       return unless $self->{ll_db};
 
+       if ($self->{ll_db}->can('dump_ascii')) {
+               return $self->{ll_db}->dump_ascii( $self->{pos} );
+       } else {
+               return dump( $self->{ll_db}->fetch_rec( $self->{pos} ) );
+       }
 }
 
-=head2 modify_record_regexps
+=head2 _get_regex
 
-Generate hash with regexpes to be applied using l<filter>.
+Helper function called which create regexps to be execute on code.
 
-  my $regexpes = $input->modify_record_regexps(
-               900 => { '^a' => { ' : ' => '^b' } },
-               901 => { '*' => { '^b' => ' ; ' } },
-  );
+  _get_regex( 900, 'regex:[0-9]+' ,'numbers' );
+  _get_regex( 900, '^b', ' : ^b' );
+
+It supports perl regexps with C<regex:> prefix to from value and has
+additional logic to skip empty subfields.
 
 =cut
 
 sub _get_regex {
        my ($sf,$from,$to) = @_;
+
+       # protect /
+       $from =~ s!/!\\/!gs;
+       $to =~ s!/!\\/!gs;
+
+       if ($from =~ m/^regex:(.+)$/) {
+               $from = $1;
+       } else {
+               $from = '\Q' . $from . '\E';
+       }
        if ($sf =~ /^\^/) {
+               my $need_subfield_data = '*';   # no
+               # if from is also subfield, require some data in between
+               # to correctly skip empty subfields
+               $need_subfield_data = '+' if ($from =~ m/^\\Q\^/);
                return
-                       's/\Q'. $sf .'\E([^\^]*?)\Q'. $from .'\E([^\^]*?)/'. $sf .'$1'. $to .'$2/';
+                       's/\Q'. $sf .'\E([^\^]' . $need_subfield_data . '?)'. $from .'([^\^]*?)/'. $sf .'$1'. $to .'$2/';
        } else {
                return
-                       's/\Q'. $from .'\E/'. $to .'/g';
+                       's/'. $from .'/'. $to .'/g';
        }
 }
 
+
+=head2 modify_record_regexps
+
+Generate hash with regexpes to be applied using L<filter>.
+
+  my $regexpes = $input->modify_record_regexps(
+               900 => { '^a' => { ' : ' => '^b' } },
+               901 => { '*' => { '^b' => ' ; ' } },
+  );
+
+=cut
+
 sub modify_record_regexps {
        my $self = shift;
        my $modify_record = {@_};
@@ -592,10 +643,11 @@ sub modify_record_regexps {
                        foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) {
                                my $to = $modify_record->{$f}->{$sf}->{$from};
                                #die "no field?" unless defined($to);
-                               $log->debug("transform: |$from| -> |$to|");
+                               my $d = "|$from| -> |$to|";
+                               $log->debug("transform: $d");
 
                                my $regex = _get_regex($sf,$from,$to);
-                               push @{ $regexpes->{$f} }, $regex;
+                               push @{ $regexpes->{$f} }, { regex => $regex, debug => $d };
                                $log->debug("regex: $regex");
                        }
                }
@@ -606,7 +658,7 @@ sub modify_record_regexps {
 
 =head2 modify_file_regexps
 
-Generate hash with regexpes to be applied using l<filter> from
+Generate hash with regexpes to be applied using L<filter> from
 pseudo hash/yaml format for regex mappings.
 
 It should be obvious:
@@ -656,7 +708,11 @@ sub modify_file_regexps {
                        $log->debug("transform: |$from| -> |$to|");
 
                        my $regex = _get_regex($sf,$from,$to);
-                       push @{ $regexpes->{$f} }, $regex;
+                       push @{ $regexpes->{$f} }, {
+                               regex => $regex,
+                               file => $modify_path,
+                               line => $.,
+                       };
                        $log->debug("regex: $regex");
                }
        }