Yak.pm

filter: Statementstmt 74.1% 129 / 174 Branchbran 31.4% 17 / 54 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 82.3% 28 / 34 Podpod 100% 4 / 4 totaltotal 65.3% 181 / 277 CC 40 SCAR 26.1
CC
40
Cov
63%
_init_data
31.4
_load_config
28.5
__ANON__
12.0
CRAP 13.6
↑ Summary Yak/OrderedPlugins.pm »
1 package Config::Yak;
2 {
3 4 $Config::Yak::VERSION = '0.23';
4 }
5 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 $Config::Yak::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a tree-based versatile config handler
9  
10 4 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Data::Structure::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 subtype 'ArrayRefOfStr',
29 as 'ArrayRef[Str]';
30  
31 coerce 'ArrayRefOfStr',
32 from 'Str',
33 1 ▶ via { [ $_ ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 4 extends 'Data::Tree' => { -version => 0.16 };
36  
37 4 has 'locations' => (
38 'is' => 'rw',
39 'isa' => 'ArrayRefOfStr',
40 'coerce' => 1,
41 'required' => 1,
42 );
43  
44 4 has 'last_ts' => (
45 'is' => 'rw',
46 'isa' => 'Num',
47 'default' => 0,
48 );
49  
50 has 'files_read' => (
51 'is' => 'rw',
52 'isa' => 'ArrayRef[Str]',
53 3 ▶ 'default' => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 4 );
55  
56 3 ▶sub config {
Subroutine config: called CC=2 SCAR=6.9
Pod documented
57 3 my $self = shift;
58 3 my $arg = shift;
59  
60 3 ▶ if ( defined($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 1 2 3
61 1 return $self->data($arg);
62 }
63 else {
64 2 return $self->data();
65 }
66 } ## end sub config
67  
68 0 ▶sub _init_debug {
Subroutine _init_debug: not called CC=2 SCAR=17.9
69 0 my $self = shift;
70  
71 0 ▶ if($ENV{'CONFIG_YAK_DEBUG'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CONFIG_YAK_DEBUG"}) 0 0 0
72 0 return 1;
73 }
74  
75 0 return 0;
76 }
77  
78 ############################################
79 # THIS METHOD IS NOT PART OF OUR PUBLIC API!
80 # Usage :
81 # Purpose :
82 # Returns :
83 # Parameters :
84 # Throws : no exceptions
85 # Comments : none
86 # See Also : n/a
87 # THIS METHOD IS NOT PART OF OUR PUBLIC API!
88 1 ▶sub _init_data {
Subroutine _init_data: called CC=13 SCAR=34.5
89 1 my $self = shift;
90  
91 # glob locations and conf.d dirs!
92 1 my @files = ();
93 1 my @legacy_files = ();
94 1 foreach my $loc ( @{ $self->locations() } ) {
95 1 ▶ if ( -d $loc ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $loc) { } 0 1 1
elsif (-e $loc) { } 1 0 1
96 0 ▶ foreach my $file ( glob( $loc . '/*.conf' ) ) {
Subroutine BEGIN: called CC=1 SCAR=0.2
97 0 ▶ if ( $self->_is_legacy_config($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_legacy_config($file)) { } 0 0 0
98 0 push( @legacy_files, $file );
99 }
100 else {
101 0 push( @files, $file );
102 }
103 } ## end foreach my $file ( glob( $loc...))
104 ## no critic (ProhibitMismatchedOperators)
105 0 ▶ if ( -d $loc . '/conf.d' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $loc . '/conf.d') 0 0 0
106 ## use critic
107 0 foreach my $file ( glob( $loc . '/conf.d/*.conf' ) ) {
108 0 ▶ if ( $self->_is_legacy_config($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_legacy_config($file)) { } 0 0 0
109 0 push( @legacy_files, $file );
110 }
111 else {
112 0 push( @files, $file );
113 }
114 } ## end foreach my $file ( glob( $loc...))
115 } ## end if ( -d $loc . '/conf.d')
116 } ## end elsif ( -d $loc )
117 elsif ( -e $loc ) {
118 1 ▶ if ( $self->_is_legacy_config($loc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_legacy_config($loc)) { } 0 1 1
119 0 push( @legacy_files, $loc );
120 }
121 else {
122 1 push( @files, $loc );
123 }
124 } ## end if ( -e $loc )
125 } ## end foreach my $loc ( @{ $self->locations...})
126 ## no critic (RequireCheckedSyscalls)
127 1 ▶ print '_init_config - glob()ed these files: ' . join( q{:}, @files ) . "\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 1 0 1
128 1 ▶ print '_init_config - glob()ed these legacy files: ' . join( q{:}, @legacy_files ) . "\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 1 0 1
129 ## use critic
130 1 my $cfg = {};
131 1 $cfg = $self->_load_legacy_config( [@legacy_files], $cfg );
132 1 foreach my $file (@files) {
133 1 $cfg = $self->_load_config( [$file], $cfg );
134 }
135 1 return $cfg;
136 } ## end sub _init_data
137  
138 1 ▶sub _is_legacy_config {
Subroutine _is_legacy_config: called CC=6 SCAR=19.7
139 1 my $self = shift;
140 1 my $file = shift;
141  
142 1 my $is_legacy = 0;
143 1 ▶ if ( -e $file && open( my $FH, '<', $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and &Config::Yak::open(my $FH, "<", $file)) 1 0 1
Condition33%1/3
-e $file and &Config::Yak::open(my $FH, "<", $file)
!ll&&!rl&&r
001
MC/DC0%0/2
-e $file and &Config::Yak::open(my $FH, "<", $file)
-e $file &Config::Yak::open(my $FH, "<", $file)
Truth table33%1/3
A and B
A= -e $fileB= &Config::Yak::open(my $FH, "<", $file)
ABresult
0X0
100
111
144 1 my @lines = <$FH>;
145 1 close($FH);
146 1 foreach my $line (@lines) {
147 1 ▶ if ( $line =~ m/^\[/ ) { # ini-style config, old
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[/) { } 0 1 1
elsif ($line =~ /^\s*</) { } 1 0 1
148 0 $is_legacy = 1;
149 0 last;
150 }
151 elsif ( $line =~ m/^\s*</ ) { # pseudo-XML config, new
152 1 $is_legacy = 0;
153 1 last;
154 }
155 } ## end foreach my $line (@lines)
156  
157 } ## end if ( -e $file && open(...))
158 1 return $is_legacy;
159 } ## end sub _is_legacy_config
160  
161 1 ▶sub _load_legacy_config {
Subroutine _load_legacy_config: called CC=4 SCAR=22.6
162 1 my $self = shift;
163 1 my $files_ref = shift;
164 1 ▶ my $cfg = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
165  
166 1 Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
167 1 foreach my $file ( @{$files_ref} ) {
168 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
169 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
170 0 my $Config = Config::Tiny::->read($file);
171 0 ▶ print '_load_legacy_config - Loaded ' . $file . "\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
172 0 Data::Structure::Util::unbless($Config);
173 0 $cfg = Hash::Merge::merge( $cfg, $Config );
174 ## no critic (ProhibitMagicNumbers)
175 0 my $last_ts = ( stat($file) )[9];
176 ## use critic
177 0 ▶ $self->last_ts($last_ts) if $last_ts > $self->last_ts();
Branch: 1 branch
BranchTrueFalseTotal
if $last_ts > $self->last_ts 0 0 0
178 0 1;
179 } ## end try
180 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
181 0 ▶ warn "Loading $file failed: $_\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
182 0 };
183 } ## end if ( -e $file )
184 } ## end foreach my $file ( @{$files_ref...})
185 1 return $cfg;
186 } ## end sub _load_legacy_config
187  
188 1 ▶sub _load_config {
Subroutine _load_config: called CC=14 SCAR=33.5
189 1 my $self = shift;
190 1 my $files_ref = shift;
191 1 ▶ my $ccfg = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
192  
193 ## no critic (ProhibitNoWarnings)
194 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
195 ## no critic (ProhibitTwoArgOpen ProhibitBarewordFileHandles RequireBriefOpen ProhibitUnixDevNull)
196 1 ▶ if(!$self->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->debug) 0 1 1
197 0 ▶ open( OLD_STDERR, '>&STDERR' )
Branch: 1 branch
BranchTrueFalseTotal
&Config::Yak::open("OLD_STDERR", ">&STDERR") or die "Failed to save STDERR" 0 0 0
198 or die('Failed to save STDERR');
199 0 ▶ open( STDERR, '>', '/dev/null' )
Branch: 1 branch
BranchTrueFalseTotal
&Config::Yak::open("STDERR", ">", "/dev/null") or die "Failed to redirect STDERR" 0 0 0
200 or die('Failed to redirect STDERR');
201 }
202 ## use critic
203 1 my $cfg = {};
204 1 ▶ my $success = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 1 $cfg = Config::Any->load_files(
206 {
207 files => $files_ref,
208 use_ext => 1,
209 driver_args => {
210  
211 # see http://search.cpan.org/~tlinden/Config-General-2.50/General.pm
212 General => {
213 -UseApacheInclude => 0,
214 -IncludeRelative => 0,
215 -IncludeDirectories => 0,
216 -IncludeGlob => 0,
217 -SplitPolicy => 'equalsign',
218 -CComments => 0,
219 -AutoTrue => 1,
220 -MergeDuplicateBlocks => 1,
221 -MergeDuplicateOptions => 0,
222 -LowerCaseNames => 1,
223 -UTF8 => 1,
224 },
225 },
226 flatten_to_hash => 1,
227 },
228 );
229 1 1;
230 } ## end try
231 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
232 0 ▶ print 'Loading ' . join( q{:}, @{$files_ref} ) . " failed: $_\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
233 1 };
234 1 ▶ return $ccfg unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 1 1
235  
236 ## no critic (ProhibitTwoArgOpen)
237 1 ▶ if(!$self->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->debug) 0 1 1
238 0 open( STDERR, '>&OLD_STDERR' );
239 }
240 4 ▶ use warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 ## use critic
242 1 Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
243  
244 # older versions of Config::Any don't know flatten_to_hash,
245 # they'll always return an array of hashes, so we'll
246 # transform them here
247 1 ▶ if ( ref($cfg) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cfg eq "ARRAY") 0 1 1
248 0 my $ncfg = {};
249 0 foreach my $c ( @{$cfg} ) {
250 0 foreach my $file ( keys %{$c} ) {
251 0 $ncfg->{$file} = $c->{$file};
252 }
253 }
254 0 $cfg = $ncfg;
255 } ## end if ( ref($cfg) eq 'ARRAY')
256 1 ▶ if ( ref($cfg) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cfg eq "HASH") 1 0 1
257 1 foreach my $file ( keys %{$cfg} ) {
258 1 ▶ print "_load_config - Loaded $file\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 1 0 1
259 1 push(@{$self->files_read()},$file);
260 1 $ccfg = Hash::Merge::merge( $ccfg, $cfg->{$file} );
261 ## no critic (ProhibitMagicNumbers)
262 1 my $last_ts = ( stat($file) )[9];
263 ## use critic
264 1 ▶ $self->last_ts($last_ts) if $last_ts > $self->last_ts();
Branch: 1 branch
BranchTrueFalseTotal
if $last_ts > $self->last_ts 1 0 1
265 } ## end foreach my $file ( keys %{$cfg...})
266 } ## end if ( ref($cfg) eq 'HASH')
267 1 return $ccfg;
268 } ## end sub _load_config
269  
270 ############################################
271 # Usage :
272 # Purpose :
273 # Returns :
274 # Parameters :
275 # Throws : no exceptions
276 # Comments : none
277 # See Also : n/a
278 0 ▶sub add_config {
Subroutine add_config: not called CC=1 SCAR=6.9
Pod documented
279 0 my $self = shift;
280 0 my $file = shift;
281  
282 0 $self->config( Hash::Merge::merge( $self->config(), $self->_load_config( [$file] ) ) );
283 0 return 1;
284 } ## end sub add_config
285  
286 ############################################
287 # Usage :
288 # Purpose :
289 # Returns :
290 # Parameters :
291 # Throws : no exceptions
292 # Comments : none
293 # See Also : n/a
294 1 ▶sub reset_config {
Subroutine reset_config: called CC=1 SCAR=0.0
Pod documented
295 1 my $self = shift;
296  
297 1 $self->config( {} );
298  
299 1 return 1;
300 } ## end sub reset_config
301 ## no critic (ProhibitBuiltinHomonyms)
302 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
303 ## use critic
304 0 my $self = shift;
305  
306 0 $Data::Dumper::Sortkeys = 1;
307 0 return Dumper( $self->config() );
308 } ## end sub dump
309  
310 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
311 4 __PACKAGE__->meta->make_immutable;
312  
313 4 1;
314  
315 __END__
↑ Summary Yak/OrderedPlugins.pm »