9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
use File::Slurp; |
use File::Slurp; |
11 |
|
|
12 |
use base qw/WebPAC::Common WebPAC::Normalize/; |
use base qw/WebPAC::Common/; |
13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
17 |
|
|
18 |
=head1 VERSION |
=head1 VERSION |
19 |
|
|
20 |
Version 0.04 |
Version 0.05 |
21 |
|
|
22 |
=cut |
=cut |
23 |
|
|
24 |
our $VERSION = '0.04'; |
our $VERSION = '0.05'; |
25 |
|
|
26 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
27 |
|
|
61 |
|
|
62 |
$log->logdie("can't iterate_inputs over this config object") unless ($self->{config}->can('iterate_inputs')); |
$log->logdie("can't iterate_inputs over this config object") unless ($self->{config}->can('iterate_inputs')); |
63 |
|
|
64 |
$self->read_sources; |
$self->_read_sources; |
65 |
|
|
66 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
67 |
} |
} |
68 |
|
|
69 |
=head2 read_sources |
=head2 valid_database |
70 |
|
|
71 |
|
my $ok = $parse->valid_database('key'); |
72 |
|
|
73 |
|
=cut |
74 |
|
|
75 |
|
sub valid_database { |
76 |
|
my $self = shift; |
77 |
|
|
78 |
|
my $database = shift || return; |
79 |
|
|
80 |
my $source_files = $parser->read_sources; |
return defined($self->{valid_inputs}->{ _q($database) }); |
81 |
|
} |
82 |
|
|
83 |
|
=head2 valid_database_input |
84 |
|
|
85 |
|
my $ok = $parse->valid_database('database_key','input_name'); |
86 |
|
|
87 |
|
=cut |
88 |
|
|
89 |
|
sub valid_database_input { |
90 |
|
my $self = shift; |
91 |
|
my ($database,$input) = @_; |
92 |
|
$input = _input_name($input); |
93 |
|
return defined($self->{valid_inputs}->{ _q($database) }->{ _q($input) }); |
94 |
|
} |
95 |
|
|
96 |
|
=head2 depends |
97 |
|
|
98 |
|
Return all databases and inputs on which specified one depends |
99 |
|
|
100 |
|
$depends_on = $parser->depends('database','input'); |
101 |
|
|
102 |
|
=cut |
103 |
|
|
104 |
|
sub depends { |
105 |
|
my $self = shift; |
106 |
|
my ($database,$input) = @_; |
107 |
|
$input = _input_name($input); |
108 |
|
$self->_get_logger->debug("depends($database,$input)"); |
109 |
|
return unless ( |
110 |
|
defined( $self->{depends}->{ _q($database) } ) && |
111 |
|
defined( $self->{depends}->{ _q($database) }->{ _q($input) } ) |
112 |
|
); |
113 |
|
return $self->{depends}->{ _q($database) }->{ _q($input) }; |
114 |
|
} |
115 |
|
|
116 |
|
=head2 have_lookup_create |
117 |
|
|
118 |
|
my @keys = $parser->have_lookup_create($database, $input); |
119 |
|
|
120 |
|
=cut |
121 |
|
|
122 |
|
sub have_lookup_create { |
123 |
|
my $self = shift; |
124 |
|
my ($database,$input) = @_; |
125 |
|
$input = _input_name($input); |
126 |
|
return unless ( |
127 |
|
defined( $self->{_lookup_create_key}->{ _q($database) } ) && |
128 |
|
defined( $self->{_lookup_create_key}->{ _q($database) }->{ _q($input) } ) |
129 |
|
); |
130 |
|
return keys %{ $self->{_lookup_create_key}->{ _q($database) }->{ _q($input) } }; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
=head2 lookup_create_rules |
135 |
|
|
136 |
|
my $source = $parser->lookup_create_rules($database, $input); |
137 |
|
|
138 |
|
=cut |
139 |
|
|
140 |
|
sub lookup_create_rules { |
141 |
|
my $self = shift; |
142 |
|
my ($database,$input) = @_; |
143 |
|
$input = _input_name($input); |
144 |
|
return unless ( |
145 |
|
defined( $self->{_lookup_create}->{ _q($database) } ) && |
146 |
|
defined( $self->{_lookup_create}->{ _q($database) }->{ _q($input) } ) |
147 |
|
); |
148 |
|
return $self->{_lookup_create}->{ _q($database) }->{ _q($input) }; |
149 |
|
} |
150 |
|
|
151 |
|
=head1 PRIVATE |
152 |
|
|
153 |
|
=head2 _read_sources |
154 |
|
|
155 |
|
my $source_files = $parser->_read_sources; |
156 |
|
|
157 |
Called by L</new>. |
Called by L</new>. |
158 |
|
|
159 |
=cut |
=cut |
160 |
|
|
161 |
sub read_sources { |
sub _read_sources { |
162 |
my $self = shift; |
my $self = shift; |
163 |
|
|
164 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
189 |
$self->{valid_inputs}->{$database}->{$input_name}++; |
$self->{valid_inputs}->{$database}->{$input_name}++; |
190 |
|
|
191 |
push @lookups, sub { |
push @lookups, sub { |
192 |
$self->parse_lookups( $database, $input_name, $full, $s ); |
$self->_parse_lookups( $database, $input_name, $full, $s ); |
193 |
}; |
}; |
194 |
|
|
195 |
$nr++; |
$nr++; |
204 |
return $nr; |
return $nr; |
205 |
} |
} |
206 |
|
|
207 |
=head2 parse_lookups |
=head2 _parse_lookups |
208 |
|
|
209 |
$parser->parse_lookups($database,$input,$path,$source); |
$parser->_parse_lookups($database,$input,$path,$source); |
210 |
|
|
211 |
Called for each normalize source in each input by L</new> |
Called for each normalize source (rules) in each input by L</_read_sources> |
212 |
|
|
213 |
It will report invalid databases and inputs in error log after parsing. |
It will report invalid databases and inputs in error log after parsing. |
214 |
|
|
215 |
=cut |
=cut |
216 |
|
|
217 |
sub parse_lookups { |
sub _parse_lookups { |
218 |
my $self = shift; |
my $self = shift; |
219 |
my ($database, $input, $path, $source) = @_; |
my ($database, $input, $path, $source) = @_; |
220 |
|
|
296 |
|
|
297 |
$log->debug("key = $key"); |
$log->debug("key = $key"); |
298 |
|
|
299 |
my $create = ' |
my $create = "save_into_lookup('$key', $e[7] $e[8] );\n"; |
|
$coderef = ' . $e[7] . $e[8] . '; |
|
|
foreach my $v ($coderef->()) { |
|
|
next unless (defined($v) && $v ne \'\'); |
|
|
push @{ $lookup->{\'' . $key . '\'}->{$v} }, $mfn; |
|
|
} |
|
|
'; |
|
300 |
|
|
301 |
$log->debug("create: $create"); |
$log->debug("create: $create"); |
302 |
|
|
304 |
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] ); |
305 |
|
|
306 |
# save code to create this lookup |
# save code to create this lookup |
307 |
$self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) } .= $create; |
$self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) } .= $create; |
308 |
|
$self->{_lookup_create_key}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) }++; |
309 |
|
|
310 |
|
|
311 |
if (defined( $self->{depends}->{ $database }->{ $input }->{ _q($e[3]) }->{ _q($e[5]) }->{ $key } )) { |
if (defined( $self->{depends}->{ $database }->{ $input }->{ _q($e[3]) }->{ _q($e[5]) }->{ $key } )) { |
346 |
} |
} |
347 |
|
|
348 |
|
|
|
=head2 lookup_create_rules |
|
|
|
|
|
my $source = $parser->lookup_create_rules($database, $input); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub lookup_create_rules { |
|
|
my $self = shift; |
|
|
my ($database,$input) = @_; |
|
|
return $self->{_lookup_create}->{ _q($database) }->{ _q($input) }; |
|
|
} |
|
|
|
|
|
=head2 valid_database |
|
|
|
|
|
my $ok = $parse->valid_database('key'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub valid_database { |
|
|
my $self = shift; |
|
|
|
|
|
my $database = shift || return; |
|
|
|
|
|
return defined($self->{valid_inputs}->{ _q($database) }); |
|
|
} |
|
|
|
|
|
=head2 valid_database_input |
|
|
|
|
|
my $ok = $parse->valid_database('database_key','input_name'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub valid_database_input { |
|
|
my $self = shift; |
|
|
|
|
|
my ($database,$input) = @_; |
|
|
return defined($self->{valid_inputs}->{ _q($database) }->{ _q($input) }); |
|
|
} |
|
|
|
|
|
=head2 depends |
|
|
|
|
|
Return all databases and inputs on which specified one depends |
|
|
|
|
|
$depends_on = $parser->depends('database','input'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub depends { |
|
|
my $self = shift; |
|
|
my ($database,$input) = @_; |
|
|
$self->_get_logger->debug("depends($database,$input)"); |
|
|
return unless ( |
|
|
defined( $self->{depends}->{ _q($database) } ) && |
|
|
defined( $self->{depends}->{ _q($database) }->{ _q($input) } ) |
|
|
); |
|
|
return $self->{depends}->{ _q($database) }->{ _q($input) }; |
|
|
} |
|
|
|
|
|
=head1 PRIVATE |
|
|
|
|
349 |
=head2 _q |
=head2 _q |
350 |
|
|
351 |
Strip single or double quotes around value |
Strip single or double quotes around value |