--- trunk/lib/WebPAC/Input.pm 2006/10/25 17:10:08 761 +++ trunk/lib/WebPAC/Input.pm 2007/11/28 22:51:43 1076 @@ -7,7 +7,7 @@ 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 @@ =head1 VERSION -Version 0.14 +Version 0.18 =cut -our $VERSION = '0.14'; +our $VERSION = '0.18'; =head1 SYNOPSIS @@ -64,6 +64,9 @@ encoding => 'ISO-8859-2', recode => 'char pairs', no_progress_bar => 1, + input_config => { + mapping => [ 'foo', 'bar', 'baz' ], + }, ); C is low-level file format module. See L and @@ -102,11 +105,6 @@ 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 @@ $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 @@ $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 @@ 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..."); @@ -344,8 +353,8 @@ foreach my $fld (keys %{ $rec }) { $self->{_stats}->{fld}->{ $fld }++; - $log->logdie("invalid record fild $fld, not ARRAY") - unless (ref($rec->{ $fld }) eq 'ARRAY'); + #$log->logdie("invalid record fild $fld, not ARRAY") + next unless (ref($rec->{ $fld }) eq 'ARRAY'); foreach my $row (@{ $rec->{$fld} }) { @@ -474,10 +483,12 @@ 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,26 @@ 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} }; map { $o .= sprintf(" %s:%d%s", $_, $s->{sf}->{$f}->{$_}->{count}, $s->{sf}->{$f}->{$_}->{repeatable} ? '*' : '', ); - } sort keys %{ $s->{sf}->{$f} }; + } ( + # first indicators and other special subfields + sort( grep { length($_) > 1 } @subfields ), + # then subfileds (single char) + sort( grep { length($_) == 1 } @subfields ), + ); } if (my $v_r = $s->{repeatable}->{$f}) { @@ -532,49 +550,87 @@ } $o; - } sort { $a cmp $b } keys %{ $s->{fld} } + } sort { + if ( $a =~ m/^\d+$/ && $b =~ m/^\d+$/ ) { + $a <=> $b + } else { + $a cmp $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. +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 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. + + my $regexpes = $input->modify_record_regexps( + 900 => { '^a' => { ' : ' => '^b' } }, + 901 => { '*' => { '^b' => ' ; ' } }, + ); + +=cut + sub modify_record_regexps { my $self = shift; my $modify_record = {@_}; @@ -592,10 +648,11 @@ 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 +663,7 @@ =head2 modify_file_regexps -Generate hash with regexpes to be applied using l from +Generate hash with regexpes to be applied using L from pseudo hash/yaml format for regex mappings. It should be obvious: @@ -656,7 +713,11 @@ $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"); } }