Treex/Core/Files.pm

filter: Statementstmt 73.3% 77 / 105 Branchbran 42.5% 17 / 40 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 81.8% 18 / 22 Podpod 80.0% 8 / 10 totaltotal 65.7% 123 / 187 CC 24 SCAR 20.5
CC
24
Cov
64%
next_line
30.0
_token_to_filenames
12.4
get_hash
12.0
CRAP 7.8
« Treex/Core/Log.pm ↑ Summary Treex/Core/Bundle.pm »
1 package Treex::Core::Files;
2 73 $Treex::Core::Files::VERSION = '2.20151210';
3 73 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 73 ▶use MooseX::SemiAffordanceAccessor 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 73 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 73 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 73 ▶use File::Slurp 9999.19;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 73 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 73 ▶use PerlIO::via::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 73 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 73 has filenames => (
13 is => 'ro',
14 isa => 'ArrayRef[Str]',
15 writer => '_set_filenames',
16 );
17  
18 73 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 73 has current_filehandle => (
28 is => 'ro',
29 writer => '_set_current_filehandle',
30 );
31  
32 73 has encoding => (
33 isa => 'Str',
34 is => 'rw',
35 default => 'utf8',
36 );
37  
38 73 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 sub BUILD {
46 10 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: called CC=3 SCAR=12.0
Pod undocumented
47 10 ▶ if ($args->{filenames}){
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'filenames'}) { } 0 10 10
elsif (defined $args->{'string'}) { } 10 0 10
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 sub string_to_filenames {
58 10 ▶ my ( $self, $string ) = @_;
Subroutine string_to_filenames: called CC=3 SCAR=14.2
Pod documented
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
BranchTrueFalseTotal
if ($string =~ /^!(.+)/) 0 10 10
63 0 my @filenames = glob $1;
64 0 ▶ log_warn "No filenames matched '$1' pattern" if !@filenames;
Branch: 1 branch
BranchTrueFalseTotal
unless @filenames 0 0 0
65 0 return \@filenames;
66 }
67
68 10 return [ map { $self->_token_to_filenames($_) } grep {/./} split /[ ,]+/, $string ];
69 }
70  
71 sub _token_to_filenames {
72 10 ▶ my ( $self, $token ) = @_;
Subroutine _token_to_filenames: called CC=7 SCAR=25.2
73 10 ▶ if ($token =~ /^!(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^!(.+)/) 0 10 10
74 0 my @filenames = glob $1;
75 0 ▶ log_warn "No filenames matched '$1' pattern" if !@filenames;
Branch: 1 branch
BranchTrueFalseTotal
unless @filenames 0 0 0
76 0 return @filenames;
77 }
78 10 ▶ return $token if $token !~ s/^@(.*)/$1/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ s/^@(.*)/$1/ 9 1 10
79 1 ▶ my $filelist = $token eq '-' ? \*STDIN : $token;
Branch: 1 branch
BranchTrueFalseTotal
$token eq '-' ? : 0 1 1
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
BranchTrueFalseTotal
if $dir eq "." 1 0 1
85 0 ▶ return map {!m{^/} ? "$dir/$_" : $_} @filenames;
Branch: 1 branch
BranchTrueFalseTotal
!m[^/] ? : 0 0 0
86 }
87  
88 sub number_of_files {
89 3 ▶ my ($self) = @_;
Subroutine number_of_files: called CC=1 SCAR=0.0
Pod documented
90 3 return scalar @{ $self->filenames };
91 }
92  
93 sub current_filename {
94 5 ▶ my ($self) = @_;
Subroutine current_filename: called CC=3 SCAR=11.0
Pod documented
95 5 ▶ return if $self->file_number == 0 || $self->file_number > @{ $self->filenames };
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_number == 0 or $self->file_number > @{$self->filenames;} 1 4 5
Condition66%2/3
$self->file_number == 0 or $self->file_number > @{$self->filenames;}
l!l&&r!l&&!r
014
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;}
ABresult
1X1
011
000
96 4 return $self->filenames->[ $self->file_number - 1 ];
97 }
98  
99 sub next_filename {
100 2 ▶ my ($self) = @_;
Subroutine next_filename: called CC=1 SCAR=0.0
Pod documented
101 2 $self->_set_file_number( $self->file_number + 1 );
102 2 return $self->current_filename();
103 }
104  
105 sub has_next_file {
106 0 ▶ my ($self) = @_;
Subroutine has_next_file: not called CC=1 SCAR=6.9
Pod undocumented
107 0 return $self->file_number < $self->number_of_files;
108 }
109  
110 sub get_hash {
111 0 ▶ my $self = shift;
Subroutine get_hash: not called CC=3 SCAR=24.8
Pod documented
112  
113 0 my $md5 = Digest::MD5->new();
114 0 for my $filename (@{$self->filenames}) {
115 0 ▶ if ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 0 0
116 0 $md5->add($filename);
117 0 $md5->add((stat($filename))[9]);
118 }
119 }
120 0 return $md5->hexdigest;
121 }
122  
123 sub next_filehandle {
124 2 ▶ my ($self) = @_;
Subroutine next_filehandle: called CC=5 SCAR=16.8
Pod documented
125 2 my $filename = $self->next_filename();
126 2 my $FH = $self->current_filehandle;
127
128 2 ▶ if (!defined $filename){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $filename) { } 1 1 2
elsif ($filename eq '-') { } 0 1 1
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
BranchTrueFalseTotal
$filename =~ /[.]gz$/ ? : 0 1 1
137 1 $mode .= $self->encoding;
138 1 ▶ open $FH, $mode, $filename or log_fatal "Can't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
&Treex::Core::Files::open($FH, $mode, $filename) or log_fatal("Can't open ${filename}: $!") 0 1 1
139 }
140 2 $self->_set_current_filehandle($FH);
141 2 return $FH;
142 }
143  
144 sub next_file_text {
145 2 ▶ my ($self) = @_;
Subroutine next_file_text: called CC=2 SCAR=6.9
Pod documented
146 2 ▶ my $FH = $self->next_filehandle() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $FH = $self->next_filehandle or return 1 1 2
147  
148 # Slurp that is compatible with Perl::IO::via::gzip.
149 1 local $/ = undef;
150 1 return <$FH>;
151 }
152  
153 sub next_line {
154 0 ▶ my ($self) = @_;
Subroutine next_line: not called CC=5 SCAR=34.0
Pod documented
155 0 my $FH = $self->current_filehandle;
156 0 ▶ return if !$FH && !$self->join_files_for_next_line;
Branch: 1 branch
BranchTrueFalseTotal
unless $FH or $self->join_files_for_next_line 0 0 0
Condition0%0/3
$FH or $self->join_files_for_next_line
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
157 0 ▶ if ( !$FH ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($FH) 0 0 0
158 0 ▶ $FH = $self->next_filehandle() or return;
Branch: 1 branch
BranchTrueFalseTotal
$FH = $self->next_filehandle or return 0 0 0
159 }
160 0 return <$FH>;
161 }
162  
163 #<<<
164 73 ▶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 73 1;
174  
175 __END__
176  
177 =pod
178  
179 =encoding utf-8
180  
181 =head1 NAME
182  
183 Treex::Core::Files - helper class for iterating over filenames
184  
185 =head1 VERSION
186  
187 version 2.20151210
188  
189 =head1 SYNOPSIS
190  
191 package My::Class;
192 use Moose;
193  
194 has from => (
195 is => 'ro',
196 isa => 'Treex::Core::Files',
197 coerce => 1,
198 handles => [qw(next_filename current_filename)],
199 );
200  
201 # and then
202 my $c = My::Class(from=>'f1.txt f2.txt.gz @my.filelist');
203  
204 while (defined (my $filename = $c->next_filename)){ ... }
205 #or
206 while (my $filehandle = $c->next_filehandle){ ... }
207  
208 # You can use also wildcard expansion
209 my $c = My::Class(from=>'!dir??/file*.txt');
210  
211  
212 =head1 DESCRIPTION
213  
214 The I<@filelist> and I<!wildcard> conventions are used in several tools, e.g. 7z or javac.
215 For a large number of files, list the file names in a file - one per line.
216 Then use the list file name preceded by an @ character.
217  
218 Methods <next_*> serve as iterators and return undef if the called after the last file is reached.
219  
220 =head1 METHODS
221  
222 =head2 number_of_files
223  
224 Returns the total number of files contained by this instance.
225  
226 =head2 file_number
227  
228 Returns ordinal number (1..number_of_files) of the current file.
229  
230 =head2 current_filename
231  
232 Returns the current filename or undef if the iterator is before the first file
233 (i.e. C<next_filename> has not been called so far) or after the last file.
234  
235 =head2 next_filename
236  
237 Returns the next filename (and increments the file_number).
238  
239 =head2 current_filehandle
240  
241 Opens the current file for reading and returns the filehandle.
242 Filename "-" is interpreted as STDIN.
243 Filenames with extension ".gz" are opened via L<PerlIO::via::gzip> (ie. unzipped on the fly).
244  
245 =head2 next_filehandle
246  
247 Returns the next filehandle (and increments the file_number).
248  
249 =head2 next_file_text
250  
251 Returns the content of the next file (slurp) and increments the file_number.
252  
253 =head2 next_line
254  
255 Returns the next line of the current file.
256 If the end of file is reached and attribute C<join_files_for_next_line> is set to true (which is by default),
257 the first line of next file is returned (and file_number incremented).
258  
259 =head2 get_hash
260  
261 Returns MD5 hash computed from the filenames and last modify times.
262  
263 =head2 $filenames_ref = string_to_filenames($string)
264  
265 Helper method that expands comma-or-space-separated list of filenames
266 and returns an array reference containing the filenames.
267 If the string starts with "!", it is interpreted as wildcards (see Perl L<glob>).
268 If a filename starts with "@" it is interpreted as a file list with one filename per line.
269  
270 =head1 AUTHOR
271  
272 Martin Popel <popel@ufal.mff.cuni.cz>
273  
274 =head1 COPYRIGHT AND LICENSE
275  
276 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
277  
278 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/Log.pm ↑ Summary Treex/Core/Bundle.pm »