| 1 | package Lab::Moose::DataFile; | ||||||||||
| 2 | 27 | $Lab::Moose::DataFile::VERSION = '3.931'; | |||||||||
| 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 | 37 | ▶ | sub _build_file_mode { | ||||||||
|
Subroutine
_build_file_mode: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 37 | return '+>'; | |||||||||
| 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 | 110 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 83 | 110 | my $self = shift; | |||||||||
| 84 | 110 | $self->_open_file(); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | # Subclass this to use fancy stuff like IO::Compress | ||||||||||
| 88 | 109 | ▶ | sub _open_filehandle { | ||||||||
|
Subroutine
_open_filehandle: called CC=2 SCAR=7.0
| |||||||||||
| 89 | 109 | my $self = shift; | |||||||||
| 90 | 109 | my $path = shift; | |||||||||
| 91 | |||||||||||
| 92 | 109 | ▶ | open my $fh, $self->mode(), $path | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | or croak "cannot open '$path': $!"; | ||||||||||
| 94 | |||||||||||
| 95 | 109 | return $fh; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # Let subclasses add suffixes | ||||||||||
| 99 | 109 | ▶ | sub _modify_file_path { | ||||||||
|
Subroutine
_modify_file_path: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 109 | my $self = shift; | |||||||||
| 101 | 109 | my $path = shift; | |||||||||
| 102 | 109 | return $path; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 110 | ▶ | sub _open_file { | ||||||||
|
Subroutine
_open_file: called CC=6 SCAR=18.3
| |||||||||||
| 106 | 110 | my $self = shift; | |||||||||
| 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
| |||||||||||
| 115 | 3 | ▶ | make_path($dirpath) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 133 | or croak "cannot set binmode for '$path'"; | ||||||||||
| 134 | |||||||||||
| 135 | 110 | ▶ | if ( $self->autoflush() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 110 | $fh->autoflush(); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 110 | $self->_filehandle($fh); | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | sub _close_file { | ||||||||
|
Subroutine
_close_file: not called CC=2 SCAR=17.9
| |||||||||||
| 144 | 0 | my $self = shift; | |||||||||
| 145 | 0 | my $fh = $self->filehandle(); | |||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | close $fh || croak "cannot close datafile"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 37 | ▶ | sub DEMOLISH { | ||||||||
|
Subroutine
DEMOLISH: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||
| 151 | 37 | my $self = shift; | |||||||||
| 152 | |||||||||||
| 153 | 37 | ▶ | if ( $self->timestamp() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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__ | ||||||||||