Moose/DataFile.pm

filter: Statementstmt 93.0% 80 / 86 Branchbran 50.0% 8 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 2 totaltotal 86.4% 108 / 125 SCAR 22.2
CC
9
Cov
86%
_open_file
6.2
_close_file
6.0
DEMOLISH
2.5
CRAP 9.2
« Moose/Instrument/SCPIBlock.pm ↑ Summary Moose/Countdown.pm »
1 package Lab::Moose::DataFile;
2 27 $Lab::Moose::DataFile::VERSION = '3.881';
3 #ABSTRACT: Base class for data file types
4  
5 27 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Lab::Moose::DataFolder;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 ▶use File::Basename qw/dirname basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use Lab::Moose::Catfile 'our_catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 27 ▶use Net::RFC3161::Timestamp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 27 has folder => (
28 is => 'ro',
29 isa => 'Lab::Moose::DataFolder',
30 required => 1,
31 );
32  
33 27 has filename => (
34 is => 'ro',
35 isa => 'Str',
36 required => 1,
37 );
38  
39 27 has autoflush => (
40 is => 'ro',
41 isa => 'Bool',
42 default => 1
43 );
44  
45 27 has timestamp => (
46 is => 'ro',
47 isa => 'Bool',
48 default => 0
49 );
50  
51 27 has tsauthority => (
52 is => 'ro',
53 isa => 'Str',
54 default => 'dfn.de'
55 );
56  
57 27 has filehandle => (
58 is => 'ro',
59 isa => 'FileHandle',
60 writer => '_filehandle',
61 init_arg => undef
62 );
63  
64 27 has mode => (
65 is => 'ro',
66 isa => 'Str',
67 builder => '_build_file_mode',
68 );
69  
70 sub _build_file_mode {
71 37 ▶ return '+>';
Subroutine _build_file_mode: called CC=1 SCAR=0.0
72 }
73  
74 # relative to cwd.
75 27 has path => (
76 is => 'ro',
77 isa => 'Str',
78 writer => '_path',
79 init_arg => undef
80 );
81  
82 sub BUILD {
83 110 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
84 110 $self->_open_file();
85 }
86  
87 # Subclass this to use fancy stuff like IO::Compress
88 sub _open_filehandle {
89 109 ▶ my $self = shift;
Subroutine _open_filehandle: called CC=2 SCAR=7.0
90 109 my $path = shift;
91  
92 109 ▶ open my $fh, $self->mode(), $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $self->mode, $path or croak("cannot open '${path}': $!") 0 109 109
93 or croak "cannot open '$path': $!";
94  
95 109 return $fh;
96 }
97  
98 # Let subclasses add suffixes
99 sub _modify_file_path {
100 109 ▶ my $self = shift;
Subroutine _modify_file_path: called CC=1 SCAR=0.0
101 109 my $path = shift;
102 109 return $path;
103 }
104  
105 sub _open_file {
106 110 ▶ my $self = shift;
Subroutine _open_file: called CC=6 SCAR=18.3
107  
108 110 my $folder = $self->folder->path();
109 110 my $filename = $self->filename();
110  
111 110 my $dirname = dirname($filename);
112 110 my $dirpath = our_catfile( $folder, $dirname );
113  
114 110 ▶ if ( not -e $dirpath ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dirpath) 3 107 110
115 3 ▶ make_path($dirpath)
Branch: 1 branch
BranchTrueFalseTotal
make_path($dirpath) or croak("cannot make directory '${dirname}'") 0 3 3
116 or croak "cannot make directory '$dirname'";
117 }
118  
119 110 my $path = our_catfile( $folder, $filename );
120  
121 110 $path = $self->_modify_file_path($path);
122  
123 110 $self->_path($path);
124  
125 110 ▶ if ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 0 110 110
126 0 croak "path '$path' does already exist";
127 }
128  
129 110 my $fh = $self->_open_filehandle($path);
130  
131 # Do not use crlf line endings on ms-w32.
132 110 ▶ binmode $fh
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("cannot set binmode for '${path}'") 0 110 110
133 or croak "cannot set binmode for '$path'";
134  
135 110 ▶ if ( $self->autoflush() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoflush) 110 0 110
136 110 $fh->autoflush();
137 }
138  
139 110 $self->_filehandle($fh);
140 }
141  
142  
143 sub _close_file {
144 0 ▶ my $self = shift;
Subroutine _close_file: not called CC=2 SCAR=17.9
145 0 my $fh = $self->filehandle();
146  
147 0 ▶ close $fh || croak "cannot close datafile";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("cannot close datafile") 0 0 0
148 }
149  
150 sub DEMOLISH {
151 37 ▶ my $self = shift;
Subroutine DEMOLISH: called CC=2 SCAR=9.2
Pod undocumented
152  
153 37 ▶ if ( $self->timestamp() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->timestamp) 0 37 37
154  
155 0 $self->_close_file();
156 0 attest_file($self->path(), $self->path().".ts", $self->tsauthority());
157  
158 }
159 };
160  
161 27 __PACKAGE__->meta->make_immutable();
162  
163 27 1;
164  
165 __END__
166  
167 =pod
168  
169 =encoding UTF-8
170  
171 =head1 NAME
172  
173 Lab::Moose::DataFile - Base class for data file types
174  
175 =head1 VERSION
176  
177 version 3.881
178  
179 =head1 METHODS
180  
181 =head2 new
182  
183 my $datafile = Lab::Moose::DataFile->new(
184 folder => $folder,
185 filename => 'myfile.dat'
186 );
187  
188 =head3 Attributes
189  
190 =over
191  
192 =item folder (required)
193  
194 A L<Lab::Moose::DataFolder> object.
195  
196 =item filename (required)
197  
198 filename in the folder.
199  
200 =item autoflush
201  
202 Enable autoflush of the filehandle. On by default.
203  
204 =item timestamp
205  
206 Request RFC3616 compatible timestamps of the measured data
207 upon completion, from the timestamp authority specified via
208 tsauthority. Off by default.
209  
210 If enabled, an additional file with the suffix .ts containing
211 the signed timestamp will be created.
212  
213 =item tsauthority
214  
215 When timestamps are requested, specify the authority to be
216 contacted. The parameter can be a shorthand as, e.g., "dfn.de";
217 see L<Net::RFC3161::Timestamp> for details. If no valid shorthand
218 is found, the parameter is interpreted as a RFC3161 URL.
219  
220 =item mode
221  
222 C<open> mode. Defaults to ">".
223  
224 =back
225  
226 =head3 Read-only attributes available after creation:
227  
228 =over
229  
230 =item path
231  
232 path relative to the current working directory.
233  
234 =item filehandle
235  
236 =back
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
241  
242 Copyright 2016 Simon Reinhardt
243 2017 Andreas K. Huettel, Simon Reinhardt
244 2018 Simon Reinhardt
245 2020-2021 Andreas K. Huettel
246  
247  
248 This is free software; you can redistribute it and/or modify it under
249 the same terms as the Perl 5 programming language system itself.
250  
251 =cut
« Moose/Instrument/SCPIBlock.pm ↑ Summary Moose/Countdown.pm »