13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
16 |
WebPAC::Parser - parse perl normalization configuration files and mungle it |
WebPAC::Parser - parse perl normalization configuration files (rules) and mungle it |
17 |
|
|
18 |
=head1 VERSION |
=head1 VERSION |
19 |
|
|
20 |
Version 0.05 |
Version 0.08 |
21 |
|
|
22 |
=cut |
=cut |
23 |
|
|
24 |
our $VERSION = '0.05'; |
our $VERSION = '0.08'; |
25 |
|
|
26 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
27 |
|
|
28 |
This module will parse L<WebPAC::Normalize/lookup> directives and generate source |
This module will parse L<WebPAC::Normalize/lookup> directives and generate source |
29 |
to produce lookups and normalization. |
to produce lookups and normalization. It will also parse other parts of |
30 |
|
source to produce some of DWIM (I<Do What I Mean>) magic |
31 |
|
(like producing MARC oputput using L<WebPAC::Output::MARC> if there are C<marc_*> |
32 |
|
rules in normalisation). |
33 |
|
|
34 |
It's written using L<PPI>, pure-perl parser for perl and heavily influenced by |
It's written using L<PPI>, pure-perl parser for perl and heavily influenced by |
35 |
reading about LISP. It might be a bit over-the board, but at least it removed |
reading about LISP. It might be a bit over-the board, but at least it removed |
49 |
my $parser = new WebPAC::Parser( |
my $parser = new WebPAC::Parser( |
50 |
config => new WebPAC::Config(), |
config => new WebPAC::Config(), |
51 |
base_path => '/optional/path/to/conf', |
base_path => '/optional/path/to/conf', |
52 |
|
only_database => $only |
53 |
); |
); |
54 |
|
|
55 |
=cut |
=cut |
152 |
return $self->{_lookup_create}->{ _q($database) }->{ _q($input) }; |
return $self->{_lookup_create}->{ _q($database) }->{ _q($input) }; |
153 |
} |
} |
154 |
|
|
155 |
|
=head2 normalize_rules |
156 |
|
|
157 |
|
my $source = $parser->normalize_rules($database, $input); |
158 |
|
|
159 |
|
=cut |
160 |
|
|
161 |
|
sub normalize_rules { |
162 |
|
my $self = shift; |
163 |
|
my ($database,$input) = @_; |
164 |
|
$input = _input_name($input); |
165 |
|
return unless ( |
166 |
|
defined( $self->{_normalize_source}->{ _q($database) } ) && |
167 |
|
defined( $self->{_normalize_source}->{ _q($database) }->{ _q($input) } ) |
168 |
|
); |
169 |
|
return $self->{_normalize_source}->{ _q($database) }->{ _q($input) }; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
=head2 have_rules |
174 |
|
|
175 |
|
my $do_marc = $parser->have_rules('marc', $database, $input); |
176 |
|
my $do_index = $parser->have_rules('search', $database); |
177 |
|
|
178 |
|
This function will return hash containing count of all found C<marc_*> or |
179 |
|
C<search> directives. Input name is optional. |
180 |
|
|
181 |
|
=cut |
182 |
|
|
183 |
|
sub have_rules { |
184 |
|
my $self = shift; |
185 |
|
|
186 |
|
my $log = $self->_get_logger(); |
187 |
|
my $type = shift @_ || $log->logconfess("need at least type"); |
188 |
|
my $database = shift @_ || $log->logconfess("database is required"); |
189 |
|
my $input = shift @_; |
190 |
|
|
191 |
|
$input = _input_name($input); |
192 |
|
|
193 |
|
|
194 |
|
return unless defined( $self->{_have_rules}->{ _q($database) } ); |
195 |
|
|
196 |
|
my $database_rules = $self->{_have_rules}->{ _q($database ) }; |
197 |
|
|
198 |
|
if (defined($input)) { |
199 |
|
|
200 |
|
return unless ( |
201 |
|
defined( $database_rules->{ _q($input) } ) && |
202 |
|
defined( $database_rules->{ _q($input) }->{ $type } ) |
203 |
|
); |
204 |
|
|
205 |
|
return $database_rules->{ _q($input) }->{ $type }; |
206 |
|
} |
207 |
|
|
208 |
|
my $usage; |
209 |
|
|
210 |
|
foreach my $i (keys %{ $database_rules }) { |
211 |
|
next unless defined( $database_rules->{$i}->{$type} ); |
212 |
|
|
213 |
|
foreach my $t (keys %{ $database_rules->{ $i }->{$type} }) { |
214 |
|
$usage->{ $t } += $database_rules->{ $i }->{ $t }; |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
return $usage; |
219 |
|
|
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
=head1 PRIVATE |
=head1 PRIVATE |
224 |
|
|
225 |
=head2 _read_sources |
=head2 _read_sources |
237 |
|
|
238 |
my $nr = 0; |
my $nr = 0; |
239 |
|
|
240 |
my @lookups; |
my @sources; |
241 |
|
|
242 |
|
my $lookup_src_cache; |
243 |
|
|
244 |
|
my $only_database = $self->{only_database}; |
245 |
|
my $only_input = $self->{only_input}; |
246 |
|
|
247 |
$self->{config}->iterate_inputs( sub { |
$self->{config}->iterate_inputs( sub { |
248 |
my ($input, $database) = @_; |
my ($input, $database) = @_; |
249 |
|
|
250 |
|
return if ( $only_database && $database !~ m/$only_database/i ); |
251 |
|
return if ( $only_input && $input->{name} !~ m/$only_input/i ); |
252 |
|
|
253 |
$log->debug("database: $database input = ", dump($input)); |
$log->debug("database: $database input = ", dump($input)); |
254 |
|
|
255 |
foreach my $normalize (@{ $input->{normalize} }) { |
foreach my $normalize (@{ $input->{normalize} }) { |
268 |
|
|
269 |
$self->{valid_inputs}->{$database}->{$input_name}++; |
$self->{valid_inputs}->{$database}->{$input_name}++; |
270 |
|
|
271 |
push @lookups, sub { |
push @sources, sub { |
272 |
$self->_parse_lookups( $database, $input_name, $full, $s ); |
#warn "### $database $input_name, $full ###\n"; |
273 |
|
$self->_parse_source( $database, $input_name, $full, $s ); |
274 |
}; |
}; |
275 |
|
|
276 |
$nr++; |
$nr++; |
279 |
|
|
280 |
$log->debug("found $nr source files"); |
$log->debug("found $nr source files"); |
281 |
|
|
282 |
# parse all lookups |
# parse all sources |
283 |
$_->() foreach (@lookups); |
$_->() foreach (@sources); |
284 |
|
|
285 |
return $nr; |
return $nr; |
286 |
} |
} |
287 |
|
|
288 |
=head2 _parse_lookups |
=head2 _parse_source |
289 |
|
|
290 |
$parser->_parse_lookups($database,$input,$path,$source); |
$parser->_parse_source($database,$input,$path,$source); |
291 |
|
|
292 |
Called for each normalize source (rules) in each input by L</read_sources> |
Called for each normalize source (rules) in each input by L</_read_sources> |
293 |
|
|
294 |
It will report invalid databases and inputs in error log after parsing. |
It will report invalid databases and inputs in error log after parsing. |
295 |
|
|
296 |
=cut |
=cut |
297 |
|
|
298 |
sub _parse_lookups { |
sub _parse_source { |
299 |
my $self = shift; |
my $self = shift; |
300 |
my ($database, $input, $path, $source) = @_; |
my ($database, $input, $path, $source) = @_; |
301 |
|
|
313 |
my $Document = PPI::Document->new( \$source ) || $log->logdie("can't parse source:\n", $self->{source}); |
my $Document = PPI::Document->new( \$source ) || $log->logdie("can't parse source:\n", $self->{source}); |
314 |
|
|
315 |
$Document->prune('PPI::Token::Whitespace'); |
$Document->prune('PPI::Token::Whitespace'); |
316 |
|
$Document->prune('PPI::Token::Comment'); |
317 |
#$Document->prune('PPI::Token::Operator'); |
#$Document->prune('PPI::Token::Operator'); |
318 |
|
|
319 |
# Find all the named subroutines |
# Find all the named subroutines |
378 |
|
|
379 |
$log->debug("key = $key"); |
$log->debug("key = $key"); |
380 |
|
|
|
my $create = ' |
|
|
$coderef = ' . $e[7] . $e[8] . '; |
|
|
foreach my $v ($coderef->()) { |
|
|
next unless (defined($v) && $v ne \'\'); |
|
|
push @{ $lookup->{\'' . $key . '\'}->{$v} }, $mfn; |
|
|
} |
|
|
'; |
|
|
|
|
|
$log->debug("create: $create"); |
|
|
|
|
381 |
return $self->_lookup_error("invalid database $e[3] in $path" ) unless $self->valid_database( $e[3] ); |
return $self->_lookup_error("invalid database $e[3] in $path" ) unless $self->valid_database( $e[3] ); |
382 |
return $self->_lookup_error("invalid input $e[5] of database $e[3] in $path", ) unless $self->valid_database_input( $e[3], $e[5] ); |
return $self->_lookup_error("invalid input $e[5] of database $e[3] in $path", ) unless $self->valid_database_input( $e[3], $e[5] ); |
383 |
|
|
384 |
|
my $create = qq{ |
385 |
|
save_into_lookup($e[3],$e[5],'$key', $e[7] $e[8] ); |
386 |
|
}; |
387 |
|
|
388 |
|
$log->debug("create: $create"); |
389 |
|
|
390 |
# save code to create this lookup |
# save code to create this lookup |
391 |
$self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) } .= $create; |
$self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) } .= $create; |
392 |
$self->{_lookup_create_key}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) }++; |
$self->{_lookup_create_key}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) }++; |
417 |
$log->debug("create: ", dump($self->{_lookup_create}) ); |
$log->debug("create: ", dump($self->{_lookup_create}) ); |
418 |
$log->debug("normalize: $normalize_source"); |
$log->debug("normalize: $normalize_source"); |
419 |
|
|
420 |
$self->{_normalize_source}->{$database}->{$input} = $normalize_source; |
$self->{_normalize_source}->{$database}->{$input} .= $normalize_source; |
421 |
|
|
422 |
if ($self->{debug}) { |
if ($self->{debug}) { |
423 |
my $Dumper = PPI::Dumper->new( $Document ); |
my $Dumper = PPI::Dumper->new( $Document ); |
426 |
|
|
427 |
$log->error("Parser errors:\n", join("\n",@{ $self->{_lookup_errors} }) ) if ($self->{_lookup_errors}); |
$log->error("Parser errors:\n", join("\n",@{ $self->{_lookup_errors} }) ) if ($self->{_lookup_errors}); |
428 |
|
|
429 |
|
$Document->find( sub { |
430 |
|
my ($Document,$Element) = @_; |
431 |
|
|
432 |
|
$Element->isa('PPI::Token::Word') or return ''; |
433 |
|
if ($Element->content =~ m/^(marc|search)/) { |
434 |
|
my $what = $1; |
435 |
|
$log->debug("found $what rules in $database/$input"); |
436 |
|
$self->{_have_rules}->{ $database }->{ $input }->{ $what }->{ $Element->content }++; |
437 |
|
} else { |
438 |
|
return ''; |
439 |
|
} |
440 |
|
}); |
441 |
|
|
442 |
return 1; |
return 1; |
443 |
} |
} |
444 |
|
|