File Coverage

blib/lib/Lab/Moose/DataFile.pm
Criterion Covered Total %
statement 69 75 92.0
branch 8 16 50.0
condition n/a
subroutine 20 21 95.2
pod 0 2 0.0
total 97 114 85.0


line stmt bran cond sub pod time code
1             package Lab::Moose::DataFile;
2             $Lab::Moose::DataFile::VERSION = '3.881';
3             #ABSTRACT: Base class for data file types
4              
5 27     27   16673 use v5.20;
  27         107  
6              
7 27     27   168 use warnings;
  27         55  
  27         740  
8 27     27   142 use strict;
  27         61  
  27         507  
9              
10 27     27   143 use Moose;
  27         69  
  27         162  
11 27     27   174524 use MooseX::StrictConstructor;
  27         77  
  27         211  
12 27     27   85857 use MooseX::Params::Validate;
  27         94  
  27         206  
13              
14 27     27   12513 use Lab::Moose::DataFolder;
  27         82  
  27         1041  
15              
16 27     27   192 use File::Basename qw/dirname basename/;
  27         80  
  27         1740  
17 27     27   196 use File::Path 'make_path';
  27         118  
  27         1956  
18 27     27   203 use Lab::Moose::Catfile 'our_catfile';
  27         60  
  27         1320  
19 27     27   208 use IO::Handle;
  27         69  
  27         1136  
20              
21 27     27   14319 use Net::RFC3161::Timestamp;
  27         1323579  
  27         1931  
22              
23 27     27   241 use Carp;
  27         73  
  27         1459  
24              
25 27     27   221 use namespace::autoclean;
  27         59  
  27         294  
26              
27             has folder => (
28             is => 'ro',
29             isa => 'Lab::Moose::DataFolder',
30             required => 1,
31             );
32              
33             has filename => (
34             is => 'ro',
35             isa => 'Str',
36             required => 1,
37             );
38              
39             has autoflush => (
40             is => 'ro',
41             isa => 'Bool',
42             default => 1
43             );
44              
45             has timestamp => (
46             is => 'ro',
47             isa => 'Bool',
48             default => 0
49             );
50              
51             has tsauthority => (
52             is => 'ro',
53             isa => 'Str',
54             default => 'dfn.de'
55             );
56              
57             has filehandle => (
58             is => 'ro',
59             isa => 'FileHandle',
60             writer => '_filehandle',
61             init_arg => undef
62             );
63              
64             has mode => (
65             is => 'ro',
66             isa => 'Str',
67             builder => '_build_file_mode',
68             );
69              
70             sub _build_file_mode {
71 37     37   2539 return '+>';
72             }
73              
74             # relative to cwd.
75             has path => (
76             is => 'ro',
77             isa => 'Str',
78             writer => '_path',
79             init_arg => undef
80             );
81              
82             sub BUILD {
83 110     110 0 4987 my $self = shift;
84 110         315 $self->_open_file();
85             }
86              
87             # Subclass this to use fancy stuff like IO::Compress
88             sub _open_filehandle {
89 109     109   205 my $self = shift;
90 109         208 my $path = shift;
91              
92 109 50       4321 open my $fh, $self->mode(), $path
93             or croak "cannot open '$path': $!";
94              
95 109         732 return $fh;
96             }
97              
98             # Let subclasses add suffixes
99             sub _modify_file_path {
100 109     109   195 my $self = shift;
101 109         158 my $path = shift;
102 109         232 return $path;
103             }
104              
105             sub _open_file {
106 110     110   177 my $self = shift;
107              
108 110         3408 my $folder = $self->folder->path();
109 110         3026 my $filename = $self->filename();
110              
111 110         3049 my $dirname = dirname($filename);
112 110         444 my $dirpath = our_catfile( $folder, $dirname );
113              
114 110 100       2266 if ( not -e $dirpath ) {
115 3 50       537 make_path($dirpath)
116             or croak "cannot make directory '$dirname'";
117             }
118              
119 110         517 my $path = our_catfile( $folder, $filename );
120              
121 110         423 $path = $self->_modify_file_path($path);
122              
123 110         4117 $self->_path($path);
124              
125 110 50       2970 if ( -e $path ) {
126 0         0 croak "path '$path' does already exist";
127             }
128              
129 110         548 my $fh = $self->_open_filehandle($path);
130              
131             # Do not use crlf line endings on ms-w32.
132 110 50       540 binmode $fh
133             or croak "cannot set binmode for '$path'";
134              
135 110 50       4195 if ( $self->autoflush() ) {
136 110         431 $fh->autoflush();
137             }
138              
139 110         8477 $self->_filehandle($fh);
140             }
141              
142              
143             sub _close_file {
144 0     0   0 my $self = shift;
145 0         0 my $fh = $self->filehandle();
146              
147 0 0       0 close $fh || croak "cannot close datafile";
148             }
149              
150             sub DEMOLISH {
151 37     37 0 243 my $self = shift;
152              
153 37 50       1067 if ( $self->timestamp() ) {
154              
155 0           $self->_close_file();
156 0           attest_file($self->path(), $self->path().".ts", $self->tsauthority());
157              
158             }
159             };
160              
161             __PACKAGE__->meta->make_immutable();
162              
163             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