ETLp/File/Config.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 111 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::File::Config;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::File::Config - Parse the file confgiuration defintions
8  
9 =head1 SYNOPSIS
10  
11 use ETLp::File::Config;
12  
13 my $file_conf = ETLp::File::Config->new(
14 directory => "$Bin/../conf",
15 definition => 'file_def.cfg',
16 );
17
18 # Get the fields from the config file:
19 my @fields = @{$file_conf->fields};
20
21 ETLp::File::Config parses data file definitions. Defintions are specified in
22 files and consist of the following components separated by whitespace:
23  
24 * field name - the name of the field
25 * nullable - whether the field is allows nulls (Y or N)
26 * validation rules:
27
28 o A regex pattern, specified as qr/<regex>/
29 o Varchar(n). String up to n characters.
30 o Integer. A positive or negative integer.
31 o Real. A real number.
32 o Date(<POSIX PATTERN>). A valid date with the format specified
33 as a POSIX pattern
34 o range(n, m). A numeric range from n to m. If n is not specified
35 there is no lower limit. If m is not specified, there is no
36 upper limit
37
38 Multiple validation rules can be specified per field - simply separate
39 with a semi-colon.
40  
41 Example Configuration entries
42  
43 custname N varchar(20)
44 cost Y integer
45 phone Y qr/^\d{3}-\d{4}$/
46 city N qr/^(Auckland|Wellington)$/
47 rec_date N date(%Y-%n-%d %H:%M%S)
48 period N range(1,50); integer
49
50 Individual rules can be enclosed in double-quotes, which will be
51 required if any indvidual rule contains a semi-colon:
52  
53 contrived_field N "qr/\d;\s{3}/"
54
55 Comments can be specified with a hash
56  
57 # This is a line comment
58 custname N varchar(20) # And this is a field comment
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 Create a Config object.
65  
66 Parameters
67  
68 Hash or hashref consisting of
69  
70 * directory: Optional. The directory where the
71 file_defintion file can be found.
72 * definition: Required. A file that contains a defintion
73 of a file.
74  
75 Returns
76  
77 * a ETLp::File::Validate object
78
79 =cut
80  
81 class ETLp::File::Config with ETLp::Role::Config {
82 use Text::CSV;
83 use ETLp::Exception;
84 use Data::Dumper;
85
86 has 'directory' => (is => 'ro', isa => 'Str', required => 0);
87 has 'definition' => (is => 'ro', isa => 'Str', required => 1);
88 has 'fields' => (is => 'rw', isa => 'ArrayRef');
89 has 'rules' => (is => 'rw', isa => 'HashRef');
90
91 # Makes sure that the rule is actually valid.
92 method _check_rule(Str $rule) {
93 ETLpException->throw(error => "unknown rule $rule")
94 unless (($rule =~ /^qr\/.*\/$/)
95 or ($rule =~ /^varchar\((\d+)\)$/i)
96 or (lc $rule eq 'integer')
97 or ($rule =~ /date\(\s*'?.*\'?\s*\)/i)
98 or (lc $rule eq 'float')
99 or ($rule =~ /^range\(\s*\d*\s*,\s*\d*\s*\)$/i));
100 }
101
102 # parses the confguration file
103 method _parse_config(Str $definition) {
104 my (@fields, %fields);
105
106 open(my $fh, '<', $definition) ||
107 ETLpException->throw(error => "Unable to open $definition: $!");
108
109 my $csv = Text::CSV->new({sep_char => ';', allow_whitespace => 1});
110
111 while (my $line = <$fh>) {
112 my $parsed_line = $line;
113 $parsed_line =~ s/#.*$//;
114 next if $parsed_line =~ /^$/;
115
116 my $parsed_rule;
117 my ($field, $nullable, $rule) =
118 ($parsed_line =~ /^\s*(\S+)\s+(\S+)\s+(\S.*?)\s*$/);
119
120 unless ($field && $nullable && $rule) {
121 ETLpException->throw(error => "Config file must provide "
122 . "the field name, nullable flag and validation rule.\n"
123 . "line: $line");
124 }
125
126 # Check if multiple rules have been specified for a field
127 # Use Text::CSV to deconstruct in case there are quotes,
128 # embedded semi-colons or any other nasties
129 if ($rule =~ /;/) {
130 my $status = $csv->parse($rule);
131 unless ($status) {
132 ETLpException->throw(error =>
133 sprintf("error: %s\nvalue: %s\nline: %s",
134 "" . $csv->error_diag(), $rule, $line));
135 }
136 my @rules = $csv->fields;
137 $rule = \@rules if (@rules > 1);
138 }
139
140 if (ref $rule eq 'ARRAY') {
141 $self->_check_rule($_) foreach @$rule;
142 } else {
143 $self->_check_rule($rule);
144 }
145
146 if ($nullable !~ /^[YyNn]$/) {
147 ETLpException->throw(error =>
148 sprintf("Nullable must be Y or N\nvalue: %s\nline: %s",
149 $nullable, $line));
150 }
151
152 push @fields, $field;
153
154 $fields{$field} = {
155 nullable => uc $nullable,
156 rule => $rule,
157 };
158 }
159
160 $self->logger->debug("Rules: " . Dumper(\%fields));
161
162 $self->fields(\@fields);
163 $self->rules(\%fields);
164 }
165
166 # Automatically called after "new"
167 method BUILD {
168 my $definition = $self->definition;
169
170 if ($self->directory) {
171 $definition = $self->directory . "/$definition";
172 }
173
174 $self->_parse_config($definition);
175 }
176
177 }
178  
179 =head1 LICENSE AND COPYRIGHT
180  
181 Copyright 2010 Redbone Systems Ltd
182  
183 This program is free software; you can redistribute it and/or modify it
184 under the terms of either: the GNU General Public License as published
185 by the Free Software Foundation; or the Artistic License.
186  
187 The terms are in the LICENSE file that accompanies this application
188
189 =cut
190