1 |
package WebPAC::Common; |
package WebPAC::Common; |
2 |
|
use Exporter 'import'; |
3 |
|
@EXPORT = qw/ |
4 |
|
force_array |
5 |
|
dump |
6 |
|
/; |
7 |
|
|
8 |
use warnings; |
use warnings; |
9 |
use strict; |
use strict; |
10 |
|
|
11 |
use Log::Log4perl qw(get_logger :levels); |
use Log::Log4perl qw/get_logger :levels/; |
12 |
|
use Time::HiRes qw/time/; |
13 |
|
use Data::Dump qw/dump/; |
14 |
|
use File::Spec; |
15 |
|
use Cwd qw/abs_path/; |
16 |
|
|
17 |
|
use base qw/Class::Accessor/; |
18 |
|
__PACKAGE__->mk_accessors( qw/log_debug no_log debug/ ); |
19 |
|
|
20 |
=head1 NAME |
=head1 NAME |
21 |
|
|
23 |
|
|
24 |
=head1 VERSION |
=head1 VERSION |
25 |
|
|
26 |
Version 0.01 |
Version 0.05 |
27 |
|
|
28 |
=cut |
=cut |
29 |
|
|
30 |
our $VERSION = '0.01'; |
our $VERSION = '0.05'; |
31 |
|
|
32 |
=head1 SYNOPSYS |
=head1 SYNOPSYS |
33 |
|
|
34 |
This module defines common functions, and is used as base for other, more |
This module defines common functions, and is used as base for other, more |
35 |
specific modules. |
specific modules. |
36 |
|
|
37 |
my $webpac = new WebPAC::Common( |
my $o = WebPAC::Common->new({ |
38 |
filter => { |
log_debug => 1, |
39 |
'filter_name_1' => sub { |
no_log => 1, |
40 |
# filter code |
debug => 1, |
41 |
return length($_); |
}); |
|
}, ... |
|
|
}, |
|
|
} |
|
|
|
|
|
=head1 FUNCTIONS |
|
|
|
|
|
=head2 fill_in |
|
|
|
|
|
Workhourse of all: takes record from in-memory structure of database and |
|
|
strings with placeholders and returns string or array of with substituted |
|
|
values from record. |
|
|
|
|
|
my $text = $webpac->fill_in($rec,'v250^a'); |
|
|
|
|
|
Optional argument is ordinal number for repeatable fields. By default, |
|
|
it's assume to be first repeatable field (fields are perl array, so first |
|
|
element is 0). |
|
|
Following example will read second value from repeatable field. |
|
|
|
|
|
my $text = $webpac->fill_in($rec,'Title: v250^a',1); |
|
|
|
|
|
This function B<does not> perform parsing of format to inteligenty skip |
|
|
delimiters before fields which aren't used. |
|
|
|
|
|
This method will automatically decode UTF-8 string to local code page |
|
|
if needed. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub fill_in { |
|
|
my $self = shift; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
my $rec = shift || $log->logconfess("need data record"); |
|
|
my $format = shift || $log->logconfess("need format to parse"); |
|
|
# iteration (for repeatable fields) |
|
|
my $i = shift || 0; |
|
|
|
|
|
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
|
|
|
|
|
# FIXME remove for speedup? |
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
if (utf8::is_utf8($format)) { |
|
|
$format = $self->_x($format); |
|
|
} |
|
|
|
|
|
my $found = 0; |
|
|
|
|
|
my $eval_code; |
|
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
|
|
|
|
my $filter_name; |
|
|
# remove filter{...} from beginning |
|
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
|
|
|
|
|
# do actual replacement of placeholders |
|
|
# repeatable fields |
|
|
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
|
|
# non-repeatable fields |
|
|
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
|
|
|
|
|
if ($found) { |
|
|
$log->debug("format: $format"); |
|
|
if ($eval_code) { |
|
|
my $eval = $self->fill_in($rec,$eval_code,$i); |
|
|
return if (! $self->_eval($eval)); |
|
|
} |
|
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
|
|
$log->debug("filter '$filter_name' for $format"); |
|
|
$format = $self->{'filter'}->{$filter_name}->($format); |
|
|
return unless(defined($format)); |
|
|
$log->debug("filter result: $format"); |
|
|
} |
|
|
# do we have lookups? |
|
|
if ($self->{'lookup'}) { |
|
|
return $self->lookup($format); |
|
|
} else { |
|
|
return $format; |
|
|
} |
|
|
} else { |
|
|
return; |
|
|
} |
|
|
} |
|
42 |
|
|
43 |
|
Options: |
44 |
|
|
45 |
=head2 get_data |
=over 20 |
46 |
|
|
47 |
Returns value from record. |
=item log_debug |
48 |
|
|
49 |
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); |
Generate additional debugging log on C<STDERR> |
50 |
|
|
51 |
Arguments are: |
=item no_log |
|
record reference C<$rec>, |
|
|
field C<$f>, |
|
|
optional subfiled C<$sf>, |
|
|
index for repeatable values C<$i>. |
|
52 |
|
|
53 |
Optinal variable C<$found> will be incremeted if there |
Disable all logging (useful for tests) |
|
is field. |
|
54 |
|
|
55 |
Returns value or empty string. |
=item debug |
56 |
|
|
57 |
=cut |
Use debugging logger which dumps output only yo C<STDERR> |
58 |
|
|
59 |
sub get_data { |
=back |
|
my $self = shift; |
|
|
|
|
|
my ($rec,$f,$sf,$i,$found) = @_; |
|
60 |
|
|
|
if ($$rec->{$f}) { |
|
|
return '' if (! $$rec->{$f}->[$i]); |
|
|
no strict 'refs'; |
|
|
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
|
|
$$found++ if (defined($$found)); |
|
|
return $$rec->{$f}->[$i]->{$sf}; |
|
|
} elsif ($$rec->{$f}->[$i]) { |
|
|
$$found++ if (defined($$found)); |
|
|
# it still might have subfield, just |
|
|
# not specified, so we'll dump all |
|
|
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
|
|
my $out; |
|
|
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
|
|
$out .= $$rec->{$f}->[$i]->{$k}." "; |
|
|
} |
|
|
return $out; |
|
|
} else { |
|
|
return $$rec->{$f}->[$i]; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
return ''; |
|
|
} |
|
|
} |
|
61 |
|
|
62 |
|
=head1 FUNCTIONS |
63 |
|
|
64 |
=head2 progress_bar |
=head2 progress_bar |
65 |
|
|
76 |
|
|
77 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
78 |
|
|
79 |
|
$self->{last_pcnt_t} ||= time(); |
80 |
|
|
81 |
$log->logconfess("no current value!") if (! $curr); |
$log->logconfess("no current value!") if (! $curr); |
82 |
$log->logconfess("no maximum value!") if (! $max); |
$log->logconfess("no maximum value!") if (! $max); |
83 |
|
|
97 |
$self->{'start_t'} = time(); |
$self->{'start_t'} = time(); |
98 |
} |
} |
99 |
|
|
100 |
if ($p != $self->{'last_pcnt'}) { |
my $t = time(); |
101 |
|
|
102 |
|
if ($p != $self->{'last_pcnt'} || ( $t - $self->{last_pcnt_t} ) > 2 ) { |
103 |
|
|
|
my $t = time(); |
|
104 |
my $rate = ($curr / ($t - $self->{'start_t'} || 1)); |
my $rate = ($curr / ($t - $self->{'start_t'} || 1)); |
105 |
my $eta = ($max-$curr) / ($rate || 1); |
my $eta = ($max-$curr) / ($rate || 1); |
106 |
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); |
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); |
107 |
$self->{'last_pcnt'} = $p; |
$self->{'last_pcnt'} = $p; |
108 |
$self->{'last_curr'} = $curr; |
$self->{'last_curr'} = $curr; |
109 |
|
$self->{last_pcnt_t} = $t; |
110 |
} |
} |
111 |
print STDERR "\n" if ($p == 100); |
print STDERR "\n" if ($p == 100); |
112 |
} |
} |
134 |
return $out; |
return $out; |
135 |
} |
} |
136 |
|
|
137 |
|
=head2 fill_in |
138 |
|
|
139 |
|
Fill in variable names by values |
140 |
|
|
141 |
|
print $webpac->fill_in( 'foo = $foo bar = $bar', |
142 |
|
foo => 42, bar => 11, |
143 |
|
); |
144 |
|
|
145 |
|
=cut |
146 |
|
|
147 |
|
sub fill_in { |
148 |
|
my $self = shift; |
149 |
|
|
150 |
|
my $format = shift || die "no format?"; |
151 |
|
my $d = {@_}; |
152 |
|
|
153 |
|
foreach my $n ( keys %$d ) { |
154 |
|
$format =~ s/\$\Q$n\E/$d->{$n}/gs; |
155 |
|
} |
156 |
|
|
157 |
|
die "unknown variables in '$format' input data = ", dump( $d ) if ( $format =~ m/\$\w+/ ); |
158 |
|
|
159 |
|
return $format; |
160 |
|
} |
161 |
|
|
162 |
# |
# |
163 |
# |
# |
164 |
# |
# |
165 |
|
|
166 |
|
=head2 var_path |
167 |
|
|
168 |
|
my $path = $self->var_path('data_dir', 'data_file', ... ); |
169 |
|
|
170 |
|
=cut |
171 |
|
|
172 |
|
my $abs_path; |
173 |
|
|
174 |
|
sub var_path { |
175 |
|
my $self = shift; |
176 |
|
|
177 |
|
if ( ! $abs_path ) { |
178 |
|
# $abs_path = abs_path( $0 ); |
179 |
|
# $abs_path =~ s!/WebPAC/Common\.pm!!; |
180 |
|
$abs_path = '/data/webpac2'; |
181 |
|
} |
182 |
|
|
183 |
|
return File::Spec->catfile($abs_path, 'var', @_); |
184 |
|
} |
185 |
|
|
186 |
|
=head1 EXPORTED NETHODS |
187 |
|
|
188 |
|
=head2 force_array |
189 |
|
|
190 |
|
my @array = force_array( $ref, sub { |
191 |
|
warn "reference is undefined!"; |
192 |
|
}); |
193 |
|
|
194 |
|
=cut |
195 |
|
|
196 |
|
sub force_array { |
197 |
|
my ( $what, $error ) = @_; |
198 |
|
my @result; |
199 |
|
if ( ref( $what ) eq 'ARRAY' ) { |
200 |
|
@result = @{ $what }; |
201 |
|
} elsif ( defined $what ) { |
202 |
|
@result = ( $what ); |
203 |
|
} else { |
204 |
|
$error->() if ref($error) eq 'CODE'; |
205 |
|
} |
206 |
|
return @result; |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
=head1 INTERNAL METHODS |
=head1 INTERNAL METHODS |
211 |
|
|
212 |
Here is a quick list of internal methods, mostly useful to turn debugging |
Here is a quick list of internal methods, mostly useful to turn debugging |
248 |
will be created. If any mode which inherits from this one is called |
will be created. If any mode which inherits from this one is called |
249 |
with C<debug> flag, it will turn logging to debug level. |
with C<debug> flag, it will turn logging to debug level. |
250 |
|
|
251 |
|
This function will also read C<log_conf> value from current object and try |
252 |
|
to read that as configuration file if it exists, if it doesn't it will |
253 |
|
fallback to default C<conf/log.conf>. |
254 |
|
|
255 |
|
You can disable all logging by adding C<no_log> to constructor of WebPAC |
256 |
|
object. Object in C<Test::Exception> class will disable logging |
257 |
|
automatically. |
258 |
|
|
259 |
=cut |
=cut |
260 |
|
|
261 |
sub _init_logger { |
sub _init_logger { |
262 |
my $self = shift; |
my $self = shift; |
263 |
my $file = shift; |
my $file = shift; |
264 |
if ($file) { |
$file ||= $self->{'log_conf'}; |
265 |
Log::Log4perl->init($file); |
$file = 'conf/log.conf'; |
266 |
} else { |
my $name = (caller(2))[3] || caller; |
267 |
my $conf = q( ); |
|
268 |
if ($self->{'debug'}) { |
my $conf = q( ); |
269 |
$conf = << '_log4perl_'; |
if ($self->no_log) { |
270 |
|
warn "# $name disabled logging\n" if $self->log_debug; |
271 |
|
$Log::Log4perl::Config::CONFIG_INTEGRITY_CHECK = 0; |
272 |
|
} elsif ($self->debug) { |
273 |
|
$conf = << '_log4perl_'; |
274 |
|
|
275 |
log4perl.rootLogger=INFO, SCREEN |
log4perl.rootLogger=INFO, SCREEN |
276 |
|
|
281 |
log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n |
log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n |
282 |
|
|
283 |
_log4perl_ |
_log4perl_ |
284 |
} |
warn "# $name is using debug logger\n" if $self->log_debug; |
285 |
Log::Log4perl->init( \$conf ); |
} elsif ($name =~ m/Test::Exception/o) { |
286 |
|
warn "# disabled logging for Text::Exception\n" if $self->log_debug; |
287 |
|
} elsif (-e $file) { |
288 |
|
warn "# $name is using $file logger\n" if $self->log_debug; |
289 |
|
Log::Log4perl->init($file); |
290 |
|
return 1; |
291 |
|
} else { |
292 |
|
warn "# $name is using null logger\n" if $self->log_debug; |
293 |
} |
} |
294 |
|
Log::Log4perl->init( \$conf ); |
295 |
|
|
296 |
|
return 1; |
297 |
} |
} |
298 |
|
|
299 |
|
|
306 |
|
|
307 |
=cut |
=cut |
308 |
|
|
309 |
|
my $_logger_seen; |
310 |
|
|
311 |
sub _get_logger { |
sub _get_logger { |
312 |
my $self = shift; |
my $self = shift; |
313 |
|
|
|
$self->{'_logger_ok'} ||= $self->_init_logger; |
|
|
|
|
314 |
my $name = (caller(1))[3] || caller; |
my $name = (caller(1))[3] || caller; |
315 |
return get_logger($name); |
|
316 |
|
# make name full |
317 |
|
my $f = ''; |
318 |
|
if ( $self->log_debug ) { |
319 |
|
foreach ( 0 .. 5 ) { |
320 |
|
my $s = (caller($_))[3]; |
321 |
|
$f .= "#### $_ >> $s\n" if ($s); |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
$self->{'_logger_'} ||= $self->_init_logger; |
326 |
|
|
327 |
|
my $log = get_logger( $name ); |
328 |
|
warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name})); |
329 |
|
$_logger_seen->{$name}++; |
330 |
|
return $log; |
331 |
} |
} |
332 |
|
|
333 |
|
|
334 |
|
=head2 _log |
335 |
|
|
336 |
|
Quick cludge to make logging object available to scripts which |
337 |
|
use webpac line this: |
338 |
|
|
339 |
|
my $log = _new WebPAC::Common()->_get_logger(); |
340 |
|
|
341 |
|
=cut |
342 |
|
|
343 |
|
sub _new { |
344 |
|
my $class = shift; |
345 |
|
my $self = {@_}; |
346 |
|
bless($self, $class); |
347 |
|
|
348 |
|
$self ? return $self : return undef; |
349 |
|
} |
350 |
|
|
351 |
=head1 LOGGING |
=head1 LOGGING |
352 |
|
|
353 |
Logging in WebPAC is performed by L<Log::Log4perl> with config file |
Logging in WebPAC is performed by L<Log::Log4perl> with config file |
362 |
also use method names, and not only classes (which are just few) |
also use method names, and not only classes (which are just few) |
363 |
to filter logging. |
to filter logging. |
364 |
|
|
365 |
|
=cut |
366 |
|
|
367 |
|
1; |