Hierarchical/Dump.pm

filter: Statementstmt 100% 167 / 167 Branchbran 100% 90 / 90 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 14 / 14 Podpod 10.0% 1 / 10 totaltotal 96.9% 281 / 290 CC 56 SCAR 21.4
CC
56
Cov
100%
parse_options
15.0
do_all
11.0
make_format
10.0
CRAP 8.5
« Hierarchical.pm ↑ Summary Hierarchical/DumpTest.pm »
1 # Config::General::Hierarchical::Dump.pm - Hierarchical Generic Config Dumper Module
2  
3 package Config::General::Hierarchical::Dump;
4  
5 1 $Config::General::Hierarchical::Dump::VERSION = 0.07;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Config::General::Hierarchical;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 26 ▶sub deep_dump {
Subroutine deep_dump: called CC=5 SCAR=16.1
Pod undocumented
13 26 my ( $names, $cfg, $errors ) = @_;
14  
15 26 my @return;
16  
17 26 foreach my $key ( sort keys %{ $cfg->value } ) {
18 70 my $file = $cfg->value->{$key}->file;
19 70 my $name = join( '->', @$names, $key );
20 70 my $value = eval { $cfg->get($key); };
21  
22 70 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 9 61 70
elsif (defined $value) { } 57 4 61
23 9 my @tmp = ( $name, 'error;', $file );
24  
25 9 push @return, \@tmp;
26 9 push @$errors, \@tmp;
27 }
28 elsif ( defined $value ) {
29 57 ▶ if ( eval { $value->isa('Config::General::Hierarchical') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $value->isa('Config::General::Hierarchical') } }) { } 12 45 57
30 12 push @$names, $key;
31 12 push @return, deep_dump( $names, $value, $errors );
32 12 pop @$names;
33 }
34 else {
35 45 push @return, translate_value( $name, $value, $file );
36 }
37 }
38 else {
39 4 push @return, [ $name, 'undef;', $file ];
40 }
41 }
42  
43 26 return @return;
44 }
45  
46 23 ▶sub do_all {
Subroutine do_all: called CC=11 SCAR=24.0
Pod undocumented
47 23 my ( $class, $file_name, $params_array, $parser_class ) = @_;
48  
49 23 ▶ return () unless $file_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_name 1 22 23
50  
51 22 my ( $check, $file, $fixed_length, $help, $json );
52 22 my ( $sfile, $stfile );
53 22 my $error = '';
54  
55 22 parse_options( $params_array, \$error, \$check, \$file, \$fixed_length,
56 \$help, \$json );
57  
58 22 ▶ return <<EOF if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 5 17 22
59 $error Usage: $0
60 Dumps the Config::General::Hierarchical configuration file itself
61  
62 -c, --check if present, prints only the variables that do
63 not respect syntax constraint
64 -f, --file shows in which file variables are defined
65 -l, --fixed-length formats output as fixed length fields
66 -h, --help prints this help and exits
67 -j, --json prints output as json
68 EOF
69  
70 17 ▶ if ($parser_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser_class) { } 1 16 17
71 1 eval "require $parser_class";
72 }
73 else {
74 16 $parser_class = $class->parser;
75 }
76  
77 17 my ( $cfg, @errors );
78  
79 17 eval { $cfg = $parser_class->new( file => $file_name ); };
80  
81 17 ▶ return "Parsing error: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 16 17
82  
83 16 ▶ return json_dump($cfg) if $json;
Branch: 1 branch
BranchTrueFalseTotal
if $json 2 14 16
84  
85 14 my @output = deep_dump( [], $cfg, \@errors );
86 14 ▶ my $output = ( $check && scalar @errors ) ? \@errors : \@output;
Branch: 1 branch
BranchTrueFalseTotal
$check && scalar @errors ? : 2 12 14
Condition100%2/2
$check and scalar @errors
!ll
122
MC/DC100%1/1
$check and scalar @errors
$check
Truth table100%2/2
A
A= $check
Aresult
00
11
87 14 my $format = make_format( $fixed_length, $file, $output );
88 14 my $base_dir = find_base_dir( $cfg->opt->files );
89  
90 14 my @files;
91 my @return;
92  
93 14 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 5 9 14
94 5 my $base_dir_len = 1 + length $base_dir;
95  
96 5 push @return, "Configuration files base dir: $base_dir/\n";
97  
98 5 @files = map substr( $_, $base_dir_len ), @{ $cfg->opt->files };
99  
100 5 ▶ if ( scalar @files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @files > 1) 1 4 5
101  
102 1 push @return, "Files inheritance structure:\n";
103 1 push @return, dump_struct( $cfg->opt->struct->{0}, \@files );
104 }
105 }
106  
107 14 ▶ push @return,
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 50 3 53
108 map( ref $_
109 ? sprintf( $format, $_->[0], $_->[1], $files[ $_->[2] ] )
110 : $_,
111 @$output );
112  
113 14 return @return;
114 }
115  
116 2 ▶sub dump_struct {
Subroutine dump_struct: called CC=3 SCAR=11.0
Pod undocumented
117 2 my ( $struct, $files, $key, $lvl ) = @_;
118  
119 2 ▶ $key ||= 0;
Condition100%2/2
$key ||= 0
l!l
11
MC/DC100%1/1
$key ||= 0
$key
Truth table100%2/2
A
A= $key
Aresult
11
00
120 2 ▶ $lvl ||= 1;
Condition100%2/2
$lvl ||= 1
l!l
11
MC/DC100%1/1
$lvl ||= 1
$lvl
Truth table100%2/2
A
A= $lvl
Aresult
11
00
121  
122 2 my @ret = ( ( ' ' x $lvl ) . $files->[$key] . "\n" );
123  
124 2 push @ret, map dump_struct( $struct->{$_}, $files, $_, $lvl + 1 ),
125 keys %$struct;
126  
127 2 return @ret;
128 }
129  
130 14 ▶sub find_base_dir {
Subroutine find_base_dir: called CC=4 SCAR=13.9
Pod undocumented
131 14 my ($files) = @_;
132  
133 14 my @mcp = split '/', $files->[0];
134 14 my $last = scalar @$files;
135  
136 14 pop @mcp;
137  
138 14 ▶ for ( my $i = 1 ; $i < $last ; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last) 4 14 18
139 4 my @this = split '/', $files->[$i];
140  
141 4 ▶ for ( my $j = 0 ; $j < scalar @mcp ; ++$j ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @mcp) 32 4 36
142 32 ▶ if ( $mcp[$j] ne $this[$j] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mcp[$j] ne $this[$j]) 4 28 32
143 4 splice @mcp, $j;
144 }
145 }
146 }
147  
148 14 return join '/', @mcp;
149 }
150  
151 2 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
152 2 my ( $class, @pars ) = @_;
153  
154 2 ▶ return if caller ne 'main';
Branch: 1 branch
BranchTrueFalseTotal
if caller ne "main" 1 1 2
155  
156 1 print join '', $class->do_all( $0, \@ARGV, $pars[0] );
157  
158 1 exit;
159 }
160  
161 4 ▶sub json_dump {
Subroutine json_dump: called CC=5 SCAR=16.1
Pod undocumented
162 4 my ($cfg) = @_;
163  
164 4 my $return = '{';
165  
166 4 foreach my $key ( sort keys %{ $cfg->value } ) {
167 14 $return .= "\"$key\":";
168  
169 14 my $value = eval { $cfg->get($key); };
170  
171 14 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 4 10 14
elsif (defined $value) { } 8 2 10
172 4 $return .= '"error",';
173 }
174 elsif ( defined $value ) {
175 8 ▶ if ( eval { $value->isa('Config::General::Hierarchical') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $value->isa('Config::General::Hierarchical') } }) { } 2 6 8
176 2 $return .= json_dump($value) . ',';
177 }
178 else {
179 6 $return .= translate_json($value);
180 }
181 }
182 else {
183 2 $return .= 'null,';
184 }
185 }
186  
187 4 chop $return;
188  
189 4 return $return . '}';
190 }
191  
192 14 ▶sub make_format {
Subroutine make_format: called CC=10 SCAR=23.0
Pod undocumented
193 14 my ( $fixed_length, $file, $output ) = @_;
194  
195 14 my $format = "\%s = \%s\n";
196  
197 14 ▶ if ($fixed_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixed_length) { } 7 7 14
198 7 my $maxlen = 0;
199 7 my $len;
200  
201 7 foreach (@$output) {
202 31 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 3 28 31
203  
204 28 $len = length $_->[0];
205  
206 28 ▶ $maxlen = $len if $len > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $maxlen 17 11 28
207 }
208  
209 7 $format = '%-' . $maxlen . "s = %";
210 7 $maxlen = 0;
211  
212 7 foreach (@$output) {
213 31 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 3 28 31
214  
215 28 $len = length $_->[1];
216  
217 28 ▶ $maxlen = $len if $len > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $maxlen 13 15 28
218 }
219  
220 7 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 3 4 7
221 3 $format .= '-' . $maxlen . "s \%s\n";
222 }
223 else {
224 4 $format .= "s\n";
225 }
226 }
227 else {
228 7 ▶ $format = "\%s = \%s \%s\n" if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 2 5 7
229 }
230  
231 14 return $format;
232 }
233  
234 22 ▶sub parse_options {
Subroutine parse_options: called CC=15 SCAR=27.1
Pod undocumented
235 22 my ( $params_array, $error, $check, $file, $fixed_length, $help, $json ) =
236 @_;
237  
238 22 foreach my $param (@$params_array) {
239 19 ▶ if ( substr( $param, 0, 1 ) ne '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($param, 0, 1) ne "-") 1 18 19
240 1 $$help = 1;
241 1 $$error = "Unknown options '$param'\n\n";
242 1 return;
243 }
244  
245 18 ▶ if ( substr( $param, 0, 2 ) eq '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($param, 0, 2) eq '--') { } 7 11 18
246 7 ▶ if ( $param eq '--check' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($param eq '--check') { } 1 6 7
elsif ($param eq '--file') { } 2 4 6
elsif ($param eq '--fixed-length') { } 1 3 4
elsif ($param eq '--help') { } 1 2 3
elsif ($param eq '--json') { } 1 1 2
247 1 $$check = 1;
248 }
249 elsif ( $param eq '--file' ) {
250 2 $$file = 1;
251 }
252 elsif ( $param eq '--fixed-length' ) {
253 1 $$fixed_length = 1;
254 }
255 elsif ( $param eq '--help' ) {
256 1 $$help = 1;
257 }
258 elsif ( $param eq '--json' ) {
259 1 $$json = 1;
260 }
261 else {
262 1 $$help = 1;
263 1 $$error = "Unknown options '$param'\n\n";
264 1 return;
265 }
266 }
267 else {
268 11 ▶ for ( my $i = 1 ; $i < length $param ; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $param) 14 10 24
269 14 my $p = substr $param, $i, 1;
270  
271 14 ▶ if ( $p eq 'c' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($p eq 'c') { } 2 12 14
elsif ($p eq 'f') { } 3 9 12
elsif ($p eq 'h') { } 1 8 9
elsif ($p eq 'j') { } 1 7 8
elsif ($p eq 'l') { } 6 1 7
272 2 $$check = 1;
273 }
274 elsif ( $p eq 'f' ) {
275 3 $$file = 1;
276 }
277 elsif ( $p eq 'h' ) {
278 1 $$help = 1;
279 }
280 elsif ( $p eq 'j' ) {
281 1 $$json = 1;
282 }
283 elsif ( $p eq 'l' ) {
284 6 $$fixed_length = 1;
285 }
286 else {
287 1 $$help = 1;
288 1 $$error = "Unknown options '-$p'\n\n";
289 1 return;
290 }
291 }
292 }
293 }
294 }
295  
296 9 ▶sub parser { return 'Config::General::Hierarchical'; }
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
297  
298 6 ▶sub translate_json {
Subroutine translate_json: called CC=3 SCAR=11.0
Pod undocumented
299 6 my ($value) = @_;
300  
301 6 ▶ unless ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 2 4 6
302 2 $value =~ s/\n/\\n/g;
303  
304 2 return "\"$value\",";
305 }
306  
307 4 my $ret = '[';
308  
309 4 foreach my $val (@$value) {
310 8 $val =~ s/\n/\\n/g;
311 8 $ret .= "\"$val\",";
312 }
313  
314 4 chop $ret;
315  
316 4 return $ret . '],';
317 }
318  
319 45 ▶sub translate_value {
Subroutine translate_value: called CC=7 SCAR=19.5
Pod undocumented
320 45 my ( $name, $value, $file ) = @_;
321  
322 45 ▶ unless ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 36 9 45
323 36 ▶ return [ $name, "'$value';", $file ] if $value !~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\n/ 34 2 36
324  
325 2 my $return = [ $name, '<<EOF;', $file ];
326  
327 2 ▶ return ( $return, $value . "EOF\n" ) if $value =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\n$/ 1 1 2
328  
329 1 return ( $return, $value . "//--new line added\nEOF\n" );
330 }
331  
332 9 my @ret;
333 9 my $simple = 1;
334  
335 9 foreach my $val (@$value) {
336 18 ▶ $simple = 0 if $val =~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\n/ 2 16 18
337 }
338  
339 9 ▶ return [ $name, "( '" . join( "', '", @$value ) . "' );", $file ]
Branch: 1 branch
BranchTrueFalseTotal
if $simple 8 1 9
340 if $simple;
341  
342 1 return ( [ $name, '*;', $file ],
343 "* = ( '" . join( "', '", @$value ) . "' );\n" );
344 }
345  
346 1 1;
347  
348 __END__
« Hierarchical.pm ↑ Summary Hierarchical/DumpTest.pm »