blib/lib/UR/ModuleConfig.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 165 SCAR 57.2
CC
17
Cov
0%
config_file
56.0
_inherit_config
30.0
config
30.0
CRAP 306.0
1 # Manage dynamic configuration of modules.
2  
3 package UR::ModuleConfig;
4  
5 =pod
6  
7 =head1 NAME
8  
9 UR::ModuleConfig - manage dynamic configuration of modules.
10  
11 =head1 SYNOPSIS
12  
13 package MyModule;
14 use base qw(UR::ModuleConfig);
15  
16 MyModule->config(%conf);
17 $val = MyModule->config('key');
18 %conf = MyModule->config;
19  
20 =head1 DESCRIPTION
21  
22 This module manages the configuration for modules. Configurations can
23 be read from files or set dynamically. Modules wishing to use the
24 configuration methods should inherit from the module.
25  
26 =cut
27  
28 # set up package
29 require 5.006_000;
30 use warnings;
31 use strict;
32 require UR;
33 our $VERSION = "0.46"; # UR $VERSION;;
34 use base qw(UR::ModuleBase);
35 use IO::File;
36  
37 =pod
38  
39 =head2 METHODS
40  
41 The methods deal with managing configuration.
42  
43 =cut
44  
45 # hash containing all configuration information
46 our %config;
47  
48 # create a combined configuration hash from inheritance tree
49 sub _inherit_config
50 {
51 my $self = shift;
52 my $class = ref($self) || $self;
53  
54 my %cfg;
55  
56 # get all packages inherited from
57 my @inheritance = $self->inheritance;
58  
59 # reverse loop through inheritance tree and construct config
60 foreach my $cls (reverse(@inheritance))
61 {
62 if (exists($config{$cls}))
63 {
64 # add hash, overriding previous values
65 %cfg = (%cfg, %{$config{$cls}});
66 }
67 }
68  
69 # now add the current class config
70 if (exists($config{$class}))
71 {
72 %cfg = (%cfg, %{$config{$class}});
73 }
74  
75 # now add the object config
76 if (ref($self))
77 {
78 # add the objects config
79 if (exists($config{"$class;$self"}))
80 {
81 %cfg = (%cfg, %{$config{"$class;$self"}});
82 }
83 }
84  
85 return %cfg;
86 }
87  
88 =pod
89  
90 =over 4
91  
92 =item config
93  
94 MyModule->config(%config);
95 $val = MyModule->config('key');
96 %conf = MyModule->config;
97  
98 my $obj = MyModule->new;
99 $obj->config(%config);
100  
101 This method can be called three ways, as either a class or object
102 method. The first method takes a hash as its argument and sets the
103 configuration parameters given in the hash. The second method takes a
104 single argument which should be one of the keys of the hash that set
105 the config parameters and returns the value of that config hash key.
106 The final method takes no arguments and returns the entire
107 configuration hash.
108  
109 When called as an object method, the config for both the object and
110 all classes in its inheritance hierarchy are referenced, with the
111 object config taking precedence over class methods and class methods
112 closer to the object (first in the @ISA array) taking precedence over
113 those further away (later in the @ISA array). When called as a class
114 method, the same procedure is used, except no object configuration is
115 referenced.
116  
117 Do not use configuration keys that begin with an underscore (C<_>).
118 These are reserved for internal use.
119  
120 =back
121  
122 =cut
123  
124 sub config
125 {
126 my $self = shift;
127 my $class = ref($self) || $self;
128  
129 # handle both object and class configuration
130 my $target;
131 if (ref($self))
132 {
133 # object config
134 $target = "$class;$self";
135 }
136 else
137 {
138 # class config
139 $target = $self;
140 }
141  
142 # lay claim to the modules configuration
143 $config{$target}{_Manager} = __PACKAGE__;
144  
145 # see if values are being set
146 if (@_ > 1)
147 {
148 # set values in config hash, overriding any current values
149 my (%opts) = @_;
150 %{$config{$target}} = (%{$config{$target}}, %opts);
151 return 1;
152 }
153 # else they want one key or the whole hash
154  
155 # store config for object and inheritance tree
156 my %cfg = $self->_inherit_config;
157  
158 # see how we were called
159 if (@_ == 1)
160 {
161 # return value of key
162 my ($key) = @_;
163 # make sure hash key exists
164 my $val;
165 if (exists($cfg{$key}))
166 {
167 $self->debug_message("config key $key exists");
168 $val = $cfg{$key};
169 }
170 else
171 {
172 $self->error_message("config key $key does not exist");
173 return;
174 }
175 return $val;
176 }
177 # else return the entire config hash
178 return %cfg;
179 }
180  
181 =pod
182  
183 =over 4
184  
185 =item check_config
186  
187 $obj->check_config($key);
188  
189 This method checks to see if a value is set. Unlike config, it does
190 not issue a warning if the key is not set. If the key is not set,
191 C<undef> is returned. If the key has been set, the value of the key
192 is returned (which may be C<undef>).
193  
194 =back
195  
196 =cut
197  
198 sub check_config
199 {
200 my $self = shift;
201  
202 my ($key) = @_;
203  
204 # get config for inheritance tree
205 my %cfg = $self->_inherit_config;
206  
207 if (exists($cfg{$key}))
208 {
209 $self->debug_message("configuration key $key set: $cfg{$key}");
210 return $cfg{$key};
211 }
212 # else
213 $self->debug_message("configuration key $key not set");
214 return;
215 }
216  
217 =pod
218  
219 =over 4
220  
221 =item default_config
222  
223 $class->default_config(%defaults);
224  
225 This method allows the developer to set configuration values, only if
226 they are not already set.
227  
228 =back
229  
230 =cut
231  
232 sub default_config
233 {
234 my $self = shift;
235  
236 my (%opts) = @_;
237  
238 # get config for inheritance tree
239 my %cfg = $self->_inherit_config;
240  
241 # loop through arguments
242 while (my ($k, $v) = each(%opts))
243 {
244 # see is config value is already set
245 if (exists($cfg{$k}))
246 {
247 $self->debug_message("config $k already set");
248 next;
249 }
250 $self->debug_message("setting default for $k");
251  
252 # set config key
253 $self->config($k => $v);
254 }
255  
256 return 1;
257 }
258  
259 =pod
260  
261 =over 4
262  
263 =item config_file
264  
265 $rv = $class->config_file(path => $path);
266 $rv = $class->config_file(handle => $fh);
267  
268 This method reads in the given file and expects key-value pairs, one
269 per line. The key and value should be separated by an equal sign,
270 C<=>, with optional surrounding space. It currently only handles
271 single value values.
272  
273 The method returns true upon success, C<undef> on failure.
274  
275 =back
276  
277 =cut
278  
279 sub config_file
280 {
281 my $self = shift;
282  
283 my (%opts) = @_;
284  
285 my $fh;
286 if ($opts{path})
287 {
288 # make sure file is ok
289 if (-f $opts{path})
290 {
291 $self->debug_message("config file exists: $opts{path}");
292 }
293 else
294 {
295 $self->error_message("config file does not exist: $opts{path}");
296 return;
297 }
298 if (-r $opts{path})
299 {
300 $self->debug_message("config file is readable: $opts{path}");
301 }
302 else
303 {
304 $self->error_message("config file is not readable: $opts{path}");
305 return;
306 }
307  
308 # open file
309 $fh = IO::File->new("<$opts{path}");
310 if (defined($fh))
311 {
312 $self->debug_message("opened config file for reading: $opts{path}");
313 }
314 else
315 {
316 $self->error_message("failed to open config file for reading: "
317 . $opts{path});
318 return;
319 }
320 }
321 elsif ($opts{handle})
322 {
323 $fh = $opts{handle};
324 }
325 else
326 {
327 $self->error_message("no config file input specified");
328 return;
329 }
330  
331 # read through file
332 my %fconfig;
333 while (defined(my $line = $fh->getline))
334 {
335 # clean up
336 chomp($line);
337 $line =~ s/\#.*//;
338 $line =~ s/^\s*//;
339 $line =~ s/\s*$//;
340 next unless $line =~ m/\S/;
341  
342 # parse
343 my ($k, $v) = split(m/\s*=\s*/, $line, 2);
344 $fconfig{$k} = $v;
345 }
346 $fh->close;
347  
348 # update config
349 return $self->config(%fconfig);
350 }
351  
352 1;
353  
354 #$Header$