| 1 | package Treex::Core::Files; | ||||||||||||||||||||||||||||
| 2 | 72 | $Treex::Core::Files::VERSION = '2.20210102'; | |||||||||||||||||||||||||||
| 3 | 72 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 72 | ▶ | use MooseX::SemiAffordanceAccessor 0.09; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 72 | ▶ | use Treex::Core::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 72 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 72 | ▶ | use File::Slurp 9999.19; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 72 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 72 | ▶ | use PerlIO::via::gzip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 72 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 72 | has filenames => ( | |||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 15 | writer => '_set_filenames', | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 72 | has file_number => ( | |||||||||||||||||||||||||||
| 19 | isa => 'Int', | ||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | writer => '_set_file_number', | ||||||||||||||||||||||||||||
| 22 | default => 0, | ||||||||||||||||||||||||||||
| 23 | init_arg => undef, | ||||||||||||||||||||||||||||
| 24 | documentation => 'Number of the current file', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 72 | has current_filehandle => ( | |||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | writer => '_set_current_filehandle', | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 72 | has encoding => ( | |||||||||||||||||||||||||||
| 33 | isa => 'Str', | ||||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | default => 'utf8', | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 72 | has join_files_for_next_line => ( | |||||||||||||||||||||||||||
| 39 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | default => 1, | ||||||||||||||||||||||||||||
| 42 | documentation => 'Should method next_line automatically go to the next file when finished reading the current file?', | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 10 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 10 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 47 | 10 | ▶ | if ($args->{filenames}){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | ## Nothing to do, $args->{filenames} are ArrayRef[Str] checked by Moose | ||||||||||||||||||||||||||||
| 49 | } elsif(defined $args->{string}){ | ||||||||||||||||||||||||||||
| 50 | 10 | $self->_set_filenames( $self->string_to_filenames( $args->{string} ) ); | |||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||
| 52 | 0 | log_fatal 'One of the parameters (filenames, string) is required'; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 10 | return; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | sub string_to_filenames { | ||||||||||||||||||||||||||
|
Subroutine
string_to_filenames: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 10 | my ( $self, $string ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # "!" means glob pattern which can contain {dir1,dir2} | ||||||||||||||||||||||||||||
| 61 | # so it cannot be combined with separating tokens with comma. | ||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ($string =~ /^!(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | my @filenames = glob $1; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | log_warn "No filenames matched '$1' pattern" if !@filenames; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return \@filenames; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10 | return [ map { $self->_token_to_filenames($_) } grep {/./} split /[ ,]+/, $string ]; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 10 | ▶ | sub _token_to_filenames { | ||||||||||||||||||||||||||
|
Subroutine
_token_to_filenames: called CC=7 SCAR=25.2
| |||||||||||||||||||||||||||||
| 72 | 10 | my ( $self, $token ) = @_; | |||||||||||||||||||||||||||
| 73 | 10 | ▶ | if ($token =~ /^!(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | my @filenames = glob $1; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | log_warn "No filenames matched '$1' pattern" if !@filenames; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | return @filenames; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 10 | ▶ | return $token if $token !~ s/^@(.*)/$1/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | my $filelist = $token eq '-' ? \*STDIN : $token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | my @filenames = grep { $_ ne '' } read_file( $filelist, chomp => 1 ); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Filnames in a filelist can be relative to the filelist directory. | ||||||||||||||||||||||||||||
| 83 | 1 | my $dir = dirname($token); | |||||||||||||||||||||||||||
| 84 | 1 | ▶ | return @filenames if $dir eq '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return map {!m{^/} ? "$dir/$_" : $_} @filenames; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | sub number_of_files { | ||||||||||||||||||||||||||
|
Subroutine
number_of_files: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 90 | 3 | return scalar @{ $self->filenames }; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | ▶ | sub current_filename { | ||||||||||||||||||||||||||
|
Subroutine
current_filename: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | 5 | ▶ | return if $self->file_number == 0 || $self->file_number > @{ $self->filenames }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->file_number == 0 or $self->file_number > @{$self->filenames;}
MC/DC50%1/2
$self->file_number == 0 or $self->file_number > @{$self->filenames;}
$self->file_number == 0
$self->file_number > @{$self->filenames;}
Truth table66%2/3
A or B
A= $self->file_number == 0B= $self->file_number > @{$self->filenames;}
| |||||||||||||||||||||||||||||
| 96 | 4 | return $self->filenames->[ $self->file_number - 1 ]; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub next_filename { | ||||||||||||||||||||||||||
|
Subroutine
next_filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 101 | 2 | $self->_set_file_number( $self->file_number + 1 ); | |||||||||||||||||||||||||||
| 102 | 2 | return $self->current_filename(); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub has_next_file { | ||||||||||||||||||||||||||
|
Subroutine
has_next_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | 0 | return $self->file_number < $self->number_of_files; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub get_hash { | ||||||||||||||||||||||||||
|
Subroutine
get_hash: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $md5 = Digest::MD5->new(); | |||||||||||||||||||||||||||
| 114 | 0 | for my $filename (@{$self->filenames}) { | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ( -f $filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $md5->add($filename); | |||||||||||||||||||||||||||
| 117 | 0 | $md5->add((stat($filename))[9]); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | return $md5->hexdigest; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | sub next_filehandle { | ||||||||||||||||||||||||||
|
Subroutine
next_filehandle: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | 2 | my $filename = $self->next_filename(); | |||||||||||||||||||||||||||
| 126 | 2 | my $FH = $self->current_filehandle; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | ▶ | if (!defined $filename){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 129 | 1 | $FH = undef; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | elsif ( $filename eq '-' ) { | ||||||||||||||||||||||||||||
| 132 | 0 | binmode STDIN, $self->encoding; | |||||||||||||||||||||||||||
| 133 | 0 | $FH = \*STDIN; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 1 | ▶ | my $mode = $filename =~ /[.]gz$/ ? '<:via(gzip):' : '<:'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 1 | $mode .= $self->encoding; | |||||||||||||||||||||||||||
| 138 | 1 | ▶ | open $FH, $mode, $filename or log_fatal "Can't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 2 | $self->_set_current_filehandle($FH); | |||||||||||||||||||||||||||
| 141 | 2 | return $FH; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | sub next_file_text { | ||||||||||||||||||||||||||
|
Subroutine
next_file_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 146 | 2 | ▶ | my $FH = $self->next_filehandle() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Slurp that is compatible with Perl::IO::via::gzip. | ||||||||||||||||||||||||||||
| 149 | 1 | local $/ = undef; | |||||||||||||||||||||||||||
| 150 | 1 | return <$FH>; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub next_line { | ||||||||||||||||||||||||||
|
Subroutine
next_line: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | my $FH = $self->current_filehandle; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | return if !$FH && !$self->join_files_for_next_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$FH or $self->join_files_for_next_line
MC/DC0%0/2
$FH or $self->join_files_for_next_line
$FH
$self->join_files_for_next_line
Truth table0%0/3
A or B
A= $FHB= $self->join_files_for_next_line
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ( !$FH ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $FH = $self->next_filehandle() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | return <$FH>; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | #<<< | ||||||||||||||||||||||||||||
| 164 | 72 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | coerce 'Treex::Core::Files' | ||||||||||||||||||||||||||||
| 166 | => from 'Str' | ||||||||||||||||||||||||||||
| 167 | 9 | ▶ | => via { Treex::Core::Files->new( string => $_ ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | => from 'ArrayRef[Str]' | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | => via { Treex::Core::Files->new( filenames => $_ ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 170 | #>>> | ||||||||||||||||||||||||||||
| 171 | # TODO: POD, next_filehandle, gz support | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 72 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||