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