| 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 | 266 | require 5.006_000; | |||||||||||||||||||
| 30 | 266 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 266 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 266 | require UR; | |||||||||||||||||||
| 33 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||||||||||||
| 34 | 266 | ▶ | use base qw(UR::ModuleBase); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 266 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 266 | our %config; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # create a combined configuration hash from inheritance tree | ||||||||||||||||||||
| 49 | sub _inherit_config | ||||||||||||||||||||
| 50 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_inherit_config: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||
| 52 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | my %cfg; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # get all packages inherited from | ||||||||||||||||||||
| 57 | 0 | my @inheritance = $self->inheritance; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # reverse loop through inheritance tree and construct config | ||||||||||||||||||||
| 60 | 0 | foreach my $cls (reverse(@inheritance)) | |||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 0 | ▶ | if (exists($config{$cls})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | { | ||||||||||||||||||||
| 64 | # add hash, overriding previous values | ||||||||||||||||||||
| 65 | 0 | %cfg = (%cfg, %{$config{$cls}}); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # now add the current class config | ||||||||||||||||||||
| 70 | 0 | ▶ | if (exists($config{$class})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | { | ||||||||||||||||||||
| 72 | 0 | %cfg = (%cfg, %{$config{$class}}); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # now add the object config | ||||||||||||||||||||
| 76 | 0 | ▶ | if (ref($self)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | # add the objects config | ||||||||||||||||||||
| 79 | 0 | ▶ | if (exists($config{"$class;$self"})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | { | ||||||||||||||||||||
| 81 | 0 | %cfg = (%cfg, %{$config{"$class;$self"}}); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
config: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||
| 127 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | # handle both object and class configuration | ||||||||||||||||||||
| 130 | 0 | my $target; | |||||||||||||||||||
| 131 | 0 | ▶ | if (ref($self)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | { | ||||||||||||||||||||
| 133 | # object config | ||||||||||||||||||||
| 134 | 0 | $target = "$class;$self"; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | else | ||||||||||||||||||||
| 137 | { | ||||||||||||||||||||
| 138 | # class config | ||||||||||||||||||||
| 139 | 0 | $target = $self; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # lay claim to the modules configuration | ||||||||||||||||||||
| 143 | 0 | $config{$target}{_Manager} = __PACKAGE__; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # see if values are being set | ||||||||||||||||||||
| 146 | 0 | ▶ | if (@_ > 1) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | { | ||||||||||||||||||||
| 148 | # set values in config hash, overriding any current values | ||||||||||||||||||||
| 149 | 0 | my (%opts) = @_; | |||||||||||||||||||
| 150 | 0 | %{$config{$target}} = (%{$config{$target}}, %opts); | |||||||||||||||||||
| 151 | 0 | return 1; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | # else they want one key or the whole hash | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # store config for object and inheritance tree | ||||||||||||||||||||
| 156 | 0 | my %cfg = $self->_inherit_config; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | # see how we were called | ||||||||||||||||||||
| 159 | 0 | ▶ | if (@_ == 1) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | { | ||||||||||||||||||||
| 161 | # return value of key | ||||||||||||||||||||
| 162 | 0 | my ($key) = @_; | |||||||||||||||||||
| 163 | # make sure hash key exists | ||||||||||||||||||||
| 164 | 0 | my $val; | |||||||||||||||||||
| 165 | 0 | ▶ | if (exists($cfg{$key})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | { | ||||||||||||||||||||
| 167 | 0 | $self->debug_message("config key $key exists"); | |||||||||||||||||||
| 168 | 0 | $val = $cfg{$key}; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | else | ||||||||||||||||||||
| 171 | { | ||||||||||||||||||||
| 172 | 0 | $self->error_message("config key $key does not exist"); | |||||||||||||||||||
| 173 | 0 | return; | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | 0 | return $val; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | # else return the entire config hash | ||||||||||||||||||||
| 178 | 0 | 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
check_config: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 0 | my ($key) = @_; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | # get config for inheritance tree | ||||||||||||||||||||
| 205 | 0 | my %cfg = $self->_inherit_config; | |||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 0 | ▶ | if (exists($cfg{$key})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 208 | { | ||||||||||||||||||||
| 209 | 0 | $self->debug_message("configuration key $key set: $cfg{$key}"); | |||||||||||||||||||
| 210 | 0 | return $cfg{$key}; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | # else | ||||||||||||||||||||
| 213 | 0 | $self->debug_message("configuration key $key not set"); | |||||||||||||||||||
| 214 | 0 | 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
default_config: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 234 | 0 | my $self = shift; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 0 | my (%opts) = @_; | |||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | # get config for inheritance tree | ||||||||||||||||||||
| 239 | 0 | my %cfg = $self->_inherit_config; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | # loop through arguments | ||||||||||||||||||||
| 242 | 0 | ▶ | while (my ($k, $v) = each(%opts)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | { | ||||||||||||||||||||
| 244 | # see is config value is already set | ||||||||||||||||||||
| 245 | 0 | ▶ | if (exists($cfg{$k})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | { | ||||||||||||||||||||
| 247 | 0 | $self->debug_message("config $k already set"); | |||||||||||||||||||
| 248 | 0 | next; | |||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | 0 | $self->debug_message("setting default for $k"); | |||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | # set config key | ||||||||||||||||||||
| 253 | 0 | $self->config($k => $v); | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 0 | 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 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
config_file: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 281 | 0 | my $self = shift; | |||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | 0 | my (%opts) = @_; | |||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | 0 | my $fh; | |||||||||||||||||||
| 286 | 0 | ▶ | if ($opts{path}) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 287 | { | ||||||||||||||||||||
| 288 | # make sure file is ok | ||||||||||||||||||||
| 289 | 0 | ▶ | if (-f $opts{path}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 290 | { | ||||||||||||||||||||
| 291 | 0 | $self->debug_message("config file exists: $opts{path}"); | |||||||||||||||||||
| 292 | } | ||||||||||||||||||||
| 293 | else | ||||||||||||||||||||
| 294 | { | ||||||||||||||||||||
| 295 | 0 | $self->error_message("config file does not exist: $opts{path}"); | |||||||||||||||||||
| 296 | 0 | return; | |||||||||||||||||||
| 297 | } | ||||||||||||||||||||
| 298 | 0 | ▶ | if (-r $opts{path}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | { | ||||||||||||||||||||
| 300 | 0 | $self->debug_message("config file is readable: $opts{path}"); | |||||||||||||||||||
| 301 | } | ||||||||||||||||||||
| 302 | else | ||||||||||||||||||||
| 303 | { | ||||||||||||||||||||
| 304 | 0 | $self->error_message("config file is not readable: $opts{path}"); | |||||||||||||||||||
| 305 | 0 | return; | |||||||||||||||||||
| 306 | } | ||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | # open file | ||||||||||||||||||||
| 309 | 0 | $fh = IO::File->new("<$opts{path}"); | |||||||||||||||||||
| 310 | 0 | ▶ | if (defined($fh)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 311 | { | ||||||||||||||||||||
| 312 | 0 | $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 | 0 | . $opts{path}); | |||||||||||||||||||
| 318 | 0 | return; | |||||||||||||||||||
| 319 | } | ||||||||||||||||||||
| 320 | } | ||||||||||||||||||||
| 321 | elsif ($opts{handle}) | ||||||||||||||||||||
| 322 | { | ||||||||||||||||||||
| 323 | 0 | $fh = $opts{handle}; | |||||||||||||||||||
| 324 | } | ||||||||||||||||||||
| 325 | else | ||||||||||||||||||||
| 326 | { | ||||||||||||||||||||
| 327 | 0 | $self->error_message("no config file input specified"); | |||||||||||||||||||
| 328 | 0 | return; | |||||||||||||||||||
| 329 | } | ||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | # read through file | ||||||||||||||||||||
| 332 | 0 | my %fconfig; | |||||||||||||||||||
| 333 | 0 | ▶ | while (defined(my $line = $fh->getline)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 334 | { | ||||||||||||||||||||
| 335 | # clean up | ||||||||||||||||||||
| 336 | 0 | chomp($line); | |||||||||||||||||||
| 337 | 0 | $line =~ s/\#.*//; | |||||||||||||||||||
| 338 | 0 | $line =~ s/^\s*//; | |||||||||||||||||||
| 339 | 0 | $line =~ s/\s*$//; | |||||||||||||||||||
| 340 | 0 | ▶ | next unless $line =~ m/\S/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | # parse | ||||||||||||||||||||
| 343 | 0 | my ($k, $v) = split(m/\s*=\s*/, $line, 2); | |||||||||||||||||||
| 344 | 0 | $fconfig{$k} = $v; | |||||||||||||||||||
| 345 | } | ||||||||||||||||||||
| 346 | 0 | $fh->close; | |||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | # update config | ||||||||||||||||||||
| 349 | 0 | return $self->config(%fconfig); | |||||||||||||||||||
| 350 | } | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | 266 | 1; | |||||||||||||||||||
| 353 | |||||||||||||||||||||
| 354 | #$Header$ | ||||||||||||||||||||