blib/lib/Youri/Config.pm

filter: Statementstmt 65.3% 51 / 78 Branchbran 22.2% 8 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 100% 3 / 3 totaltotal 54.1% 72 / 133 CC 22 SCAR 39.6
CC
22
Cov
51%
new
85.7
get_param
12.0
get_arg
6.0
CRAP 52.6
↑ Summary
1 # $Id: Config.pm 2458 2017-11-13 18:30:13Z guillomovitch $
2 package Youri::Config;
3  
4 =head1 NAME
5  
6 Youri::Config - Youri configuration handler
7  
8 =head1 SYNOPSIS
9  
10 use Youri::Config;
11  
12 my $app = Youri::Config->new(
13 options => {
14 help => '|h!'
15 },
16 directories => [ '/etc/youri', "$ENV{HOME}/.youri" ],
17 file => 'app.conf',
18 );
19  
20 # get command line argument
21 my $foo = $app->get_arg('foo');
22  
23 # get configuration file parameter
24 my $bar = $app->get_param('bar');
25  
26 =head1 DESCRIPTION
27  
28 This class handle configuration for all YOURI applications.
29  
30 The command line specification is used to manage arguments through
31 Getopt::Long. Unless B<--config> argument is given, the list of directories is
32 then scanned for a file with given name, and halt as soon as it find one. If no
33 readable file is found, an exception is thrown. The file is then processed
34 through YAML::AppConfig. If parsing fails, an exception is thrown.
35  
36 =head1 CONFIGURATION FILE FORMAT
37  
38 =head2 SHARED KEYS
39  
40 In addition to the application-specific optional or mandatory parameters, all
41 YOURI applications support the following optional top-level parameters:
42  
43 =over
44  
45 =item B<includes>
46  
47 A list of additional configuration files.
48  
49 =item B<foo>
50  
51 An arbitrary variable, usable everywhere else in the file.
52  
53 =back
54  
55 =head2 PLUGIN DEFINITION
56  
57 All YOURI application heavily rely on plugins defined in their configuration
58 files. A plugin definition is composed from the following parameters:
59  
60 =over
61  
62 =item B<class>
63  
64 The class of this plugin.
65  
66 =item B<options>
67  
68 The options of this plugin.
69  
70 =back
71  
72 =head1 SEE ALSO
73  
74 YAML::AppConfig, Getopt::Long
75  
76 =cut
77  
78 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use YAML::AppConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use version; our $VERSION = qv('0.2.4');
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 =head2 new(%args)
88  
89 Creates and returns a new Youri::Config object.
90  
91 =cut
92  
93 2 ▶sub new {
Subroutine new: called CC=19 SCAR=44.5
Pod documented
94 2 my ($class, %options) = @_;
95  
96 # command line arguments
97 2 my $args = {
98 verbose => 0
99 };
100 2 my @args;
101 2 ▶ if ($options{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"args"}) 0 2 2
102 0 ▶ while (my ($arg, $spec) = each %{$options{args}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$options{"args"};}) 0 0 0
103 0 push(@args, ($arg . $spec) => \$args->{$arg});
104 }
105 }
106 push(@args,
107 'config=s' => \$args->{config},
108 'h|help' => \$args->{help},
109 'v|verbose+' => \$args->{verbose}
110 2 );
111 2 GetOptions(@args);
112  
113 2 ▶ if ($args->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"help"}) 0 2 2
114 0 ▶ if (!@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV) 0 0 0
115 # standard help, available immediatly
116 0 my $filename = (caller)[1];
117 0 pod2usage(
118 -input => $filename,
119 -verbose => 0
120 );
121 }
122 }
123  
124 # config files parameters
125
126 # find configuration file to use
127 2 my $main_file;
128 2 ▶ if ($args->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'config'}) { } 0 2 2
129 0 ▶ if (! -f $args->{config}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -f $args->{'config'}) { } 0 0 0
elsif (not -r $args->{'config'}) { } 0 0 0
130 0 croak "Non-existing file $args->{config}";
131 } elsif (! -r $args->{config}) {
132 0 croak "Non-readable file $args->{config}";
133 } else {
134 0 $main_file = $args->{config};
135 }
136 } else {
137 2 foreach my $directory (@{$options{directories}}) {
138 2 my $file = "$directory/$options{file}";
139 2 ▶ next unless -f $file && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and -r $file 0 2 2
Condition33%1/3
-f $file and -r $file
!ll&&!rl&&r
002
MC/DC0%0/2
-f $file and -r $file
-f $file -r $file
Truth table33%1/3
A and B
A= -f $fileB= -r $file
ABresult
0X0
100
111
140 2 $main_file = $file;
141 2 last;
142 }
143 }
144  
145 2 my $params;
146 2 ▶ if ($main_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_file) { } 2 0 2
147 2 eval {
148 2 $params = YAML::AppConfig->new(file => $main_file);
149 };
150 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
151 1 croak
152 "Invalid configuration file $main_file, aborting. " .
153 "The parser error was:\n" . $@;
154 }
155  
156 # process inclusions
157 1 my $includes = $params->get('includes');
158 1 ▶ if ($includes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($includes) 0 1 1
159 0 foreach my $include_file (@{$includes}) {
160 # convert relative path to absolute ones
161 0 $include_file = File::Spec->rel2abs(
162 $include_file, (File::Spec->splitpath($main_file))[1]
163 );
164  
165 0 ▶ if (! -f $include_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -f $include_file) { } 0 0 0
elsif (not -r $include_file) { } 0 0 0
166 0 warn "Non-existing file $include_file, skipping";
167 } elsif (! -r $include_file) {
168 0 warn "Non-readable file $include_file, skipping";
169 } else {
170 0 eval {
171 0 $params->merge(file => $include_file);
172 };
173 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
174 0 carp "Invalid included configuration file $include_file, skipping";
175 }
176 }
177 }
178 }
179 } else {
180 0 ▶ croak 'No config file found, aborting' if $options{mandatory};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"mandatory"} 0 0 0
181 }
182  
183 1 my $self = bless {
184 _args => $args,
185 _params => $params
186 }, $class;
187  
188 1 return $self;
189 }
190  
191 =head1 INSTANCE METHODS
192  
193 =head2 get_arg($arg)
194  
195 Returns the command-line argument $arg.
196  
197 =cut
198  
199 0 ▶sub get_arg {
Subroutine get_arg: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($self, $arg) = @_;
201 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
202  
203 0 return $self->{_args}->{$arg};
204 }
205  
206 =head2 get_param($param)
207  
208 Returns the configuration file parameter $param.
209  
210 =cut
211  
212  
213 0 ▶sub get_param {
Subroutine get_param: not called CC=3 SCAR=24.8
Pod documented
214 0 my ($self, $param) = @_;
215 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
216  
217 return $self->{_params} ?
218 0 ▶ $self->{_params}->get($param) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_params'} ? : 0 0 0
219 undef;
220 }
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 Copyright (C) 2002-2006, YOURI project
225  
226 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
227  
228 =cut
229  
230 2 1;
↑ Summary