line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PDF::OCR::Thorough::Cached; |
2
|
2
|
|
|
2
|
|
40367
|
use base 'PDF::OCR::Thorough'; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
1558
|
|
3
|
|
|
|
|
|
|
use strict; |
4
|
|
|
|
|
|
|
#use File::Slurp; |
5
|
|
|
|
|
|
|
use vars qw($ABS_CACHE_DIR $CACHE_BY_SUM); |
6
|
|
|
|
|
|
|
$ABS_CACHE_DIR = '/tmp/PDF-OCR-Thorough-Cached'; |
7
|
|
|
|
|
|
|
$CACHE_BY_SUM = 0; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub _slurp { |
10
|
|
|
|
|
|
|
my $abs = shift; |
11
|
|
|
|
|
|
|
open(FILE,'<', $abs) or die($!); |
12
|
|
|
|
|
|
|
local $/; |
13
|
|
|
|
|
|
|
my $txt = ; |
14
|
|
|
|
|
|
|
close FILE; |
15
|
|
|
|
|
|
|
return $txt; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _cli_md5sum { |
20
|
|
|
|
|
|
|
my $abs = shift; |
21
|
|
|
|
|
|
|
my $sum = `md5sum '$abs'`; |
22
|
|
|
|
|
|
|
$sum=~s/\s.+$|\s+$//g; |
23
|
|
|
|
|
|
|
$sum=~/^\w{32}$/ or die(); |
24
|
|
|
|
|
|
|
return $sum; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _sum2file { |
29
|
|
|
|
|
|
|
my $sum = shift; |
30
|
|
|
|
|
|
|
$sum=~/^(\w{2})/ or die; |
31
|
|
|
|
|
|
|
return "$ABS_CACHE_DIR/$1/$sum"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# methods ... |
37
|
|
|
|
|
|
|
sub get_text { |
38
|
|
|
|
|
|
|
my $self = shift; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
unless( defined $self->{__text} ){ |
41
|
|
|
|
|
|
|
if ( $self->is_cached ){ |
42
|
|
|
|
|
|
|
#my $text = File::Slurp::slurp($self->abs_cached); |
43
|
|
|
|
|
|
|
my $text = _slurp($self->abs_cached); |
44
|
|
|
|
|
|
|
$self->{__text} = $text; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
|
|
|
|
|
|
my $text = $self->SUPER::get_text; |
49
|
|
|
|
|
|
|
$self->_assure_abs_cached_loc; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
open(FILE,">".$self->abs_cached) |
52
|
|
|
|
|
|
|
or die( sprintf "cannot write to %s, $!", $self->abs_cached ); |
53
|
|
|
|
|
|
|
print FILE $text; |
54
|
|
|
|
|
|
|
close FILE; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$self->{is_cached} = 1; |
57
|
|
|
|
|
|
|
$self->{__text} = $text; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return $self->{__text}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _md5sum { |
66
|
|
|
|
|
|
|
my $self = shift; |
67
|
|
|
|
|
|
|
$self->{md5sum} ||= _cli_md5sum($self->abs_pdf); |
68
|
|
|
|
|
|
|
return $self->{md5sum}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub abs_cached { |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $cached; |
77
|
|
|
|
|
|
|
if( $CACHE_BY_SUM ){ |
78
|
|
|
|
|
|
|
my $sum = $self->_md5sum; |
79
|
|
|
|
|
|
|
$cached = _sum2file($sum); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
else { |
83
|
|
|
|
|
|
|
$cached = $ABS_CACHE_DIR.'/'.$self->abs_pdf.'.txt'; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $cached; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _assure_abs_cached_loc { |
91
|
|
|
|
|
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $loc = $self->abs_cached; |
94
|
|
|
|
|
|
|
$loc=~s/\/[^\/]+$//; |
95
|
|
|
|
|
|
|
-d $loc or File::Path::mkpath($loc) or die("cant make $loc, $!"); |
96
|
|
|
|
|
|
|
return 1; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
*set_abs_cache = \&abs_cache; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub abs_cache { |
104
|
|
|
|
|
|
|
my $self = shift; |
105
|
|
|
|
|
|
|
carp("abs_cache() is deprecated, use \$".__PACKAGE__."::ABS_CACHE_DIR instead"); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $abs_cache = shift; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
if (defined $abs_cache){ |
110
|
|
|
|
|
|
|
$ABS_CACHE_DIR = $abs_cache; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return $ABS_CACHE_DIR; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub is_cached { |
117
|
|
|
|
|
|
|
my $self = shift; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
unless( defined $self->{is_cached} ){ |
120
|
|
|
|
|
|
|
no warnings; |
121
|
|
|
|
|
|
|
$self->{is_cached} =( -f $self->abs_cached ? 1 : 0 ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
return $self->{is_cached}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |