| 1 | =pod | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | ETL::Pipeline::Input::File - Role for file based input sources | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # In the input source... | ||||||||||||||||||||||||||||
| 10 | use Moose; | ||||||||||||||||||||||||||||
| 11 | with 'ETL::Pipeline::Input::File'; | ||||||||||||||||||||||||||||
| 12 | ... | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # In the ETL::Pipeline script... | ||||||||||||||||||||||||||||
| 15 | ETL::Pipeline->new( { | ||||||||||||||||||||||||||||
| 16 | work_in => {search => 'C:\Data', find => qr/Ficticious/}, | ||||||||||||||||||||||||||||
| 17 | input => ['Excel', matching => qr/\.xlsx?$/ ], | ||||||||||||||||||||||||||||
| 18 | mapping => {Name => 'A', Address => 'B', ID => 'C' }, | ||||||||||||||||||||||||||||
| 19 | constants => {Type => 1, Information => 'Demographic' }, | ||||||||||||||||||||||||||||
| 20 | output => ['SQL', table => 'NewData' ], | ||||||||||||||||||||||||||||
| 21 | } )->process; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Or with a specific file... | ||||||||||||||||||||||||||||
| 24 | ETL::Pipeline->new( { | ||||||||||||||||||||||||||||
| 25 | work_in => {search => 'C:\Data', find => qr/Ficticious/}, | ||||||||||||||||||||||||||||
| 26 | input => ['Excel', file => 'ExportedData.xlsx' ], | ||||||||||||||||||||||||||||
| 27 | mapping => {Name => 'A', Address => 'B', ID => 'C' }, | ||||||||||||||||||||||||||||
| 28 | constants => {Type => 1, Information => 'Demographic' }, | ||||||||||||||||||||||||||||
| 29 | output => ['SQL', table => 'NewData' ], | ||||||||||||||||||||||||||||
| 30 | } )->process; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | B<ETL::Pipeline::Input::File> provides methods and attributes common to | ||||||||||||||||||||||||||||
| 35 | file based input sources. It makes file searches available for any file | ||||||||||||||||||||||||||||
| 36 | format. With B<ETL::Pipeline::Input::File>, you can... | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item Specify the exact path to the file. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item Or search the file system for a matching name. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =back | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | For setting an exact path, see the L</path> attribute. For searches, see the | ||||||||||||||||||||||||||||
| 47 | L</find> attribute. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 File vs. DataFile | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | L<ETL::Pipeline::Input::DataFile> extends B<ETL::Pipeline::Input::File>. | ||||||||||||||||||||||||||||
| 52 | This role, B<ETL::Pipeline::Input::File> makes no assumptions about the file | ||||||||||||||||||||||||||||
| 53 | format. It works CSV text files, MS Access databases, spread sheets, XML, or | ||||||||||||||||||||||||||||
| 54 | any other format found on disk. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | L<ETL::Pipeline::Input::DataFile> assumes that each record is stored on one | ||||||||||||||||||||||||||||
| 57 | row. And the data is divided into fields (columns). Basically, | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | package ETL::Pipeline::Input::File; | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use 5.014000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | use MooseX::Types::Path::Class qw/Dir File/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | use Path::Class::Rule; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | use String::Util qw/hascontent/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | our $VERSION = '2.00'; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 METHODS & ATTRIBUTES | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 Arguments for L<ETL::Pipeline/input> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head3 matching | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | B<matching> locates the first file that matches the given pattern. The | ||||||||||||||||||||||||||||
| 81 | pattern can be a glob or regular expression. B<matching> sets L</file> | ||||||||||||||||||||||||||||
| 82 | to the first file that matches. Search patterns are case insensitive. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Search using a regular expression... | ||||||||||||||||||||||||||||
| 85 | $etl->input( 'Excel', matching => qr/\.xlsx$/i ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Search using a file glob... | ||||||||||||||||||||||||||||
| 88 | $etl->input( 'Excel', matching => '*.xlsx' ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | For very weird cases, B<matching> also accepts a code reference. | ||||||||||||||||||||||||||||
| 91 | B<matching> executes the subroutine against the file names. B<matching> | ||||||||||||||||||||||||||||
| 92 | sets L</file> to the first file where the subroutine returns a true | ||||||||||||||||||||||||||||
| 93 | value. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | B<matching> passes two parameters into the subroutine... | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =over | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item The L<ETL::Pipeline> object | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item The L<Path::Class::File> object | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =back | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # File larger than 2K... | ||||||||||||||||||||||||||||
| 106 | $etl->input( 'Excel', matching => sub { | ||||||||||||||||||||||||||||
| 107 | my ($etl, $file) = @_; | ||||||||||||||||||||||||||||
| 108 | return (!$file->is_dir && $file->size > 2048 ? 1 : 0); | ||||||||||||||||||||||||||||
| 109 | } ); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | B<matching> searches inside the L<ETL::Pipeline/data_in> directory. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | has 'matching' => ( | |||||||||||||||||||||||||||
| 116 | is => 'ro', | ||||||||||||||||||||||||||||
| 117 | isa => 'Maybe[CodeRef|RegexpRef|Str]', | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head3 file | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | B<file> holds a L<Path::Class::File> object pointing to the input file. | ||||||||||||||||||||||||||||
| 124 | If L<ETL::Pipeline/input> does not set B<file>, then the L</matching> | ||||||||||||||||||||||||||||
| 125 | attribute searches the file system for a match. If | ||||||||||||||||||||||||||||
| 126 | L<ETL::Pipeline/input> sets B<file>, then L</matching> is ignored. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | B<file> is relative to L<ETL::Pipeline/data_in>, unless you set it to an | ||||||||||||||||||||||||||||
| 129 | absolute path name. With L</matching>, the search is always limited to | ||||||||||||||||||||||||||||
| 130 | L<ETL::Pipeline/data_in>. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # File inside of "data_in"... | ||||||||||||||||||||||||||||
| 133 | $etl->input( 'Excel', file => 'Data.xlsx' ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Absolute path name... | ||||||||||||||||||||||||||||
| 136 | $etl->input( 'Excel', file => 'C:\Data.xlsx' ); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | has 'file' => ( | |||||||||||||||||||||||||||
| 141 | builder => '_build_file', | ||||||||||||||||||||||||||||
| 142 | coerce => 1, | ||||||||||||||||||||||||||||
| 143 | is => 'ro', | ||||||||||||||||||||||||||||
| 144 | isa => File, | ||||||||||||||||||||||||||||
| 145 | lazy => 1, | ||||||||||||||||||||||||||||
| 146 | trigger => \&_trigger_file, | ||||||||||||||||||||||||||||
| 147 | writer => '_set_file', | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub _build_file { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_file: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | my $rule = Path::Class::Rule->new; | |||||||||||||||||||||||||||
| 155 | 0 | my $pattern = $self->matching; | |||||||||||||||||||||||||||
| 156 | 0 | my $pipeline = $self->pipeline; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if (ref( $pattern ) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | my $search = $rule->iter( $pipeline->data_in ); | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | while (my $file = $search->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | return $file if $pipeline->execute_code_ref( $pattern, $file ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | croak 'No file matched for "input"'; | |||||||||||||||||||||||||||
| 164 | 0 | return undef; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 0 | $rule->file; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | $rule->iname( $pattern ) if defined $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | my $search = $rule->iter( $pipeline->data_in ); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | my $file = $search->(); | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | croak 'No file matched for "input"' unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | return $file; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub _trigger_file { | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | my ($self, $old, $new) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trigger_file: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | $self->_set_file( $new->absolute( $self->pipeline->data_in ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $new and $new->is_relative
MC/DC0%0/2
defined $new and $new->is_relative
defined $new
$new->is_relative
Truth table0%0/3
A and B
A= defined $newB= $new->is_relative
| |||||||||||||||||||||||||||||
| 180 | if defined( $new ) && $new->is_relative; | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | L<ETL::Pipeline>, L<ETL::Pipeline::Input>, L<ETL::Pipeline::Input::TabularFile> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Robert Wohlfarth <robert.j.wohlfarth@vanderbilt.edu> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Copyright 2016 (c) Vanderbilt University Medical Center | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 197 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Required by Perl to load the module. | ||||||||||||||||||||||||||||
| 204 | 1 | 1; | |||||||||||||||||||||||||||