1 package OpenSRF::Utils::Config::Section;
5 use vars qw/@ISA $AUTOLOAD $VERSION/;
6 push @ISA, qw/OpenSRF::Utils/;
8 use OpenSRF::Utils (':common');
10 $VERSION = do { my @r=(q$Revision$=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
15 #use overload '""' => \&OpenSRF::Utils::Config::dump_ini;
19 return $sec->__id(@_);
24 my $class = ref($self) || $self;
26 $self = bless {}, $class;
30 for my $line (@$lines) {
32 #($line) = split(/\s+\/\//, $line);
33 #($line) = split(/\s+#/, $line);
35 if ($line =~ /^\s*\[([^\[\]]+)\]/) {
36 $self->_sub_builder('__id');
41 my ($protokey,$value,$keytype,$key);
42 if ($line =~ /^([^=\s]+)\s*=\s*(.*)/s) {
43 ($protokey,$value) = ($1,$2);
44 ($keytype,$key) = split(/:/,$protokey);
47 $key = $protokey unless ($key);
49 if ($keytype ne $key) {
50 $keytype = lc $keytype;
51 if ($keytype eq 'list') {
52 $value = [split /\s*,\s*/, $value];
53 } elsif ($keytype eq 'bool') {
54 $value = do{ $value =~ /^t|y|1/i ? 1 : 0; };
55 } elsif ($keytype eq 'interval') {
56 $value = interval_to_seconds($value);
57 } elsif ($keytype eq 'subsection') {
58 if (exists $SECTIONCACHE{$value}) {
59 $value = $SECTIONCACHE{$value};
61 $SUBSECTION_FIXUP{$value}{$self->SECTION} = $key ;
67 $self->_sub_builder($key);
72 if (my $parent_def = $SUBSECTION_FIXUP{$self->SECTION}) {
73 my ($parent_section, $parent_key) = each %$parent_def;
74 $SECTIONCACHE{$parent_section}->{$parent_key} = $self;
75 delete $SUBSECTION_FIXUP{$self->SECTION};
78 $SECTIONCACHE{$self->SECTION} = $self;
83 package OpenSRF::Utils::Config;
85 use vars qw/@ISA $AUTOLOAD $VERSION $OpenSRF::Utils::ConfigCache/;
86 push @ISA, qw/OpenSRF::Utils/;
89 use OpenSRF::Utils (':common');
90 use OpenSRF::Utils::Logger;
92 #use overload '""' => \&OpenSRF::Utils::Config::dump_ini;
96 my $config_file = shift;
98 return unless $config_file;
100 $class->load( config_file => $config_file);
108 if ($self->isa('OpenSRF::Utils::Config')) {
109 if (UNIVERSAL::isa(scalar(caller()), 'OpenSRF::Utils::Config' )) {
112 $string = "# Main File: " . $self->FILE . "\n\n" . $string;
115 for my $section ( ('__id', grep { $_ ne '__id' } sort keys %$self) ) {
116 next if ($section eq 'env' && $self->isa('OpenSRF::Utils::Config'));
117 if ($section eq '__id') {
118 $string .= '['.$self->SECTION."]\n" if ($self->isa('OpenSRF::Utils::Config::Section'));
119 } elsif (ref($self->$section)) {
120 if (ref($self->$section) =~ /ARRAY/o) {
121 $string .= "list:$section = ". join(', ', @{$self->$section}) . "\n";
122 } elsif (UNIVERSAL::isa($self->$section,'OpenSRF::Utils::Config::Section')) {
123 if ($self->isa('OpenSRF::Utils::Config::Section')) {
124 $string .= "subsection:$section = " . $self->$section->SECTION . "\n";
127 next if ($self->$section->{__sub} && !$included);
128 $string .= $self->$section . "\n";
130 } elsif (UNIVERSAL::isa($self->$section,'OpenSRF::Utils::Config')) {
131 $string .= $self->$section . "\n";
134 next if $section eq '__sub';
135 $string .= "$section = " . $self->$section . "\n";
139 $string =~ s/^/## /gm;
140 $string = "# Subfile: " . $self->FILE . "\n#" . '-'x79 . "\n".'#include "'.$self->FILE."\"\n". $string;
148 OpenSRF::Utils::Config
154 use OpenSRF::Utils::Config;
156 my $config_obj = OpenSRF::Utils::Config->load( config_file => '/config/file.cnf' );
158 my $attrs_href = $config_obj->attributes();
160 $config_obj->attributes->no_db(0);
162 open FH, '>'.$config_obj->FILE() . '.new';
163 print FH $config_obj;
171 This module is mainly used by other modules to load a configuration file.
177 Hashrefs of sections can be returned by calling a method of the object of the same name as the section.
178 They can be set by passing a hashref back to the same method. Sections will B<NOT> be autovivicated, though.
180 Here be a config file example, HAR!:
185 subsection:definition=devel_db
188 dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl;host=nsite-dev
194 dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl
200 subsection:definition=devel_rpc
212 url=https://localhost:9000/
217 SSL_key_file=client-key.pem
218 SSL_cert_file=client-cert.pem
219 SSL_ca_file=cacert.pem
223 base_dir=/home/miker/cvs/NOC/monitor_core/
233 $VERSION = do { my @r=(q$Revision$=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
236 =head2 OpenSRF::Utils::Config->load( config_file => '/some/config/file.cnf' )
238 Returns a OpenSRF::Utils::Config object representing the config file that was loaded.
239 The most recently loaded config file (hopefully the only one per app)
240 is stored at $OpenSRF::Utils::ConfigCache. Use OpenSRF::Utils::Config::current() to get at it.
247 $pkg = ref($pkg) || $pkg;
251 (my $new_pkg = $args{config_file}) =~ s/\W+/_/g;
252 $new_pkg .= "::$pkg";
253 $new_section_pkg .= "${new_pkg}::Section";
259 sub section_pkg { return '$new_section_pkg'; }
261 package $new_section_pkg;
262 use base "${pkg}::Section";
267 return $new_pkg->_load( %args );
272 $pkg = ref($pkg) || $pkg;
277 if ((exists $$self{config_file} and OpenSRF::Utils::Config->current) and (OpenSRF::Utils::Config->current->FILE eq $$self{config_file}) and (!$self->{force})) {
278 delete $$self{force};
279 return OpenSRF::Utils::Config->current();
282 $self->_sub_builder('__id');
283 $self->FILE($$self{config_file});
284 delete $$self{config_file};
285 return undef unless ($self->FILE);
287 $self->load_config();
289 $self->mangle_dirs();
290 $self->mangle_logs();
292 $OpenSRF::Utils::ConfigCache = $self unless $self->nocache;
293 delete $$self{nocache};
294 delete $$self{force};
302 my @parts = (grep { UNIVERSAL::isa($_,'OpenSRF::Utils::Config::Section') } values %$self);
304 my $must_match = scalar(keys %filters);
306 foreach my $part (@parts) {
308 for my $fkey (keys %filters) {
309 $part_count++ if ($part->$key eq $filters{$key});
311 push @ok_parts, $part if ($part_count == $must_match);
319 return $OpenSRF::Utils::ConfigCache;
323 return shift()->__id(@_);
328 my $host = `hostname -f` || `uname -n`;
330 $$self{env} = $self->section_pkg->new;
331 $$self{env}{hostname} = $host;
336 return unless ($self->logs && $self->dirs && $self->dirs->log_dir);
337 for my $i ( keys %{$self->logs} ) {
338 next if ($self->logs->$i =~ /^\//);
339 $self->logs->$i($self->dirs->log_dir."/".$self->logs->$i);
345 return unless ($self->dirs && $self->dirs->base_dir);
346 for my $i ( keys %{$self->dirs} ) {
347 if ( $i ne 'base_dir' ) {
348 next if ($self->dirs->$i =~ /^\//);
349 my $dir_tmp = $self->dirs->base_dir."/".$self->dirs->$i;
350 $dir_tmp =~ s#//#/#go;
351 $dir_tmp =~ s#/$##go;
352 $self->dirs->$i($dir_tmp);
359 my $config = new FileHandle $self->FILE, 'r';
361 OpenSRF::Utils::Log->error("Could not open ".$self->FILE.": $!\n");
362 die "Could not open ".$self->FILE.": $!\n";
364 my @stripped_config = $self->__strip_comments($config) if (defined $config);
367 for my $line (@stripped_config) {
371 if ($line =~ /^\s*\[/ and @$chunk) {
372 my $section = $self->section_pkg->new($chunk);
374 my $sub_name = $section->SECTION;
375 $self->_sub_builder($sub_name);
376 $self->$sub_name($section);
378 #$self->{$section->SECTION} = $section;
384 if ($line =~ /^#\s*include\s+"(\S+)"\s*$/o) {
385 my $included_file = $1;
386 my $section = OpenSRF::Utils::Config->load(config_file => $included_file, nocache => 1);
388 my $sub_name = $section->FILE;
389 $self->_sub_builder($sub_name);
390 $self->$sub_name($section);
392 for my $subsect (keys %$section) {
393 next if ($subsect eq '__id');
395 $self->_sub_builder($subsect);
396 $self->$subsect($$section{$subsect});
398 #$self->$subsect($section->$subsect);
399 $self->$subsect->{__sub} = 1;
406 my $section = $self->section_pkg->new($chunk) if (@$chunk);
407 my $sub_name = $section->SECTION;
408 $self->_sub_builder($sub_name);
409 $self->$sub_name($section);
414 #------------------------------------------------------------------------------------------------------------------------------------
422 No know bugs, but report any to miker@purplefrog.com.
424 =head1 COPYRIGHT AND LICENSING
426 Mike Rylander, Copyright 2000-2004
428 The OpenSRF::Utils::Config module is free software. You may distribute under the terms
429 of the GNU General Public License version 2 or greater.