OpenInteract/Config.pm

filter: Statementstmt 16.3% 15 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 21.4% 3 / 14 Podpod 0.0% 0 / 10 totaltotal 11.3% 18 / 158 CC 21 SCAR 29.9
CC
21
Cov
12%
flatten_action_config
56.0
instance
20.0
get_dir
20.0
CRAP 20.0
« OpenInteract.pm ↑ Summary OpenInteract/Utility.pm »
1 package OpenInteract::Config;
2  
3 # $Id: Config.pm,v 1.7 2002/04/23 13:01:33 lachoy Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use OpenInteract::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Exporter;
8  
9 # AUTOLOAD not being used any longer... see below
10 #use vars qw( $AUTOLOAD );
11 #$AUTOLOAD = '';
12  
13 1 @OpenInteract::Config::ISA = qw( Exporter );
14 1 $OpenInteract::Config::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
15 1 @OpenInteract::Config::EXPORT_OK = qw( _w DEBUG );
16 1 my %CONFIG_TYPES = (
17 'perl' => 'OpenInteract::Config::PerlFile',
18 'ini' => 'OpenInteract::Config::IniFile',
19 );
20  
21 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Interface: subclasses should override these
24  
25 0 ▶sub read_config { return $_[0]; }
Subroutine read_config: not called CC=1 SCAR=6.9
Pod undocumented
26 0 ▶sub save_config { return undef; }
Subroutine save_config: not called CC=1 SCAR=6.9
Pod undocumented
27  
28  
29 # Create a new config object. This is a factory method: rather than
30 # creating new objects of the class OpenInteract::Config, we use the
31 # variable $type and create an object based on it.
32  
33 0 ▶sub instance {
Subroutine instance: not called CC=4 SCAR=30.0
Pod undocumented
34 0 my ( $pkg, $type, @params ) = @_;
35  
36 # Backwards compatibility fix -- this probably won't be here forever.
37  
38 0 ▶ $type ||= 'perl';
Condition0%0/2
$type ||= "perl"
l!l
00
MC/DC0%0/1
$type ||= "perl"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
39 0 my $class = $CONFIG_TYPES{ $type };
40 0 ▶ die "No configuration class corresponding to type ($type)" unless ( $class );
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
41 0 eval "require $class";
42 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
43 0 die "Configuration class ($class) cannot be used. Error: $@";
44 }
45 0 my $data = $class->read_config( @params );
46 0 return bless( $data, $class );
47 }
48  
49  
50 0 ▶sub read_file {
Subroutine read_file: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ( $class, $filename ) = @_;
52 0 DEBUG && _w( 1, "Config trying to read file [$filename]" );
53 0 ▶ open( CONF, $filename )
Branch: 1 branch
BranchTrueFalseTotal
open CONF, $filename or die "Cannot open [$filename] for reading: $!\n" 0 0 0
54 || die "Cannot open [$filename] for reading: $!\n";
55 0 my @lines = <CONF>;
56 0 close( CONF );
57 0 return \@lines;
58 }
59  
60 # Copy items from the default action into all the other actions --
61 # this method doesn't quite belong here but since we don't have
62 # anything dealing strictly with action information... One possibility
63 # is to create a ActionTable module which does stuff like this and
64 # operates on $R
65  
66 0 ▶sub flatten_action_config {
Subroutine flatten_action_config: not called CC=7 SCAR=40.3
Pod undocumented
67 0 my ( $self ) = @_;
68 0 ▶ my $default_action = $self->{action_info}{default} || $self->{action}{_default_action_info_};
Condition0%0/3
$self->{'action_info'}{'default'} || $self->{'action'}{'_default_action_info_'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'action_info'}{'default'} || $self->{'action'}{'_default_action_info_'}
$self->{'action_info'}{'default'} $self->{'action'}{'_default_action_info_'}
Truth table0%0/3
A || B
A= $self->{'action_info'}{'default'}B= $self->{'action'}{'_default_action_info_'}
ABresult
1X1
011
000
69 0 my @names = ();
70 0 foreach my $action_key ( keys %{ $self->{action} } ) {
71 0 ▶ next if ( $action_key eq 'default' or $action_key =~ /^_/ );
Branch: 1 branch
BranchTrueFalseTotal
if $action_key eq "default" or $action_key =~ /^_/ 0 0 0
Condition0%0/3
$action_key eq "default" or $action_key =~ /^_/
l!l&&r!l&&!r
000
MC/DC0%0/2
$action_key eq "default" or $action_key =~ /^_/
$action_key eq "default" $action_key =~ /^_/
Truth table0%0/3
A or B
A= $action_key eq "default"B= $action_key =~ /^_/
ABresult
1X1
011
000
72 0 foreach my $def ( keys %{ $default_action } ) {
73 0 ▶ $self->{action}{ $action_key }{ $def } ||= $default_action->{ $def };
Condition0%0/3
$self->{"action"}{$action_key}{$def} ||= $default_action->{$def}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"action"}{$action_key}{$def} ||= $default_action->{$def}
$self->{"action"}{$action_key}{$def} $default_action->{$def}
Truth table0%0/3
A ||= B
A= $self->{"action"}{$action_key}{$def}B= $default_action->{$def}
ABresult
1X1
011
000
74 }
75  
76 # Also ensure that the action information knows its own key
77  
78 0 $self->{action}{ $action_key }{name} = $action_key;
79 0 push @names, $action_key;
80 }
81 0 return \@names;
82 }
83  
84  
85 # Set a parameter
86  
87 0 ▶sub param_set {
Subroutine param_set: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my ( $self, $config, $value ) = @_;
89 0 $config = lc $config;
90 0 ▶ $self->{ $config } = $value if ( $value );
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
91 0 return $self->{ $config };
92 }
93  
94  
95 # Get the value of a key
96  
97 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
98 0 my ( $self, @p ) = @_;
99 0 my @configs = ();
100 0 foreach my $conf ( @p ) {
101 0 push @configs, $self->param_set( $conf );
102 }
103 0 ▶ if ( scalar @configs == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @configs == 1) 0 0 0
104 0 return $configs[0];
105 }
106 0 return @configs;
107 }
108  
109  
110 # Allow you to set multiple values at once
111  
112 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my ( $self, $p ) = @_;
114 0 my %configs = ();
115 0 my $count = 0;
116 0 my $last_conf = ''; # hack to return one value if only one passed in
117 0 foreach my $conf ( keys %{ $p } ) {
118 0 $configs{ $conf } = $self->param_set( $conf, $p->{ $conf } );
119 0 $last_conf = $conf;
120 0 $count++;
121 }
122 0 ▶ if ( $count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) 0 0 0
123 0 return $configs{ $last_conf };
124 }
125 0 return %configs;
126 }
127  
128  
129 # Do a macro expansion on the directory names -- this SHOULD be done
130 # only once (on read, or by request) and then the information in the
131 # config object would be stable
132  
133 0 ▶sub get_dir {
Subroutine get_dir: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my ( $self, $dir_tag ) = @_;
135 0 my $dir_hash = $self->{dir};
136 0 $dir_tag =~ s/_dir$//;
137 0 my $dir = $dir_hash->{ lc $dir_tag };
138 0 DEBUG && _w( 1, "get_dir(): start out with <<$dir>>" );
139 0 ▶ return undef if ( ! $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
140 0 ▶ while ( $dir =~ m|^\$([\w\_]+)/| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ m[^\$([\w\_]+)/]) 0 0 0
141 0 my $orig_lookup = $1;
142 0 my $lookup_dir = lc $orig_lookup;
143 0 DEBUG && _w( 1, " get_dir(): found lookup dir of <<$lookup_dir>>" );
144 0 ▶ return undef if ( ! $dir_hash->{ $lookup_dir } );
Branch: 1 branch
BranchTrueFalseTotal
unless $dir_hash->{$lookup_dir} 0 0 0
145 0 $dir =~ s/^\$$orig_lookup/$dir_hash->{ $lookup_dir }/;
146 0 DEBUG && _w( 1, " get_dir(): new directory: <<$dir>>" );
147 }
148 0 return $dir;
149 }
150  
151  
152  
153 0 ▶sub is_file_valid {
Subroutine is_file_valid: not called CC=2 SCAR=17.9
Pod undocumented
154 0 my ( $self, $filename ) = @_;
155  
156 0 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 0 0
157 0 my $msg = 'Cannot read configuration file!';
158 0 my $system_msg = "No valid filename ($filename) for reading configuration information!";
159 0 OpenInteract::Error->set({ user_msg => $msg,
160 type => 'config',
161 system_msg => $system_msg,
162 method => 'read_config',
163 extra => { filename => $filename } });
164 0 die $msg;
165 }
166 0 return 1;
167 }
168  
169 # Allow you to call config keys as methods -- we should probably get
170 # rid of this and force you to use it as a hashref...
171  
172 # AUTOLOAD no longer supported -- please email Chris if you have an
173 # issue with this.
174  
175 #sub AUTOLOAD {
176 # my ( $self, @params ) = @_;
177 # my $request = $AUTOLOAD;
178 # $request =~ s/.*://;
179 # DEBUG && _w( 1, "Trying to fulfill request ($request) from AUTOLOAD" );
180 # return $self->param_set( $request, @params);
181 #}
182  
183  
184 0 ▶sub _w {
Subroutine _w: not called CC=2 SCAR=17.9
185 0 ▶ return unless ( DEBUG >= shift );
Branch: 1 branch
BranchTrueFalseTotal
unless 0 >= shift() 0 0 0
186 0 my ( $pkg, $file, $line ) = caller;
187 0 my @ci = caller(1);
188 0 warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n";
189 }
190  
191 1 1;
192  
193 __END__
« OpenInteract.pm ↑ Summary OpenInteract/Utility.pm »