r810@llin: dpavlin | 2006-07-05 21:53:01 +0200
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 5 Jul 2006 19:52:45 +0000 (19:52 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 5 Jul 2006 19:52:45 +0000 (19:52 +0000)
 change of parametars to WebPAC::Input

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@585 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Input.pm
run.pl
t/6-unit.t

index 298340d..baafa95 100644 (file)
@@ -16,11 +16,11 @@ WebPAC::Input - read different file formats into WebPAC
 
 =head1 VERSION
 
-Version 0.05
+Version 0.07
 
 =cut
 
-our $VERSION = '0.05';
+our $VERSION = '0.07';
 
 =head1 SYNOPSIS
 
@@ -44,7 +44,6 @@ Perhaps a little code snippet.
     my $db = WebPAC::Input->new(
        module => 'WebPAC::Input::ISIS',
                config => $config,
-               lookup => $lookup_obj,
                low_mem => 1,
     );
 
@@ -64,7 +63,7 @@ Create new input database object.
 
   my $db = new WebPAC::Input(
        module => 'WebPAC::Input::MARC',
-       code_page => 'ISO-8859-2',
+       encoding => 'ISO-8859-2',
        low_mem => 1,
        recode => 'char pairs',
        no_progress_bar => 1,
@@ -73,7 +72,7 @@ Create new input database object.
 C<module> is low-level file format module. See L<WebPAC::Input::Isis> and
 L<WebPAC::Input::MARC>.
 
-Optional parametar C<code_page> specify application code page (which will be
+Optional parametar C<encoding> specify application code page (which will be
 used internally). This should probably be your terminal encoding, and by
 default, it C<ISO-8859-2>.
 
@@ -96,6 +95,9 @@ sub new {
 
        my $log = $self->_get_logger;
 
+       $log->logconfess("code_page argument is not suppored any more. change it to encoding") if ($self->{lookup});
+       $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_ref") if ($self->{lookup});
+
        $log->logconfess("specify low-level file format module") unless ($self->{module});
        my $module = $self->{module};
        $module =~ s#::#/#g;
@@ -121,7 +123,7 @@ sub new {
                $self->{init}->($self, @_);
        }
 
-       $self->{'code_page'} ||= 'ISO-8859-2';
+       $self->{'encoding'} ||= 'ISO-8859-2';
 
        # running with low_mem flag? well, use DBM::Deep then.
        if ($self->{'low_mem'}) {
@@ -163,6 +165,10 @@ This function will read whole database in memory and produce lookups.
        offset => 6000,
        lookup => $lookup_obj,
        stats => 1,
+       lookup_ref => sub {
+               my ($k,$v) = @_;
+               # store lookup $k => $v
+       },
  );
 
 By default, C<code_page> is assumed to be C<852>.
@@ -173,6 +179,9 @@ C<limit> is optional parametar to read just C<limit> records from database
 
 C<stats> create optional report about usage of fields and subfields
 
+C<lookup_coderef> is closure to call when adding key => $value combinations to
+lookup.
+
 Returns size of database, regardless of C<offset> and C<limit>
 parametars, see also C<size>.
 
@@ -184,6 +193,10 @@ sub open {
 
        my $log = $self->_get_logger();
 
+       $log->logconfess("lookup argument is not suppored any more. rewrite call to lookup_coderef") if ($arg->{lookup});
+       $log->logconfess("lookup_coderef must be CODE, not ",ref($arg->{lookup_coderef}))
+               if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE');
+
        $log->logcroak("need path") if (! $arg->{'path'});
        my $code_page = $arg->{'code_page'} || '852';
 
@@ -194,7 +207,7 @@ sub open {
        }
 
        # create Text::Iconv object
-       $self->{iconv} = Text::Iconv->new($code_page,$self->{'code_page'});
+       $self->{iconv} = Text::Iconv->new($code_page,$self->{'encoding'});      ## FIXME remove!
 
        my $filter_ref;
 
@@ -259,7 +272,7 @@ sub open {
        # store size for later
        $self->{size} = ($to_rec - $from_rec) ? ($to_rec - $from_rec + 1) : 0;
 
-       $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{code_page}", $self->{stats} ? ' [stats]' : '');
+       $log->info("processing $self->{size}/$size records [$from_rec-$to_rec] convert $code_page -> $self->{encoding}", $self->{stats} ? ' [stats]' : '');
 
        # read database
        for (my $pos = $from_rec; $pos <= $to_rec; $pos++) {
@@ -283,7 +296,7 @@ sub open {
                }
 
                # create lookup
-               $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'});
+               $arg->{'lookup_coderef'}->( $rec ) if ($rec && $arg->{'lookup_coderef'});
 
                # update counters for statistics
                if ($self->{stats}) {
diff --git a/run.pl b/run.pl
index 85a293a..66b51a0 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -8,7 +8,7 @@ use lib './lib';
 
 use WebPAC::Common 0.02;
 use WebPAC::Lookup;
-use WebPAC::Input 0.03;
+use WebPAC::Input 0.07;
 use WebPAC::Store 0.03;
 use WebPAC::Normalize 0.11;
 use WebPAC::Output::TT;
@@ -233,10 +233,13 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
 
                my $input_db = new WebPAC::Input(
                        module => $input_module,
-                       code_page => $config->{webpac}->{webpac_encoding},
+                       encoding => $config->{webpac}->{webpac_encoding},
                        limit => $limit || $input->{limit},
                        offset => $offset,
-                       lookup => $lookup,
+                       lookup_coderef => sub {
+                               my $rec = shift || return;
+                               $lookup->add( $rec );
+                       },
                        recode => $input->{recode},
                        stats => $stats,
                );
index 5658ce6..480e7bd 100755 (executable)
@@ -49,7 +49,10 @@ ok(my $isis = new WebPAC::Input(
 ok(my $maxmfn = $isis->open(
        path => $isis_file,
        code_page => '852',             # database encoding
-       lookup => $lookup,
+       lookup_coderef => sub {
+               my $rec = shift || return;
+               $lookup->add( $rec );
+       },
 ), "Input::ISIS->open");
 
 ok(my $path = tempdir( CLEANUP => 1 ), "path");