line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chart::Kaleido; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Base class for Chart::Kaleido |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
579
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
7
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.010'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
12
|
1
|
|
|
1
|
|
328
|
use Config; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
55
|
|
13
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
9
|
|
14
|
1
|
|
|
1
|
|
142
|
use Types::Standard qw(Int Str); |
|
1
|
|
|
|
|
26
|
|
|
1
|
|
|
|
|
13
|
|
15
|
1
|
|
|
1
|
|
1329
|
use File::Which qw(which); |
|
1
|
|
|
|
|
1055
|
|
|
1
|
|
|
|
|
62
|
|
16
|
1
|
|
|
1
|
|
958
|
use IPC::Run qw(); |
|
1
|
|
|
|
|
35187
|
|
|
1
|
|
|
|
|
31
|
|
17
|
1
|
|
|
1
|
|
11
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
88
|
use constant KALEIDO => 'kaleido'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
448
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has timeout => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => Int, |
25
|
|
|
|
|
|
|
default => 30, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has base_args => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
init_arg => 0, |
31
|
|
|
|
|
|
|
default => sub { [] }, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has _default_chromium_args => ( |
35
|
|
|
|
|
|
|
is => 'ro', |
36
|
|
|
|
|
|
|
default => sub { |
37
|
|
|
|
|
|
|
[ |
38
|
|
|
|
|
|
|
qw( |
39
|
|
|
|
|
|
|
--disable-gpu |
40
|
|
|
|
|
|
|
--allow-file-access-from-files |
41
|
|
|
|
|
|
|
--disable-breakpad |
42
|
|
|
|
|
|
|
--disable-dev-shm-usage |
43
|
|
|
|
|
|
|
) |
44
|
|
|
|
|
|
|
]; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has disable_gpu => ( |
49
|
|
|
|
|
|
|
is => 'ro', |
50
|
|
|
|
|
|
|
default => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has _stall_timeout => ( |
54
|
|
|
|
|
|
|
is => 'lazy', |
55
|
|
|
|
|
|
|
builder => |
56
|
1
|
|
|
1
|
|
26
|
sub { IPC::Run::timeout( $_[0]->timeout, name => 'stall timeout' ) }, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has _h => ( is => 'rw' ); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
has _ios => ( |
62
|
|
|
|
|
|
|
is => 'ro', |
63
|
|
|
|
|
|
|
default => sub { |
64
|
|
|
|
|
|
|
return { map { $_ => '' } qw(in out err) }; |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# class attributes |
69
|
0
|
|
|
0
|
0
|
0
|
sub all_formats { [] } |
70
|
0
|
|
|
0
|
0
|
0
|
sub scope_name { "" } |
71
|
0
|
|
|
0
|
0
|
0
|
sub scope_flags { [] } |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub DEMOLISH { |
74
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
75
|
0
|
|
|
|
|
0
|
$self->shutdown_kaleido; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _reset { |
79
|
1
|
|
|
1
|
|
4
|
my ($self) = @_; |
80
|
1
|
|
|
|
|
8
|
$self->_ios->{in} = ''; |
81
|
1
|
|
|
|
|
4
|
$self->_ios->{out} = ''; |
82
|
1
|
|
|
|
|
4
|
$self->_ios->{err} = ''; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub kaleido_args { |
86
|
4
|
|
|
4
|
0
|
1493
|
my ($self) = @_; |
87
|
|
|
|
|
|
|
|
88
|
4
|
|
|
|
|
39
|
my @args = @{ $self->base_args }; |
|
4
|
|
|
|
|
31
|
|
89
|
4
|
50
|
|
|
|
31
|
unless ( $self->disable_gpu ) { |
90
|
0
|
|
|
|
|
0
|
@args = grep { $_ ne '--disable-gpu' } @args; |
|
0
|
|
|
|
|
0
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
1
|
|
8
|
no strict 'refs'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1160
|
|
94
|
|
|
|
|
|
|
push @args, map { |
95
|
16
|
|
|
|
|
47
|
my $val = $self->$_; |
96
|
16
|
50
|
|
|
|
33
|
if ( defined $val ) { |
97
|
0
|
|
|
|
|
0
|
my $flag = $_; |
98
|
0
|
|
|
|
|
0
|
$flag =~ s/_/-/g; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# too bad Perl does not have a core boolean type.. |
101
|
0
|
0
|
0
|
|
|
0
|
if ( ref($val) =~ /^(JSON::.*::Boolean|boolean)$/ and $val ) { |
102
|
0
|
|
|
|
|
0
|
"--$flag"; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
0
|
|
|
|
|
0
|
"--$flag=$val"; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
16
|
|
|
|
|
30
|
(); |
110
|
|
|
|
|
|
|
} |
111
|
4
|
|
|
|
|
8
|
} @{ $self->scope_flags }; |
|
4
|
|
|
|
|
24
|
|
112
|
|
|
|
|
|
|
|
113
|
4
|
|
|
|
|
63
|
return \@args; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub ensure_kaleido { |
117
|
2
|
|
|
2
|
0
|
35
|
my ( $self, $override_args ) = @_; |
118
|
2
|
|
33
|
|
|
37
|
$override_args //= $self->kaleido_args; |
119
|
|
|
|
|
|
|
|
120
|
2
|
100
|
66
|
|
|
33
|
unless ( $self->_h and $self->_h->pumpable ) { |
121
|
1
|
|
|
|
|
8
|
$self->_reset; |
122
|
|
|
|
|
|
|
my $h = IPC::Run::start( |
123
|
1
|
|
|
|
|
3
|
[ KALEIDO, @{ $self->kaleido_args } ], |
124
|
|
|
|
|
|
|
\$self->_ios->{in}, |
125
|
|
|
|
|
|
|
\$self->_ios->{out}, |
126
|
|
|
|
|
|
|
\$self->_ios->{err}, |
127
|
1
|
|
|
|
|
2
|
$self->_stall_timeout, |
128
|
|
|
|
|
|
|
); |
129
|
1
|
|
|
|
|
10873
|
$self->_h($h); |
130
|
|
|
|
|
|
|
|
131
|
1
|
|
|
|
|
56
|
$self->_stall_timeout->start; |
132
|
1
|
|
|
|
|
443
|
my $resp = $self->_get_kaleido_out; |
133
|
1
|
50
|
33
|
|
|
34
|
if ( exists $resp->{code} and $resp->{code} == 0 ) { |
134
|
1
|
|
|
|
|
31
|
return $resp->{version}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
0
|
|
|
|
|
0
|
die $resp->{message}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub shutdown_kaleido { |
143
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
144
|
|
|
|
|
|
|
|
145
|
0
|
0
|
|
|
|
0
|
if ( $self->_h ) { |
146
|
0
|
|
|
|
|
0
|
eval { $self->finish; }; |
|
0
|
|
|
|
|
0
|
|
147
|
0
|
0
|
|
|
|
0
|
if ($@) { |
148
|
0
|
|
|
|
|
0
|
$self->_h->kill_kill; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub do_transform { |
154
|
2
|
|
|
2
|
0
|
8
|
my ( $self, $data ) = @_; |
155
|
|
|
|
|
|
|
|
156
|
2
|
|
|
|
|
13
|
$self->ensure_kaleido; |
157
|
|
|
|
|
|
|
|
158
|
2
|
|
|
|
|
144
|
my $json = JSON->new->allow_blessed(1)->convert_blessed(1); |
159
|
2
|
|
|
|
|
97
|
$self->_ios->{in} .= $json->encode($data) . "\n"; |
160
|
2
|
|
|
|
|
77
|
$self->_stall_timeout->start; |
161
|
2
|
|
|
|
|
477
|
my $resp = $self->_get_kaleido_out; |
162
|
2
|
|
|
|
|
56
|
return $resp; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub version { |
166
|
0
|
|
|
0
|
0
|
0
|
my ( $class, $force_check ) = @_; |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
0
|
if ( $class->_check_alien($force_check) ) { |
169
|
0
|
|
|
|
|
0
|
return Alien::Plotly::Kaleido->version; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
else { |
172
|
0
|
|
|
|
|
0
|
state $version; |
173
|
0
|
0
|
0
|
|
|
0
|
if ( not $version or $force_check ) { |
174
|
0
|
|
|
|
|
0
|
$version = $class->_detect_kaleido_version; |
175
|
|
|
|
|
|
|
} |
176
|
0
|
|
|
|
|
0
|
return $version; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub _get_kaleido_out { |
181
|
3
|
|
|
3
|
|
23
|
my ($self) = @_; |
182
|
|
|
|
|
|
|
|
183
|
3
|
|
|
|
|
17
|
while (1) { |
184
|
14
|
|
|
|
|
118
|
$self->_h->pump; |
185
|
14
|
|
|
|
|
1736673
|
my $out = $self->_ios->{out}; |
186
|
14
|
|
|
|
|
866
|
my @lines = split( /\n/, $out ); |
187
|
14
|
100
|
|
|
|
72
|
next unless @lines; |
188
|
|
|
|
|
|
|
|
189
|
10
|
|
|
|
|
37
|
for my $line (@lines) { |
190
|
10
|
|
|
|
|
24
|
my $data; |
191
|
10
|
|
|
|
|
19
|
eval { $data = decode_json($line); }; |
|
10
|
|
|
|
|
1340
|
|
192
|
10
|
100
|
|
|
|
74
|
next if $@; |
193
|
3
|
|
|
|
|
133
|
$self->_stall_timeout->reset; |
194
|
3
|
|
|
|
|
391
|
$self->_ios->{out} = ''; # clear out buffer |
195
|
3
|
|
|
|
|
18
|
return $data; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub _check_alien { |
201
|
1
|
|
|
1
|
|
3
|
my ( $class, $force_check ) = @_; |
202
|
|
|
|
|
|
|
|
203
|
1
|
|
|
|
|
2
|
state $has_alien; |
204
|
|
|
|
|
|
|
|
205
|
1
|
50
|
33
|
|
|
15
|
if ( !defined $has_alien or $force_check ) { |
206
|
1
|
|
|
|
|
3
|
$has_alien = 0; |
207
|
1
|
|
|
|
|
3
|
eval { require Alien::Plotly::Kaleido; }; |
|
1
|
|
|
|
|
842
|
|
208
|
1
|
50
|
33
|
|
|
26244
|
if ( !$@ and Alien::Plotly::Kaleido->install_type eq 'share' ) { |
209
|
|
|
|
|
|
|
$ENV{PATH} = join( |
210
|
|
|
|
|
|
|
$Config{path_sep}, |
211
|
|
|
|
|
|
|
Alien::Plotly::Kaleido->bin_dir, |
212
|
1
|
|
50
|
|
|
20034
|
$ENV{PATH} // '' |
213
|
|
|
|
|
|
|
); |
214
|
1
|
|
|
|
|
754
|
$has_alien = 1; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
1
|
|
|
|
|
5
|
return $has_alien; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub _kaleido_available { |
221
|
1
|
|
|
1
|
|
3
|
my ( $class, $force_check ) = @_; |
222
|
|
|
|
|
|
|
|
223
|
1
|
|
|
|
|
2
|
state $available; |
224
|
1
|
50
|
33
|
|
|
8
|
if ( !defined $available or $force_check ) { |
225
|
1
|
|
|
|
|
2
|
$available = 0; |
226
|
1
|
0
|
33
|
|
|
5
|
if ( not $class->_check_alien($force_check) |
227
|
|
|
|
|
|
|
and ( not which(KALEIDO) ) ) |
228
|
|
|
|
|
|
|
{ |
229
|
0
|
|
|
|
|
0
|
die "Kaleido tool (its 'kaleido' command) must be installed and " |
230
|
|
|
|
|
|
|
. "in PATH in order to export images. " |
231
|
|
|
|
|
|
|
. "Either install Alien::Plotly::Kaleido from CPAN, or install " |
232
|
|
|
|
|
|
|
. "it manually (see https://github.com/plotly/Kaleido/releases)"; |
233
|
|
|
|
|
|
|
} |
234
|
1
|
|
|
|
|
4
|
$available = 1; |
235
|
|
|
|
|
|
|
} |
236
|
1
|
|
|
|
|
3
|
return $available; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub _detect_kaleido_version { |
240
|
0
|
|
|
0
|
|
|
my ($class) = @_; |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
my $kaleido = which('kaleido'); |
243
|
0
|
0
|
|
|
|
|
if ($kaleido) { |
244
|
0
|
|
|
|
|
|
my $kaleido = $class->new; |
245
|
0
|
|
|
|
|
|
my $args = [ 'plotly', '--disable-gpu' ]; |
246
|
0
|
|
|
|
|
|
my $version = $kaleido->ensure_kaleido($args); |
247
|
0
|
|
|
|
|
|
$kaleido->shutdown_kaleido; |
248
|
0
|
|
|
|
|
|
return $version; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
die "Failed to detect kaleido version"; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
__PACKAGE__->_kaleido_available; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
1; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
__END__ |