lib/OWL/Config.pm

filter: Statementstmt 84.2% 80 / 95 Branchbran 56.2% 18 / 32 Conditioncond 42.8% 6 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 8 totaltotal 69.3% 122 / 176 CC 25 SCAR 16.3
CC
25
Cov
74%
init
9.6
_resolve_file
7.2
dump
6.0
CRAP 5.1
« bin/scripts/owl2perl-generate-modules.pl ↑ Summary blib/script/owl2perl-generate-modules.pl »
1 #-----------------------------------------------------------------
2 # OWL::Config
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # $Id: Config.pm,v 1.1 2009-08-25 14:30:50 ubuntu Exp $
7 #-----------------------------------------------------------------
8
9 package OWL::Config;
10
11 5 ▶use Config::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use vars qw( $DEFAULT_CONFIG_FILE $ENV_CONFIG_DIR );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 # add versioning to this module
18 5 ▶use vars qw /$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /: (\d+)\.(\d+)/;
20
21 5 my %Config = (); # here are all configuration parameters
22 5 my %Unsuccess = (); # here are names (and reasons) of failed files
23 5 my %Success = (); # here are names of successfully read files
24
25 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 $DEFAULT_CONFIG_FILE = 'owl2perl-config.cfg';
27 5 $ENV_CONFIG_DIR = 'OWL_CFG_DIR';
28 }
29
30 # OO access; but there are no instance attributes - everything are
31 # class (shared) attributes
32
33 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
34 1 my ($class, @args) = @_;
35
36 # create an object
37 1 ▶ my $self = bless {}, ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
38
39 # initialize the object
40 1 $self->import (@args);
41
42 # done
43 1 return $self;
44 }
45
46
47 # this allows to specify a configuration file in the use clause:
48 # use OWL::Config ( 'my.file', 'your.file', { cachedir => 'somewhere' } )
49
50 6 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
51 6 shift->init ($DEFAULT_CONFIG_FILE, @_);
52 }
53
54 # @configs can be mix of scalars (names of configuration files) or
55 # hash references (direct configuration parameters); can be called
56 # more times - everything is appended
57
58 10 ▶sub init {
Subroutine init: called CC=9 SCAR=22.6
Pod undocumented
59 10 shift; # ignore invocant
60 10 my (@configs) = @_;
61 # add parameters AND resolve file names
62 10 foreach my $config (@configs) {
63 6 ▶ if (ref ($config) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq 'HASH') { } 0 6 6
64 0 %Config = (%Config, %$config);
65 } else {
66 6 my $file = OWL::Config->_resolve_file ($config);
67 6 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 4 2 6
68 4 $Unsuccess{$config} = 'File not found.';
69 4 next;
70 }
71 2 eval {
72 2 Config::Simple->import_from ($file, \%Config);
73 };
74 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
75 0 $@ =~ s| /\S+Config/Simple.pm line \d+, <FH>||;
76 #print STDERR "Reading configuration file '$file' failed: $@";
77 0 $Unsuccess{$file} = $@;
78 } else {
79 2 $Success{$file} = 1;
80 }
81 }
82 }
83 # I do not like default.XXX (done by Config::Simple) - so
84 # replicate these keys without the prefix 'default'
85 10 foreach my $key (keys %Config) {
86 5 my ($realkey) = ($key =~ /^$Config::Simple::DEFAULTNS\.(.*)/);
87 5 ▶ if ($realkey && ! exists $Config{$realkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realkey and not exists $Config{$realkey}) 1 4 5
Condition100%3/3
$realkey and not exists $Config{$realkey}
!ll&&!rl&&r
311
MC/DC100%2/2
$realkey and not exists $Config{$realkey}
$realkey not exists $Config{$realkey}
Truth table100%3/3
A and not(B)
A= $realkeyB= not exists $Config{$realkey}
ABresult
0X0
100
111
88 1 $Config{$realkey} = $Config{$key};
89 }
90 }
91 # Remove potential whitespaces from the keys (Config::Simple may
92 # leave theme there)
93 10 map { my $orig_key = $_;
94 6 ▶ s/\s//g and $Config{$_} = delete $Config{$orig_key} } keys %Config;
Condition33%1/3
s/\s//g && ($Config{$_} = delete $Config{$orig_key})
!ll&&!rl&&r
600
MC/DC0%0/2
s/\s//g && ($Config{$_} = delete $Config{$orig_key})
s/\s//g ($Config{$_} = delete $Config{$orig_key})
Truth table33%1/3
A && B
A= s/\s//gB= ($Config{$_} = delete $Config{$orig_key})
ABresult
0X0
100
111
95 }
96
97 # try to locate given $filename, return its full path:
98 # a) as it is - if such file exists
99 # b) as $ENV{OWL_CFG_DIR}/$filename
100 # c) in one of the @INC directories
101 # d) return undef
102
103 6 ▶sub _resolve_file {
Subroutine _resolve_file: called CC=7 SCAR=19.8
104 6 shift; # ignore invocant
105 6 my ($filename) = @_;
106 6 ▶ return $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 6 6
107
108
109 6 my $realfilename;
110 6 ▶ if ($ENV{$ENV_CONFIG_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$OWL::Config::ENV_CONFIG_DIR}) 2 4 6
111 2 $realfilename = File::Spec->catdir ($ENV{$ENV_CONFIG_DIR}, $filename);
112 2 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 2 0 2
113 }
114
115 4 $realfilename = undef;
116 4 $realfilename = File::Spec->catfile( File::Spec->catdir (File::HomeDir->my_home, "Perl-OWL2Perl"), $filename);
117 4 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 4 4
118
119 4 foreach my $prefix (@INC) {
120 37 $realfilename = File::Spec->catfile ($prefix, $filename);
121 37 ▶ return $realfilename if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 0 37 37
122 }
123 4 return undef;
124 }
125
126 # return value of a configuration argument; or add a new argument
127
128 3 ▶sub param {
Subroutine param: called CC=3 SCAR=14.2
Pod undocumented
129 3 shift;
130
131 # If called with no arguments, return all the
132 # possible keys
133 3 ▶ return keys %Config unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
134
135 # if called with a single argument, return the value
136 # matching this key
137 3 ▶ return $Config{$_[0]} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 3 0 3
138
139 # more arguments means adding...
140 # return $Config{$_[0]} = $_[1];
141 0 my $ret = $Config{$_[0]} = $_[1];
142 0 OWL::Config->import_names ('OWLCFG');
143 0 return $ret;
144 }
145
146 # remove one, more, or all configuration arguments
147
148 1 ▶sub delete {
Subroutine delete: called CC=4 SCAR=17.1
Pod undocumented
149 1 shift;
150
151 # if called with no arguments, delete all keys
152 1 ▶ %Config = () and return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
Condition20%1/5
@_ || (%Config = ()) && (return)
l!l&&r!l&&!r
100
%Config = () and return
!ll
00
MC/DC0%0/2
@_ || (%Config = ()) && (return)
@_ %Config = ()
Truth table33%1/3
A || B
A= @_B= %Config = ()
ABresult
1X1
011
000
153
154 # if called with arguments, delete the matching keys
155 1 foreach my $key (@_) {
156 1 delete $Config{$key};
157 }
158 }
159
160 # return a stringified version of all configuration options; an
161 # optional argument is a name for variable into which it is
162 # stringified (I do not know how to express it better: simply speaking
163 # this argument is passed to the Data::Dumper->Dump as the variable
164 # name)
165
166 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
167 0 shift;
168 0 ▶ my $varname = @_ ? shift : 'CONFIG';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
169 0 require Data::Dumper;
170 0 return Data::Dumper->Dump ( [\%Config], [$varname]);
171 }
172
173 # imports names into the caller's namespace as global variables;
174 # adapted from the same method in Config::Simple
175
176 4 ▶sub import_names {
Subroutine import_names: called CC=4 SCAR=15.0
Pod undocumented
177 4 shift;
178 4 ▶ my $namespace = @_ ? shift : (caller)[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 0 4
179 4 ▶ return if $namespace eq 'OWL::Config';
Branch: 1 branch
BranchTrueFalseTotal
if $namespace eq "OWL::Config" 0 4 4
180
181 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
182 5 ▶ no warnings; # avoid "Useless use of a variable..."
Subroutine BEGIN: called CC=1 SCAR=0.0
183 4 ▶ while ( my ($key, $value) = each %Config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Config) 0 4 4
184 0 $key =~ s/\W/_/g;
185 0 ${$namespace . '::' . uc($key)} = $value;
186 }
187 }
188
189 # return a list of configuration files successfully read (so far)
190
191 0 ▶sub ok_files {
Subroutine ok_files: not called CC=1 SCAR=6.9
Pod undocumented
192 0 return sort keys %Success;
193 }
194
195 # return a hash of configuration files un-successfully read (so far) -
196 # with corresponding error messages
197
198 0 ▶sub failed_files {
Subroutine failed_files: not called CC=1 SCAR=6.9
Pod undocumented
199 0 return %Unsuccess;
200 }
201
202
203
204 5 1;
205 __END__
« bin/scripts/owl2perl-generate-modules.pl ↑ Summary blib/script/owl2perl-generate-modules.pl »