lib/Convert/Pheno/IO/FileIO.pm

filter: Statementstmt 93.3% 113 / 121 Branchbran 75.0% 21 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.4% 27 / 28 Podpod 0.0% 0 / 6 totaltotal 87.9% 161 / 183 CC 15 SCAR 9.8
CC
15
Cov
90%
_slurp_text
12.0
io_yaml_or_json
4.0
_slurp_raw
3.0
CRAP 2.7
« lib/Convert/Pheno/Tabular/REDCap/Dictionary.pm ↑ Summary lib/Convert/Pheno/ExecutionContext.pm »
1 package Convert::Pheno::IO::FileIO;
2  
3 138 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 138 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 138 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 138 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 138 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 138 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 138 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 138 ▶use YAML::XS qw(Load Dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 138 $YAML::XS::Boolean = 'JSON::PP'; # use JSON::PP::Boolean objects
12 138 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 138 ▶use IO::Compress::Gzip qw($GzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 138 ▶use IO::Uncompress::Gunzip qw($GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 138 ▶use Data::Leaf::Walker;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 138 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 138 our @EXPORT = qw(read_json read_yaml io_yaml_or_json write_json write_yaml);
18  
19 #########################
20 #########################
21 # SUBROUTINES FOR I/O #
22 #########################
23 #########################
24  
25 0 ▶sub _slurp_text {
Subroutine _slurp_text: not called CC=3 SCAR=24.8
26 0 my ($file) = @_;
27  
28 0 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) 0 0 0
29 # Gzipped text has to be decoded explicitly because Path::Tiny only
30 # covers plain files. Keep this logic isolated so plain-file semantics
31 # stay unchanged elsewhere.
32 0 ▶ my $fh = IO::Uncompress::Gunzip->new( $file, MultiStream => 1 )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Uncompress::Gunzip"->new($file, "MultiStream", 1) or die "Cannot gunzip <$file>: $Convert::Pheno::IO::FileIO::GunzipError" 0 0 0
33 or die "Cannot gunzip <$file>: $GunzipError";
34 0 binmode( $fh, ':encoding(UTF-8)' );
35 0 return do { local $/; <$fh> };
36 }
37  
38 0 return path($file)->slurp_utf8;
39 }
40  
41 385 ▶sub _slurp_raw {
Subroutine _slurp_raw: called CC=3 SCAR=11.0
42 385 my ($file) = @_;
43  
44 385 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) 4 381 385
45 # JSON::XS and YAML::XS do not want the same thing:
46 # JSON decoding expects UTF-8 bytes, while some YAML paths are safer
47 # when delegated to YAML::XS file APIs. This helper is only for cases
48 # where raw bytes are the correct boundary.
49 4 ▶ my $fh = IO::Uncompress::Gunzip->new( $file, MultiStream => 1 )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Uncompress::Gunzip"->new($file, "MultiStream", 1) or die "Cannot gunzip <$file>: $Convert::Pheno::IO::FileIO::GunzipError" 4 0 4
50 or die "Cannot gunzip <$file>: $GunzipError";
51 4 return do { local $/; <$fh> };
52 }
53  
54 381 return path($file)->slurp_raw;
55 }
56  
57 119 ▶sub _spew_text {
Subroutine _spew_text: called CC=3 SCAR=11.0
58 119 my ( $file, $text ) = @_;
59  
60 119 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) 2 117 119
61 # JSON strings produced without ->utf8 are Perl text and should be
62 # encoded exactly once at the filehandle boundary.
63 # Gzip's binmode is a no-op; its Encode option handles Perl text.
64 2 ▶ my $fh = IO::Compress::Gzip->new($file, Encode => 'UTF-8')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Compress::Gzip"->new($file, "Encode", "UTF-8") or die "Cannot gzip <$file>: $Convert::Pheno::IO::FileIO::GzipError" 2 0 2
65 or die "Cannot gzip <$file>: $GzipError";
66 2 print {$fh} $text;
67 2 close $fh;
68 2 return 1;
69 }
70  
71 117 path($file)->spew_utf8($text);
72 117 return 1;
73 }
74  
75 5 ▶sub _spew_raw {
Subroutine _spew_raw: called CC=3 SCAR=11.0
76 5 my ( $file, $text ) = @_;
77  
78 5 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) 2 3 5
79 # Keep a raw gzip path for payloads already serialized by another
80 # library. Re-encoding here caused the mojibake regressions we saw in
81 # the fixtures.
82 2 ▶ my $fh = IO::Compress::Gzip->new($file)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Compress::Gzip"->new($file) or die "Cannot gzip <$file>: $Convert::Pheno::IO::FileIO::GzipError" 2 0 2
83 or die "Cannot gzip <$file>: $GzipError";
84 2 print {$fh} $text;
85 2 close $fh;
86 2 return 1;
87 }
88  
89 3 path($file)->spew_raw($text);
90 3 return 1;
91 }
92  
93 383 ▶sub read_json {
Subroutine read_json: called CC=1 SCAR=0.0
Pod undocumented
94 383 my $str = _slurp_raw(shift);
95 383 return decode_json($str); # Decode to Perl data structure
96 }
97  
98 6 ▶sub read_yaml {
Subroutine read_yaml: called CC=2 SCAR=6.9
Pod undocumented
99 6 my $file = shift;
100 6 ▶ my $data =
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\.gz$/ ? : 2 4 6
101 $file =~ /\.gz$/
102 ? Load( _slurp_raw($file) )
103 : YAML::XS::LoadFile($file); # preserve legacy plain-file behavior
104 # Do not replace the plain-file branch with slurp+Load. LoadFile and
105 # Path::Tiny had stable semantics here and changing that broke fixtures
106 # containing non-ASCII text.
107 6 traverse_yaml_data_to_coerce_numbers($data)
108 ; # revert floatings getting stringified by YAML::XS
109 6 return $data;
110 }
111  
112 351 ▶sub io_yaml_or_json {
Subroutine io_yaml_or_json: called CC=4 SCAR=13.9
Pod undocumented
113 351 my $arg = shift;
114 351 my $file = $arg->{filepath};
115 351 my $mode = $arg->{mode};
116 351 ▶ my $data = $mode eq 'write' ? $arg->{data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'write' ? : 124 227 351
117  
118 # Checking for the below extension
119 351 my @exts = map { $_, $_ . '.gz' } qw(.yaml .yml .json .jsonld .ymlld .yamlld);
120 351 my $msg = qq(Can't recognize <$file> extension. Extensions allowed are: )
121 . ( join ',', @exts ) . "\n";
122 351 my ( undef, undef, $ext ) = fileparse( $file, @exts );
123 351 ▶ die $msg unless any { $_ eq $ext } @exts;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $ext; } , @exts) 1 350 351
124  
125 # To simplify return values, we create a hash
126 350 $ext =~ s/\.gz$//;
127 350 $ext =~ tr/a.//d; # Unify $ext (delete 'a' and '.')
128 350 $ext =~ s/ld$//; # delete ending ld
129 350 my $return = {
130 read => { json => \&read_json, yml => \&read_yaml },
131 write => { json => \&write_json, yml => \&write_yaml }
132 };
133  
134 # We return according to the mode (read or write) and format
135 return $mode eq 'read'
136 ? $return->{$mode}{$ext}->($file)
137 350 ▶ : $return->{$mode}{$ext}->( { filepath => $file, data => $data } );
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'read' ? : 226 124 350
138 }
139  
140 119 ▶sub write_json {
Subroutine write_json: called CC=1 SCAR=0.0
Pod undocumented
141 119 my $arg = shift;
142 119 my $file = $arg->{filepath};
143 119 my $json_data = $arg->{data};
144 # Intentionally omit ->utf8 here. We want a Perl character string for
145 # plain files and let the writer encode once at the boundary.
146 119 my $json = JSON::XS->new->canonical->pretty->encode($json_data);
147 119 return _spew_text( $file, $json );
148 }
149  
150 5 ▶sub write_yaml {
Subroutine write_yaml: called CC=1 SCAR=0.0
Pod undocumented
151 5 my $arg = shift;
152 5 my $file = $arg->{filepath};
153 5 my $json_data = $arg->{data};
154 # Dump returns UTF-8 bytes. Write them unchanged to avoid both double
155 # encoding and Windows LF-to-CRLF translation, including inside values.
156 5 ▶ return _spew_raw( $file, Dump($json_data) );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
157 }
158  
159 6 ▶sub traverse_yaml_data_to_coerce_numbers {
Subroutine traverse_yaml_data_to_coerce_numbers: called CC=3 SCAR=11.0
Pod undocumented
160 6 my $data = shift;
161  
162 # Traversing the data to force numbers to be numbers
163 # NB: Changing the original data structure
164 6 my $walker = Data::Leaf::Walker->new($data);
165 6 ▶ while ( my ( $key_path, $value ) = $walker->each ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $walker->each) 5368 6 5374
166 5368 ▶ $walker->store( $key_path, $value + 0 )
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::looks_like_number($value) 614 4754 5368
167 if Scalar::Util::looks_like_number $value;
168 }
169 }
170  
171 138 1;
« lib/Convert/Pheno/Tabular/REDCap/Dictionary.pm ↑ Summary lib/Convert/Pheno/ExecutionContext.pm »