lib/MOSES/MOBY/Config.pm

filter: Statementstmt 61.0% 58 / 95 Branchbran 28.1% 9 / 32 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 68.4% 13 / 19 Podpod 75.0% 6 / 8 totaltotal 48.8% 86 / 176 CC 25 SCAR 25.1
CC
25
Cov
48%
init
33.0
delete
20.0
param
12.0
CRAP 12.3
« lib/MOSES/MOBY/Generators/Utils.pm ↑ Summary lib/auto/share/dist/MOSES-MOBY/samples/Service/Mabuhay.pm »
1 #-----------------------------------------------------------------
2 # MOSES::MOBY::Config
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # $Id: Config.pm,v 1.6 2009/10/13 16:42:20 kawas Exp $
7 #-----------------------------------------------------------------
8  
9 package MOSES::MOBY::Config;
10 2 ▶use Config::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use vars qw( $DEFAULT_CONFIG_FILE $ENV_CONFIG_DIR );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # add versioning to this module
17 2 ▶use vars qw /$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /: (\d+)\.(\d+)/;
19  
20 2 my %Config = (); # here are all configuration parameters
21 2 my %Unsuccess = (); # here are names (and reasons) of failed files
22 2 my %Success = (); # here are names of successfully read files
23  
24 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 $DEFAULT_CONFIG_FILE = 'moby-services.cfg';
26 2 $ENV_CONFIG_DIR = 'BIOMOBY_CFG_DIR';
27 }
28  
29 =head1 NAME
30  
31 MOSES::MOBY::Config - A hash based configuration module based on Config::Simple
32  
33 =head1 SYNOPSIS
34  
35 # use config allong with the MOSES config file
36 use MOSES::MOBY::Config;
37  
38 # use config along with my properties.file
39 use MOSES::MOBY::Config qw /'properties.file'/;
40  
41 # print the successfully read config files
42 foreach my $file (MOSES::MOBY::Config->ok_files) {
43 print "\t$file - successfully processed\n";
44 }
45  
46 # print a list of files that failed to load
47 my %failed = MOSES::MOBY::Config->failed_files;
48 if (keys %failed > 0) {
49 print "Failed configuration files:\n";
50 foreach my $file (sort keys %failed) {
51 my $msg = $failed{$file}; $msg =~ s/\n$//;
52 print "\t$file => $msg\n";
53 }
54 }
55  
56 # print out the config params read thus far
57 print "All configuration parameters:\n";
58 foreach my $name (sort MOSES::MOBY::Config->param()) {
59 print "\t$name => " . MOSES::MOBY::Config->param ($name);
60 }
61  
62 =head1 DESCRIPTION
63  
64 A module for reading configuration files and maintaining configuration parameters
65  
66 =head1 AUTHORS
67  
68 Martin Senger (martin.senger [at] gmail [dot] com)
69 Edward Kawas (edward.kawas [at] gmail [dot] com)
70  
71 =head1 SUBROUTINES
72  
73 =cut
74  
75 # OO access; but there are no instance attributes - everything are
76 # class (shared) attributes
77  
78 =head2 new
79  
80 Instantiates a new MOSES::MOBY::Config reference. Mainly here for OO access.
81 There are no instance attributes, only class attributes
82  
83 =cut
84  
85  
86 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
87 0 my ($class, @args) = @_;
88  
89 # create an object
90 0 ▶ my $self = bless {}, ref ($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
91  
92 # initialize the object
93 0 $self->import (@args);
94  
95 # done
96 0 return $self;
97 }
98  
99  
100 # this allows to specify a configuration file in the use clause:
101 # use MOSES::MOBY::Config ( 'my.file', 'your.file', { cachedir => 'somewhere' } )
102  
103 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
104 2 shift->init ($DEFAULT_CONFIG_FILE, @_);
105 }
106  
107 # @configs can be mix of scalars (names of configuration files) or
108 # hash references (direct configuration parameters); can be called
109 # more times - everything is appended
110  
111 4 ▶sub init {
Subroutine init: called CC=9 SCAR=35.0
Pod undocumented
112 4 shift; # ignore invocant
113 4 my (@configs) = @_;
114  
115 # add parameters AND resolve file names
116 4 foreach my $config (@configs) {
117 2 ▶ if (ref ($config) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq 'HASH') { } 0 2 2
118 0 %Config = (%Config, %$config);
119 } else {
120 2 my $file = MOSES::MOBY::Config->_resolve_file ($config);
121 2 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 2 0 2
122 2 $Unsuccess{$config} = 'File not found.';
123 2 next;
124 }
125 0 eval {
126 0 Config::Simple->import_from ($file, \%Config);
127 };
128 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
129 0 $@ =~ s| /\S+Config/Simple.pm line \d+, <FH>||;
130 # print STDERR "Reading configuration file '$file' failed: $@";
131 0 $Unsuccess{$file} = $@;
132 } else {
133 0 $Success{$file} = 1;
134 }
135 }
136 }
137 # I do not like default.XXX (done by Config::Simple) - so
138 # replicate these keys without the prefix 'default'
139 4 foreach my $key (keys %Config) {
140 0 my ($realkey) = ($key =~ /^$Config::Simple::DEFAULTNS\.(.*)/);
141 0 ▶ if ($realkey && ! exists $Config{$realkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realkey and not exists $Config{$realkey}) 0 0 0
Condition0%0/3
$realkey and not exists $Config{$realkey}
!ll&&!rl&&r
000
MC/DC0%0/2
$realkey and not exists $Config{$realkey}
$realkey not exists $Config{$realkey}
Truth table0%0/3
A and not(B)
A= $realkeyB= not exists $Config{$realkey}
ABresult
0X0
100
111
142 0 $Config{$realkey} = $Config{$key};
143 }
144 }
145  
146 # Remove potential whitespaces from the keys (Config::Simple may
147 # leave theme there)
148 0 map { my $orig_key = $_;
149 0 ▶ s/\s//g and $Config{$_} = delete $Config{$orig_key} } keys %Config;
Condition0%0/3
s/\s//g && ($Config{$_} = delete $Config{$orig_key})
!ll&&!rl&&r
000
MC/DC0%0/2
s/\s//g && ($Config{$_} = delete $Config{$orig_key})
s/\s//g ($Config{$_} = delete $Config{$orig_key})
Truth table0%0/3
A && B
A= s/\s//gB= ($Config{$_} = delete $Config{$orig_key})
ABresult
0X0
100
111
150 }
151  
152 # try to locate given $filename, return its full path:
153 # a) as it is - if such file exists
154 # b) as $ENV{BIOMOBY_CFG_DIR}/$filename
155 # c) in one of the @INC directories
156 # d) return undef
157  
158 2 ▶sub _resolve_file {
Subroutine _resolve_file: called CC=7 SCAR=21.8
159 2 shift; # ignore invocant
160 2 my ($filename) = @_;
161 2 ▶ return $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 2 2
162  
163  
164 2 my $realfilename;
165 2 ▶ if ($ENV{$ENV_CONFIG_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$MOSES::MOBY::Config::ENV_CONFIG_DIR}) 0 2 2
166 0 $realfilename = File::Spec->catdir ($ENV{$ENV_CONFIG_DIR}, $filename);
167 0 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 0 0
168 }
169
170 2 $realfilename = undef;
171 2 $realfilename = File::Spec->catfile( File::Spec->catdir (File::HomeDir->my_home, "Perl-MoSeS"), $filename);
172 2 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 2 2
173
174 2 foreach my $prefix (@INC) {
175 18 $realfilename = File::Spec->catfile ($prefix, $filename);
176 18 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 18 18
177 }
178 2 return undef;
179 }
180  
181 # return value of a configuration argument; or add a new argument
182  
183 =head2 param
184  
185 If called with no arguments, all of the possible config keys are returned.
186 If called with a single argument, then that argument is assumed to be a key and the value for that key is returned.
187  
188 =cut
189  
190 0 ▶sub param {
Subroutine param: not called CC=3 SCAR=24.8
Pod documented
191 0 shift;
192  
193 # If called with no arguments, return all the
194 # possible keys
195 0 ▶ return keys %Config unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
196  
197 # if called with a single argument, return the value
198 # matching this key
199 0 ▶ return $Config{$_[0]} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
200  
201 # more arguments means adding...
202 # return $Config{$_[0]} = $_[1];
203 0 my $ret = $Config{$_[0]} = $_[1];
204 0 MOSES::MOBY::Config->import_names ('MOBYCFG');
205 0 return $ret;
206 }
207  
208 # remove one, more, or all configuration arguments
209  
210 =head2 delete
211  
212 removes one or more of the configuration keys and their associated values.
213  
214 =cut
215  
216 0 ▶sub delete {
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
217 0 shift;
218  
219 # if called with no arguments, delete all keys
220 0 ▶ %Config = () and return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
Condition0%0/5
@_ || (%Config = ()) && (return)
l!l&&r!l&&!r
000
%Config = () and return
!ll
00
MC/DC0%0/2
@_ || (%Config = ()) && (return)
@_ %Config = ()
Truth table0%0/3
A || B
A= @_B= %Config = ()
ABresult
1X1
011
000
221  
222 # if called with arguments, delete the matching keys
223 0 foreach my $key (@_) {
224 0 delete $Config{$key};
225 }
226 }
227  
228 # return a stringified version of all configuration options; an
229 # optional argument is a name for variable into which it is
230 # stringified (I do not know how to express it better: simply speaking
231 # this argument is passed to the Data::Dumper->Dump as the variable
232 # name)
233  
234 =head2 dump
235  
236 Returns a stringified version of all configuration parameters;
237  
238 If passed a scalar parameter, then the dump will be given that variable name.
239 This dump can be eval{}'ed.
240  
241 =cut
242  
243 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
244 0 shift;
245 0 ▶ my $varname = @_ ? shift : 'CONFIG';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
246 0 require Data::Dumper;
247 0 return Data::Dumper->Dump ( [\%Config], [$varname]);
248 }
249  
250 # imports names into the caller's namespace as global variables;
251 # adapted from the same method in Config::Simple
252  
253 2 ▶sub import_names {
Subroutine import_names: called CC=4 SCAR=15.0
Pod undocumented
254 2 shift;
255 2 ▶ my $namespace = @_ ? shift : (caller)[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
256 2 ▶ return if $namespace eq 'MOSES::MOBY::Config';
Branch: 1 branch
BranchTrueFalseTotal
if $namespace eq "MOSES::MOBY::Config" 0 2 2
257  
258 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
259 2 ▶ no warnings; # avoid "Useless use of a variable..."
Subroutine BEGIN: called CC=1 SCAR=0.0
260 2 ▶ while ( my ($key, $value) = each %Config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Config) 0 2 2
261 0 $key =~ s/\W/_/g;
262 0 ${$namespace . '::' . uc($key)} = $value;
263 }
264 }
265  
266 # return a list of configuration files successfully read (so far)
267  
268 =head2 ok_files
269  
270 returns a list of the configuration files successfully read thus far ...
271  
272 =cut
273  
274 0 ▶sub ok_files {
Subroutine ok_files: not called CC=1 SCAR=6.9
Pod documented
275 0 return sort keys %Success;
276 }
277  
278 # return a hash of configuration files un-successfully read (so far) -
279 # with corresponding error messages
280  
281 =head2 failed_files
282  
283 returns a hash of the configuration files unsuccessfully read thus far and their corresponding error messages.
284  
285 =cut
286  
287 0 ▶sub failed_files {
Subroutine failed_files: not called CC=1 SCAR=6.9
Pod documented
288 0 return %Unsuccess;
289 }
290  
291 2 1;
292 __END__
« lib/MOSES/MOBY/Generators/Utils.pm ↑ Summary lib/auto/share/dist/MOSES-MOBY/samples/Service/Mabuhay.pm »