--- trunk/lib/WebPAC/Input.pm 2006/09/06 18:08:30 634 +++ trunk/lib/WebPAC/Input.pm 2006/09/25 15:26:12 707 @@ -16,11 +16,11 @@ =head1 VERSION -Version 0.11 +Version 0.13 =cut -our $VERSION = '0.11'; +our $VERSION = '0.13'; =head1 SYNOPSIS @@ -107,7 +107,7 @@ #eval $self->{module} .'->import'; # check if required subclasses are implemented - foreach my $subclass (qw/open_db fetch_rec init/) { + foreach my $subclass (qw/open_db fetch_rec init dump_rec/) { my $n = $self->{module} . '::' . $subclass; if (! defined &{ $n }) { my $missing = "missing $subclass in $self->{module}"; @@ -162,16 +162,16 @@ code_page => 'cp852', limit => 500, offset => 6000, - lookup => $lookup_obj, stats => 1, - lookup_ref => sub { - my ($k,$v) = @_; - # store lookup $k => $v + lookup_coderef => sub { + my $rec = shift; + # store lookups }, modify_records => { 900 => { '^a' => { ' : ' => '^b' } }, 901 => { '*' => { '^b' => ' ; ' } }, }, + modify_file => 'conf/modify/mapping.map', ); By default, C is assumed to be C. @@ -182,13 +182,16 @@ C create optional report about usage of fields and subfields -C is closure to call when adding C<< key => 'value' >> combinations to -lookup. +C is closure to called to save data into lookups C specify mapping from subfields to delimiters or from delimiters to subfields, as well as oprations on fields (if subfield is defined as C<*>. +C is alternative for C above which preserves order and offers +(hopefully) simplier sintax than YAML or perl (see L). This option +overrides C if both exists for same input. + Returns size of database, regardless of C and C parametars, see also C. @@ -204,6 +207,8 @@ $log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef})) if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); + $log->debug( $arg->{lookup_coderef} ? '' : 'not ', "using lookup_coderef"); + $log->logcroak("need path") if (! $arg->{'path'}); my $code_page = $arg->{'code_page'} || 'cp852'; @@ -235,8 +240,15 @@ } - my $rec_regex = $self->modify_record_regexps(%{ $arg->{modify_records} }); - $log->debug("rec_regex: ", Dumper($rec_regex)); + my $rec_regex; + if (my $p = $arg->{modify_file}) { + $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 )); + $rec_regex = $self->modify_record_regexps(%{ $h }); + } + $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex); my ($db, $size) = $self->{open_db}->( $self, path => $arg->{path}, @@ -286,7 +298,7 @@ $log->debug("position: $pos\n"); - my $rec = $self->{fetch_rec}->($self, $db, $pos, sub { + my $rec = $self->{fetch_rec}->($self, $pos, sub { my ($l,$f_nr) = @_; # return unless defined($l); # return $l unless ($rec_regex && $f_nr); @@ -336,7 +348,7 @@ if ($self->{stats}) { # fetch clean record with regexpes applied for statistics - my $rec = $self->{fetch_rec}->($self, $db, $pos); + my $rec = $self->{fetch_rec}->($self, $pos); foreach my $fld (keys %{ $rec }) { $self->{_stats}->{fld}->{ $fld }++; @@ -534,9 +546,22 @@ return $out; } +=head2 dump + +Display humanly readable dump of record + +=cut + +sub dump { + my $self = shift; + + return $self->{dump_rec}->($self, $self->{pos}); + +} + =head2 modify_record_regexps -Generate hash with regexpes to be applied using L. +Generate hash with regexpes to be applied using l. my $regexpes = $input->modify_record_regexps( 900 => { '^a' => { ' : ' => '^b' } }, @@ -545,6 +570,17 @@ =cut +sub _get_regex { + my ($sf,$from,$to) = @_; + if ($sf =~ /^\^/) { + return + 's/\Q'. $sf .'\E([^\^]*?)\Q'. $from .'\E([^\^]*?)/'. $sf .'$1'. $to .'$2/'; + } else { + return + 's/\Q'. $from .'\E/'. $to .'/g'; + } +} + sub modify_record_regexps { my $self = shift; my $modify_record = {@_}; @@ -564,22 +600,73 @@ #die "no field?" unless defined($to); $log->debug("transform: |$from| -> |$to|"); - if ($sf =~ /^\^/) { - my $regex = - 's/\Q'. $sf .'\E(.*?)\Q'. $from .'\E(.*?)/'. $sf .'$1'. $to .'$2/g'; - push @{ $regexpes->{$f} }, $regex; - $log->debug(">>>>> $regex [sf]"); - } else { - my $regex = - 's/\Q'. $from .'\E/'. $to .'/g'; - push @{ $regexpes->{$f} }, $regex; - $log->debug(">>>>> $regex [global]"); - } - + my $regex = _get_regex($sf,$from,$to); + push @{ $regexpes->{$f} }, $regex; + $log->debug("regex: $regex"); } } } + return $regexpes; +} + +=head2 modify_file_regexps + +Generate hash with regexpes to be applied using l from +pseudo hash/yaml format for regex mappings. + +It should be obvious: + + 200 + '^a' + ' : ' => '^e' + ' = ' => '^d' + +In field I<200> find C<'^a'> and then C<' : '>, and replace it with C<'^e'>. +In field I<200> find C<'^a'> and then C<' = '>, and replace it with C<'^d'>. + + my $regexpes = $input->modify_file_regexps( 'conf/modify/common.pl' ); + +On undef path it will just return. + +=cut + +sub modify_file_regexps { + my $self = shift; + + my $modify_path = shift || return; + + my $log = $self->_get_logger(); + + my $regexpes; + + CORE::open(my $fh, $modify_path) || $log->logdie("can't open modify file $modify_path: $!"); + + my ($f,$sf); + + while(<$fh>) { + chomp; + next if (/^#/ || /^\s*$/); + + if (/^\s*(\d+)\s*$/) { + $f = $1; + $log->debug("field: $f"); + next; + } elsif (/^\s*'([^']*)'\s*$/) { + $sf = $1; + $log->die("can't define subfiled before field in: $_") unless ($f); + $log->debug("subfield: $sf"); + } elsif (/^\s*'([^']*)'\s*=>\s*'([^']*)'\s*$/) { + my ($from,$to) = ($1, $2); + + $log->debug("transform: |$from| -> |$to|"); + + my $regex = _get_regex($sf,$from,$to); + push @{ $regexpes->{$f} }, $regex; + $log->debug("regex: $regex"); + } + } + return $regexpes; }