| 1 |
# This class represents an possible output format for analizo operations. It |
| 2 |
# has the responsibility to generate an output based on a series of jobs, and to inform |
| 3 |
# |
| 4 |
package Analizo::Batch::Output; |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use parent qw(Class::Accessor::Fast); |
| 10 |
|
| 11 |
sub new { |
| 12 |
my ($class) = @_; |
| 13 |
return bless {}, $class; |
| 14 |
} |
| 15 |
|
| 16 |
# This method must be overriden by subclasses, and must return 0 or 1 based on |
| 17 |
# whether the given output format requires calculation of metrics or not. |
| 18 |
# |
| 19 |
# Given that calculating metrics requires a significant amount of processing, |
| 20 |
# by default we return 0 here. |
| 21 |
sub requires_metrics { |
| 22 |
0; |
| 23 |
} |
| 24 |
|
| 25 |
# Runners must use this method to push jobs into the output object. |
| 26 |
# |
| 27 |
# Subclasses (output drives) must override this method. Output drives MAY NOT |
| 28 |
# ASSUME that the jobs will be pushed in any particular order. |
| 29 |
sub push { |
| 30 |
} |
| 31 |
|
| 32 |
# To which file the output must be written |
| 33 |
__PACKAGE__->mk_accessors(qw(file)); |
| 34 |
|
| 35 |
# Makes any initialization needed for the given output driver. |
| 36 |
# |
| 37 |
# This method must be overriden by subclasses |
| 38 |
sub initialize { |
| 39 |
} |
| 40 |
|
| 41 |
# Opens a file for output, delegates the actual writing to subclasses, and |
| 42 |
# closes the file. If no file was given, write to standard output. |
| 43 |
sub flush { |
| 44 |
my ($self) = @_; |
| 45 |
if ($self->file) { |
| 46 |
open(my $fh, '>', $self->file) or die("Could not open output file $self->file: $!"); |
| 47 |
$self->write_data($fh); |
| 48 |
close($fh); |
| 49 |
} else { |
| 50 |
$self->write_data(*STDOUT); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
# Must be overriden by subclasses. Will receive a FILEHANDLE, and must write |
| 55 |
# the data to it. |
| 56 |
sub write_data { |
| 57 |
} |
| 58 |
|
| 59 |
1; |