| 1 | package Chart::Plotly::Image::Orca; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Export static images of Plotly charts using orca | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Which; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::ShareDir qw(dist_file); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | our $VERSION = '0.042'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my $ORCA_COMMAND = 'orca'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # have this in a sub to avoid breaking auto-generated tests like pod-coverage. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _plotlyjs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plotlyjs: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | state $plotlyjs = dist_file( 'Chart-Plotly', 'plotly.js/plotly.min.js' ); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | return $plotlyjs; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub _check_alien { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_alien: called CC=5 SCAR=18.5
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my ($force_check) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | state $has_alien; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | if ( !defined $has_alien or $force_check ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $has_alien or $force_check
MC/DC0%0/2
not defined $has_alien or $force_check
not defined $has_alien
$force_check
Truth table33%1/3
not(A) or B
A= not defined $has_alienB= $force_check
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | $has_alien = undef; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | eval { require Alien::Plotly::Orca; }; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if ( !$@ and Alien::Plotly::Orca->install_type eq 'share' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $@ and "Alien::Plotly::Orca"->install_type eq "share"
MC/DC0%0/2
not $@ and "Alien::Plotly::Orca"->install_type eq "share"
not $@
"Alien::Plotly::Orca"->install_type eq "share"
Truth table33%1/3
not(A) and B
A= not $@B= "Alien::Plotly::Orca"->install_type eq "share"
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $ENV{PATH} = join( $Config{path_sep}, Alien::Plotly::Orca->bin_dir, $ENV{PATH} ); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $has_alien = 1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | $has_alien = 0; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | return $has_alien; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub orca { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
orca: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( orca_available() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my $plot = $params{plot}; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $file = path( $params{file} ); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $format = $params{format}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | unless ( defined $format ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ($format) = $file =~ /\.([^\.]+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $plotlyjs = $params{plotlyjs} // $params{plotly} // _plotlyjs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
($params{"plotlyjs"} // $params{"plotly"}) // _plotlyjs()
$params{"plotlyjs"} // $params{"plotly"}
MC/DC0%0/3
($params{"plotlyjs"} // $params{"plotly"}) // _plotlyjs()
$params{"plotlyjs"}
$params{"plotly"}
_plotlyjs()
Truth table0%0/4
A // B // C
A= $params{"plotlyjs"}B= $params{"plotly"}C= _plotlyjs()
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $tmp_json = Path::Tiny->tempfile( SUFFIX => '.json' ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $tmp_json->spew_raw( $plot->to_json_text ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # For now have to explicitly specify -d as otherwise orca would | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # not be able to store output to a different path other than cwd. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # See https://github.com/plotly/orca/issues/101 | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my @orca_line = ( $ORCA_COMMAND, 'graph', $tmp_json, '--plotlyjs', $plotlyjs, '-d', $file->parent, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | '-o', $file->basename, ( $format ? ( '--format', $format ) : () ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | for my $arg (qw(mathjax scale width height)) { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ( my $val = $params{$arg} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | push @orca_line, ( "--${arg}", $val ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | for my $arg (qw(safe verbose debug)) { | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $params{$arg} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | push @orca_line, "--${arg}"; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $rc = system(@orca_line); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return 1 unless ( $rc >> 8 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub correct_orca { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
correct_orca: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $orca_help = `$ORCA_COMMAND --help`; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return ( $orca_help =~ /plotly/i ); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | sub orca_available { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
orca_available: called CC=6 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | my ($force_check) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | state $available; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if ( !defined $available or $force_check ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $available or $force_check
MC/DC0%0/2
not defined $available or $force_check
not defined $available
$force_check
Truth table33%1/3
not(A) or B
A= not defined $availableB= $force_check
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | $available = undef; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if ( not _check_alien($force_check) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
_check_alien($force_check) or which($ORCA_COMMAND) and correct_orca()
which($ORCA_COMMAND) and correct_orca()
MC/DC0%0/3
_check_alien($force_check) or which($ORCA_COMMAND) and correct_orca()
_check_alien($force_check)
which($ORCA_COMMAND)
correct_orca()
Truth table25%1/4
A or B and C
A= _check_alien($force_check)B= which($ORCA_COMMAND)C= correct_orca()
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | and ( not which($ORCA_COMMAND) or not correct_orca() ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | die "Orca tool (its 'orca' command) must be installed and in " | |||||||||||||||||||||||||||||||||||||||||
| 96 | . "PATH in order to export images. " | ||||||||||||||||||||||||||||||||||||||||||
| 97 | . "Either install Alien::Plotly::Orca from CPAN, or install " | ||||||||||||||||||||||||||||||||||||||||||
| 98 | . "it manually (see https://github.com/plotly/orca#installation)"; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $available = 1; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $available; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub orca_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
orca_version: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my ($force_check) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | state $version; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( _check_alien($force_check) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return Alien::Plotly::Orca->version; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ( orca_available($force_check) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $version = `$ORCA_COMMAND --version`; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | chomp($version); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | return $version; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||||||||||||