/[webpac]/trunk2/lib/WebPAC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk2/lib/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 373 by dpavlin, Sun Jun 20 15:49:09 2004 UTC revision 398 by dpavlin, Sat Jul 24 13:48:08 2004 UTC
# Line 188  sub open_isis { Line 188  sub open_isis {
188    
189                                          push @{$self->{'data'}->{$mfn}->{$k}}, $val;                                          push @{$self->{'data'}->{$mfn}->{$k}}, $val;
190                                  }                                  }
191                            } else {
192                                    push @{$self->{'data'}->{$mfn}->{'000'}}, $mfn;
193                          }                          }
194    
195                  }                  }
# Line 196  sub open_isis { Line 198  sub open_isis {
198                  my $rec = $self->{'data'}->{$mfn};                  my $rec = $self->{'data'}->{$mfn};
199                  $self->create_lookup($rec, @{$arg->{'lookup'}});                  $self->create_lookup($rec, @{$arg->{'lookup'}});
200    
201                    $self->progress_bar($mfn,$maxmfn);
202    
203          }          }
204    
205          $self->{'current_mfn'} = 1;          $self->{'current_mfn'} = 1;
206            $self->{'last_pcnt'} = 0;
207    
208          # store max mfn and return it.          # store max mfn and return it.
209          return $self->{'max_mfn'} = $maxmfn;          return $self->{'max_mfn'} = $maxmfn;
# Line 226  sub fetch_rec { Line 231  sub fetch_rec {
231                  return;                  return;
232          }          }
233    
234            $self->progress_bar($mfn,$self->{'max_mfn'});
235    
236          return $self->{'data'}->{$mfn};          return $self->{'data'}->{$mfn};
237  }  }
238    
239    =head2 progress_bar
240    
241    Draw progress bar on STDERR.
242    
243     $webpac->progress_bar($current, $max);
244    
245    =cut
246    
247    sub progress_bar {
248            my $self = shift;
249    
250            my ($curr,$max) = @_;
251    
252            my $log = $self->_get_logger();
253    
254            $log->logconfess("no current value!") if (! $curr);
255            $log->logconfess("no maximum value!") if (! $max);
256    
257            if ($curr > $max) {
258                    $max = $curr;
259                    $log->debug("overflow to $curr");
260            }
261    
262            $self->{'last_pcnt'} ||= 1;
263    
264            $self->{'last_pcnt'} = $curr if ($curr < $self->{'last_pcnt'});
265    
266            my $p = int($curr * 100 / $max);
267            if ($p != $self->{'last_pcnt'}) {
268                    printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$curr,$max,"=" x ($p/2).">", $p );
269                    $self->{'last_pcnt'} = $p;
270            }
271    }
272    
273  =head2 open_import_xml  =head2 open_import_xml
274    
275  Read file from C<import_xml/> directory and parse it.  Read file from C<import_xml/> directory and parse it.
# Line 252  sub open_import_xml { Line 293  sub open_import_xml {
293    
294          $self->{'tag'} = $type2tag{$type_base};          $self->{'tag'} = $type2tag{$type_base};
295    
296          $log->debug("using type '",$self->{'type'},"' tag <",$self->{'tag'},">") if ($self->{'debug'});          $log->info("using type '",$self->{'type'},"' tag <",$self->{'tag'},">");
297    
298          my $f = "./import_xml/".$self->{'type'}.".xml";          my $f = "./import_xml/".$self->{'type'}.".xml";
299          $log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f");          $log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f");
300    
301          $log->debug("reading '$f'") if ($self->{'debug'});          $log->info("reading '$f'");
302    
303            $self->{'import_xml_file'} = $f;
304    
305          $self->{'import_xml'} = XMLin($f,          $self->{'import_xml'} = XMLin($f,
306                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],
307          );          );
308    
309            $log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) });
310    
311  }  }
312    
313  =head2 create_lookup  =head2 create_lookup
# Line 329  sub get_data { Line 374  sub get_data {
374    
375          if ($$rec->{$f}) {          if ($$rec->{$f}) {
376                  return '' if (! $$rec->{$f}->[$i]);                  return '' if (! $$rec->{$f}->[$i]);
377                    no strict 'refs';
378                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {                  if ($sf && $$rec->{$f}->[$i]->{$sf}) {
379                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
380                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
# Line 369  Following example will read second value Line 415  Following example will read second value
415  This function B<does not> perform parsing of format to inteligenty skip  This function B<does not> perform parsing of format to inteligenty skip
416  delimiters before fields which aren't used.  delimiters before fields which aren't used.
417    
418    This method will automatically decode UTF-8 string to local code page
419    if needed.
420    
421  =cut  =cut
422    
423  sub fill_in {  sub fill_in {
# Line 384  sub fill_in { Line 433  sub fill_in {
433          # FIXME remove for speedup?          # FIXME remove for speedup?
434          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
435    
436            if (utf8::is_utf8($format)) {
437                    $format = $self->_x($format);
438            }
439    
440          my $found = 0;          my $found = 0;
441    
442          my $eval_code;          my $eval_code;
# Line 481  sub parse { Line 534  sub parse {
534    
535          $i = 0 if (! $i);          $i = 0 if (! $i);
536    
537          my $format = $self->{'utf2cp'}->convert($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'});          my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'});
538    
539          my @out;          my @out;
540    
# Line 602  It is used later to produce output. Line 655  It is used later to produce output.
655    
656   my @ds = $webpac->data_structure($rec);   my @ds = $webpac->data_structure($rec);
657    
658    This method will also set C<$webpac->{'currnet_filename'}> if there is
659    <filename> tag in C<import_xml> and C<$webpac->{'headline'}> if there is
660    <headline> tag.
661    
662  =cut  =cut
663    
664  sub data_structure {  sub data_structure {
# Line 612  sub data_structure { Line 669  sub data_structure {
669          my $rec = shift;          my $rec = shift;
670          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);          $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
671    
672            undef $self->{'currnet_filename'};
673            undef $self->{'headline'};
674    
675          my @sorted_tags;          my @sorted_tags;
676          if ($self->{tags_by_order}) {          if ($self->{tags_by_order}) {
677                  @sorted_tags = @{$self->{tags_by_order}};                  @sorted_tags = @{$self->{tags_by_order}};
# Line 643  sub data_structure { Line 703  sub data_structure {
703                          }                          }
704                          next if (! @v);                          next if (! @v);
705    
706                            # use format?
707                            if ($tag->{'format_name'}) {
708                                    @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;
709                            }
710    
711                            if ($field eq 'filename') {
712                                    $self->{'current_filename'} = join('',@v);
713                                    $log->debug("filename: ",$self->{'current_filename'});
714                            } elsif ($field eq 'headline') {
715                                    $self->{'headline'} .= join('',@v);
716                                    $log->debug("headline: ",$self->{'headline'});
717                                    next; # don't return headline in data_structure!
718                            }
719    
720                          # does tag have type?                          # does tag have type?
721                          if ($tag->{'type'}) {                          if ($tag->{'type'}) {
722                                  push @{$row->{$tag->{'type'}}}, @v;                                  push @{$row->{$tag->{'type'}}}, @v;
# Line 651  sub data_structure { Line 725  sub data_structure {
725                                  push @{$row->{'swish'}}, @v;                                  push @{$row->{'swish'}}, @v;
726                          }                          }
727    
728    
729                  }                  }
730    
731                  if ($row) {                  if ($row) {
732                          $row->{'tag'} = $field;                          $row->{'tag'} = $field;
733    
734                            # TODO: name_sigular, name_plural
735                            my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
736                            $row->{'name'} = $name ? $self->_x($name) : $field;
737    
738                          push @ds, $row;                          push @ds, $row;
739    
740                          $log->debug("row $field: ",sub { Dumper($row) });                          $log->debug("row $field: ",sub { Dumper($row) });
741                  }                  }
742    
# Line 694  sub output { Line 775  sub output {
775          return $out;          return $out;
776  }  }
777    
778    =head2 apply_format
779    
780    Apply format specified in tag with C<format_name="name"> and
781    C<format_delimiter=";;">.
782    
783     my $text = $webpac->apply_format($format_name,$format_delimiter,$data);
784    
785    Formats can contain C<lookup{...}> if you need them.
786    
787    =cut
788    
789    sub apply_format {
790            my $self = shift;
791    
792            my ($name,$delimiter,$data) = @_;
793    
794            my $log = $self->_get_logger();
795    
796            if (! $self->{'import_xml'}->{'format'}->{$name}) {
797                    $log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'});
798                    return $data;
799            }
800    
801            $log->warn("no delimiter for format $name") if (! $delimiter);
802    
803            my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'");
804    
805            my @data = split(/\Q$delimiter\E/, $data);
806    
807            my $out = sprintf($format, @data);
808            $log->debug("using format $name [$format] on $data to produce: $out");
809    
810            if ($out =~ m/$LOOKUP_REGEX/o) {
811                    return $self->lookup($out);
812            } else {
813                    return $out;
814            }
815    
816    }
817    
818    
819  #  #
820  #  #
821  #  #
# Line 746  sub _sort_by_order { Line 868  sub _sort_by_order {
868          return $va <=> $vb;          return $va <=> $vb;
869  }  }
870    
871    =head2 _get_logger
872    
873    Get C<Log::Log4perl> object with a twist: domains are defined for each
874    method
875    
876     my $log = $webpac->_get_logger();
877    
878    =cut
879    
880  sub _get_logger {  sub _get_logger {
881          my $self = shift;          my $self = shift;
882    
883          my @c = caller(1);          my $name = (caller(1))[3] || caller;
884          return get_logger($c[3]);          return get_logger($name);
885    }
886    
887    =head2 _x
888    
889    Convert string from UTF-8 to code page defined in C<import_xml>.
890    
891     my $text = $webpac->_x('utf8 text');
892    
893    =cut
894    
895    sub _x {
896            my $self = shift;
897            my $utf8 = shift || return;
898    
899            return $self->{'utf2cp'}->convert($utf8) ||
900                    $self->_get_logger()->logwarn("can't convert '$utf8'");
901  }  }
902    
903  #  #

Legend:
Removed from v.373  
changed lines
  Added in v.398

  ViewVC Help
Powered by ViewVC 1.1.26