lib/Treex/Block/Write/BaseWriter.pm

filter: Statementstmt 66.6% 58 / 87 Branchbran 41.1% 14 / 34 Conditioncond 38.8% 7 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 55.5% 90 / 162 CC 24 SCAR 23.3
CC
24
Cov
57%
_get_filename
16.7
_prepare_file_handle
15.6
_open_file_handle
11.2
CRAP 10.3
« lib/Treex/Core/Node/P.pm ↑ Summary lib/Treex/Core/Node.pm »
1 package Treex::Block::Write::BaseWriter;
2 {
3 7 $Treex::Block::Write::BaseWriter::VERSION = '0.08633_1';
4 }
5  
6 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use autodie; # die if the output file cannot be opened
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 extends 'Treex::Core::Block';
13  
14 7 has extension => (
15 isa => 'Str',
16 is => 'ro',
17 documentation => 'Default extension for the output file type.',
18 default => ''
19 );
20  
21 7 has compress => (
22 is => 'rw',
23 isa => 'Bool',
24 documentation => 'Compression to .gz. Defaults to document->compress, or 0.'
25 );
26  
27 7 has clobber => (
28 isa => 'Bool',
29 is => 'ro',
30 default => 0,
31 documentation => 'Allow overwriting output files.',
32 );
33  
34 7 has [qw(file_stem path)] => (
35 isa => 'Str',
36 is => 'ro',
37 documentation => 'These provide a possibility of creating output file names from input file names.',
38 );
39  
40 7 has stem_suffix => (
41 isa => 'Str',
42 is => 'ro',
43 documentation => 'A suffix to append after file_stem.',
44 );
45  
46 7 has to => (
47 isa => 'Str',
48 is => 'ro',
49 documentation => 'The destination filename (default is "-" meaning standard output; '
50 . 'use "." for the filename inherited from upstream blocks).',
51 );
52  
53 7 has _filenames => (
54 isa => 'ArrayRef[Str]',
55 is => 'rw',
56 builder => '_build_filenames',
57 writer => '_set_filenames',
58 lazy_build => 1,
59 documentation => 'Array of filenames where to save the documents if using multiple files;'
60 . ' automatically initialized from the attribute "to".',
61 );
62  
63 7 has _file_handle => (
64 isa => 'Maybe[FileHandle]',
65 is => 'rw',
66 writer => '_set_file_handle',
67 documentation => 'The open output file handle.',
68 );
69  
70 7 has _last_filename => (
71 isa => 'Str',
72 is => 'rw',
73 writer => '_set_last_filename',
74 documentation => 'Last output filename, to keep stream open if unchanged.',
75 );
76  
77 sub _build_filenames {
78 0 ▶ my $self = shift;
Subroutine _build_filenames: not called CC=2 SCAR=17.9
79 0 ▶ log_fatal "Parameter 'to' must be defined!" if !defined $self->to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->to 0 0 0
80 0 return [ split /[ ,]+/, $self->to ];
81 }
82  
83 # Return 1 if the document should be compressed (and '.gz' added to its extension).
84 sub _compress_document {
85  
86 2 ▶ my ( $self, $document ) = @_;
Subroutine _compress_document: called CC=3 SCAR=12.7
87 2 my $compress = 0;
88  
89 2 ▶ if ( defined $self->compress ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->compress) { } 2 0 2
elsif (defined $document->compress) { } 0 0 0
90 2 $compress = $self->compress;
91 }
92 elsif ( defined $document->compress ) {
93 0 $compress = $document->compress;
94 }
95  
96 2 return $compress;
97 }
98  
99 # Return the correct extension for the given document, according to the default file extension
100 # for the format and the compression settings.
101 sub _document_extension {
102 2 ▶ my ( $self, $document ) = @_;
Subroutine _document_extension: called CC=2 SCAR=6.9
103 2 ▶ return $self->extension . ( $self->_compress_document($document) ? '.gz' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->_compress_document($document) ? : 1 1 2
104 }
105  
106 # This just returns the next filename from the list given in the 'to' parameter.
107 sub _get_next_filename {
108
109 0 ▶ my ($self) = @_;
Subroutine _get_next_filename: not called CC=1 SCAR=6.9
110
111 0 my ( $next_filename, @rest_filenames ) = @{ $self->_filenames };
112 0 $self->_set_filenames( \@rest_filenames );
113 0 return $next_filename;
114 }
115  
116  
117 # This returns the correct file name for the next document, taking the path, file_stem,
118 # stem_suffix and to parameters into account.
119 sub _get_filename {
120  
121 2 ▶ my ( $self, $document ) = @_;
Subroutine _get_filename: called CC=9 SCAR=28.2
122 2 my $filename = $document->full_filename . $self->_document_extension($document);
123  
124 2 ▶ if ( defined $self->path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path) 0 2 2
125 0 $document->set_path( $self->path );
126 0 $filename = $document->full_filename . $self->_document_extension($document);
127 }
128 2 ▶ if ( defined $self->file_stem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->file_stem) 0 2 2
129 0 $document->set_file_stem( $self->file_stem );
130 0 $filename = $document->full_filename . $self->_document_extension($document);
131 }
132 2 ▶ if ( defined $self->stem_suffix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->stem_suffix) 0 2 2
133 0 ▶ my $origstem = defined $self->file_stem
Branch: 1 branch
BranchTrueFalseTotal
defined $self->file_stem ? : 0 0 0
134 ? $self->file_stem : $document->file_stem;
135 0 $document->set_file_stem( $origstem . $self->stem_suffix );
136 0 $filename = $document->full_filename . $self->_document_extension($document);
137 }
138  
139 2 ▶ if ( defined( $self->to ) && ( $self->to ne '.' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->to and $self->to ne ".") 1 1 2
Condition66%2/3
defined $self->to and $self->to ne "."
!ll&&!rl&&r
101
MC/DC50%1/2
defined $self->to and $self->to ne "."
defined $self->to $self->to ne "."
Truth table66%2/3
A and B
A= defined $self->toB= $self->to ne "."
ABresult
0X0
100
111
140  
141 1 my $next_filename = $self->_get_next_filename();
142  
143 1 ▶ if ( !defined $next_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next_filename) { } 0 1 1
144 0 log_warn "There are more documents to save than filenames given ("
145 . $self->to . "). Falling back to the filename filled in by a DocumentReader ($filename).";
146 }
147 else {
148 1 ▶ $filename = ( defined $self->path ? $self->path : '' ) . $next_filename;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->path ? : 0 1 1
149 }
150 }
151  
152 2 return $filename;
153 }
154  
155 # Default process_document method for all Writer blocks.
156 override 'process_document' => sub {
157  
158 0 ▶ my ( $self, $document ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
159  
160 # set _file_handle properly (this MUST be called if process_document is overridden)
161 0 $self->_prepare_file_handle($document);
162  
163 # call the original process_document with _file_handle set
164 0 super();
165 0 return;
166 7 };
167  
168 # Prepare the file handle for the next file to be processed.
169 # This MUST be called in all process_document overrides.
170 sub _prepare_file_handle {
171  
172 2 ▶ my ( $self, $document ) = @_;
Subroutine _prepare_file_handle: called CC=9 SCAR=27.5
173  
174 2 my $filename = $self->_get_filename($document);
175  
176 2 ▶ if ( defined $self->_last_filename && $filename eq $self->_last_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_filename and $filename eq $self->_last_filename) { } 0 2 2
Condition33%1/3
defined $self->_last_filename and $filename eq $self->_last_filename
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->_last_filename and $filename eq $self->_last_filename
defined $self->_last_filename $filename eq $self->_last_filename
Truth table33%1/3
A and B
A= defined $self->_last_filenameB= $filename eq $self->_last_filename
ABresult
0X0
100
111
177  
178 # nothing to do, keep writing to the old filename
179 }
180 else {
181  
182 # need to switch output stream
183  
184 # close the previous one (except if it's stdout)
185 2 ▶ close $self->_file_handle
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_file_handle and !defined($self->_last_filename) || $self->_last_filename ne "-" 0 2 2
Condition16%1/6
defined $self->_file_handle and !defined($self->_last_filename) || $self->_last_filename ne "-"
!ll&&!rl&&r
200
not defined $self->_last_filename or $self->_last_filename ne "-"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->_file_handle and !defined($self->_last_filename) || $self->_last_filename ne "-"
defined $self->_file_handle not defined $self->_last_filename $self->_last_filename ne "-"
Truth table25%1/4
A and not(B) or C
A= defined $self->_file_handleB= not defined $self->_last_filenameC= $self->_last_filename ne "-"
ABCresult
0XX0
1000
11X1
1011
186 if defined $self->_file_handle
187 && ( !defined $self->_last_filename || $self->_last_filename ne "-" );
188  
189 # open the new output stream
190 2 log_info "Saving to $filename";
191 2 ▶ log_fatal "Won't overwrite $filename (use clobber=1 to force)."
Branch: 1 branch
BranchTrueFalseTotal
if not $self->clobber and -e $filename and not $filename =~ m[^/dev/std] 0 2 2
Condition50%3/6
not $self->clobber and -e $filename and not $filename =~ m[^/dev/std]
!ll&&!rl&&r
200
not $self->clobber and -e $filename
!ll&&!rl&&r
110
MC/DC0%0/3
not $self->clobber and -e $filename and not $filename =~ m[^/dev/std]
not $self->clobber -e $filename not $filename =~ m[^/dev/std]
Truth table50%2/4
not(A) and B and not(C)
A= not $self->clobberB= -e $filenameC= not $filename =~ m[^/dev/std]
ABCresult
0XX0
10X0
1100
1111
192 if ( !$self->clobber && -e $filename && $filename !~ m/^\/dev\/std/ );
193 2 $self->_set_file_handle( $self->_open_file_handle($filename) );
194 }
195  
196 # remember last used filename
197 2 $self->_set_last_filename($filename);
198 2 return;
199 }
200  
201 # Open the given file handle (including compressed variants and standard output).
202 sub _open_file_handle {
203  
204 1 ▶ my ( $self, $filename ) = @_;
Subroutine _open_file_handle: called CC=4 SCAR=24.1
205  
206 1 ▶ if ( $filename eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq "-") 1 0 1
207 1 return \*STDOUT;
208 }
209  
210 0 my $opn;
211 my $hdl;
212  
213 # file might not recognize some files!
214 0 ▶ if ( $filename =~ /\.gz$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /\.gz$/) { } 0 0 0
elsif ($filename =~ /\.bz2$/) { } 0 0 0
215 0 $opn = "| gzip -c > '$filename'";
216 }
217 elsif ( $filename =~ /\.bz2$/ ) {
218 0 $opn = "| bzip2 > '$filename'";
219 }
220 else {
221 0 $opn = ">$filename";
222 }
223 0 mkpath( dirname($filename) );
224 0 open $hdl, $opn; # we use autodie here
225 0 return $hdl;
226 }
227  
228 7 1;
229  
230 __END__
231  
232 =encoding utf-8
233  
234 =head1 NAME
235  
236 Treex::Block::Write::BaseWriter
237  
238 =head1 VERSION
239  
240 version 0.08633_1
241  
242 =head1 DESCRIPTION
243  
244 This is the base class for document writer blocks in Treex.
245  
246 It handles selecting and opening the output files, allowing for output of one-file per document.
247 The output file name(s) may be set in several ways (standard output may also be used as a file
248 with the name '-'); GZip file compression is supported.
249  
250 Other features, such as writing all documents to one file or setting character encoding,
251 are enabled in L<Treex::Block::Write::BaseTextWriter>.
252  
253 =head1 PARAMETERS
254  
255 =over
256  
257 =item C<to>
258  
259 Space-or-comma-separated list of output file names.
260  
261 =item C<file_stem>, C<path>
262  
263 These override the respective attributes in documents
264 (filled in by a L<DocumentReader|Treex::Core::DocumentReader>),
265 which are used for generating output file names.
266  
267 =item C<stem_suffix>
268  
269 A string to append after C<file_stem>.
270  
271 =item C<compress>
272  
273 If set to 1, the output files are compressed using GZip (if C<to> is used to set
274 file names, the names must also contain the ".gz" suffix).
275  
276 =item C<clobber>
277  
278 If set to 1, existing destination files will be overwritten.
279  
280 =back
281  
282 =head1 DERIVED CLASSES
283  
284 The derived classes should just use C<print { $self->_file_handle } "output text">, the
285 base class will take care of opening the proper file.
286  
287 All derived classes that override the C<process_document> method directly must call
288 the C<_prepare_file_handle> method to gain access to the correct file handle.
289  
290 The C<extension> parameter should be overriden with the default file extension
291 for the given file type.
292  
293 =head1 TODO
294  
295 =over
296  
297 =item *
298  
299 Set C<compress> if file name contains .gz or .bz2? Add .gz to extension to even for file names set with
300 the C<to> parameter if C<compress> is set to true?
301  
302 =item *
303  
304 Possibly rearrange somehow so that the C<_prepare_file_handle> method is not needed. The problem is that
305 if this was a Moose role, it would have to be applied only after an override to C<process_document>. The
306 Moose C<inner> and C<augment> operators are a possibility, but would not remove a need for a somewhat
307 non-standard behavior in derived classes (one could not just override C<process_document>, but would
308 have to C<augment> it).
309  
310 =back
311
312 =head1 AUTHORS
313  
314 Ondřej Dušek <odusek@ufal.mff.cuni.cz>
315  
316 Martin Popel <popel@ufal.mff.cuni.cz>
317  
318 Ondřej Bojar <bojar@ufal.mff.cuni.cz>
319  
320 =head1 COPYRIGHT AND LICENSE
321  
322 Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
323  
324 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/Node/P.pm ↑ Summary lib/Treex/Core/Node.pm »