1 package OpenSRF::Utils::Config::Section;
5 use vars qw/@ISA $AUTOLOAD $VERSION/;
6 push @ISA, qw/OpenSRF::Utils/;
8 use OpenSRF::Utils (':common');
9 use Net::Domain qw/hostfqdn/;
11 $VERSION = do { my @r=(q$Revision$=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
16 #use overload '""' => \&OpenSRF::Utils::Config::dump_ini;
20 return $sec->__id(@_);
25 my $class = ref($self) || $self;
27 $self = bless {}, $class;
31 for my $line (@$lines) {
33 #($line) = split(/\s+\/\//, $line);
34 #($line) = split(/\s+#/, $line);
36 if ($line =~ /^\s*\[([^\[\]]+)\]/) {
37 $self->_sub_builder('__id');
42 my ($protokey,$value,$keytype,$key);
43 if ($line =~ /^([^=\s]+)\s*=\s*(.*)/s) {
44 ($protokey,$value) = ($1,$2);
45 ($keytype,$key) = split(/:/,$protokey);
48 $key = $protokey unless ($key);
50 if ($keytype ne $key) {
51 $keytype = lc $keytype;
52 if ($keytype eq 'list') {
53 $value = [split /\s*,\s*/, $value];
54 } elsif ($keytype eq 'bool') {
55 $value = do{ $value =~ /^t|y|1/i ? 1 : 0; };
56 } elsif ($keytype eq 'interval') {
57 $value = interval_to_seconds($value);
58 } elsif ($keytype eq 'subsection') {
59 if (exists $SECTIONCACHE{$value}) {
60 $value = $SECTIONCACHE{$value};
62 $SUBSECTION_FIXUP{$value}{$self->SECTION} = $key ;
68 $self->_sub_builder($key);
73 if (my $parent_def = $SUBSECTION_FIXUP{$self->SECTION}) {
74 my ($parent_section, $parent_key) = each %$parent_def;
75 $SECTIONCACHE{$parent_section}->{$parent_key} = $self;
76 delete $SUBSECTION_FIXUP{$self->SECTION};
79 $SECTIONCACHE{$self->SECTION} = $self;
84 package OpenSRF::Utils::Config;
86 use vars qw/@ISA $AUTOLOAD $VERSION $OpenSRF::Utils::ConfigCache/;
87 push @ISA, qw/OpenSRF::Utils/;
90 use OpenSRF::Utils (':common');
91 use OpenSRF::Utils::Logger;
93 #use overload '""' => \&OpenSRF::Utils::Config::dump_ini;
97 my $config_file = shift;
99 return unless $config_file;
101 $class->load( config_file => $config_file);
109 if ($self->isa('OpenSRF::Utils::Config')) {
110 if (UNIVERSAL::isa(scalar(caller()), 'OpenSRF::Utils::Config' )) {
113 $string = "# Main File: " . $self->FILE . "\n\n" . $string;
116 for my $section ( ('__id', grep { $_ ne '__id' } sort keys %$self) ) {
117 next if ($section eq 'env' && $self->isa('OpenSRF::Utils::Config'));
118 if ($section eq '__id') {
119 $string .= '['.$self->SECTION."]\n" if ($self->isa('OpenSRF::Utils::Config::Section'));
120 } elsif (ref($self->$section)) {
121 if (ref($self->$section) =~ /ARRAY/o) {
122 $string .= "list:$section = ". join(', ', @{$self->$section}) . "\n";
123 } elsif (UNIVERSAL::isa($self->$section,'OpenSRF::Utils::Config::Section')) {
124 if ($self->isa('OpenSRF::Utils::Config::Section')) {
125 $string .= "subsection:$section = " . $self->$section->SECTION . "\n";
128 next if ($self->$section->{__sub} && !$included);
129 $string .= $self->$section . "\n";
131 } elsif (UNIVERSAL::isa($self->$section,'OpenSRF::Utils::Config')) {
132 $string .= $self->$section . "\n";
135 next if $section eq '__sub';
136 $string .= "$section = " . $self->$section . "\n";
140 $string =~ s/^/## /gm;
141 $string = "# Subfile: " . $self->FILE . "\n#" . '-'x79 . "\n".'#include "'.$self->FILE."\"\n". $string;
149 OpenSRF::Utils::Config
155 use OpenSRF::Utils::Config;
157 my $config_obj = OpenSRF::Utils::Config->load( config_file => '/config/file.cnf' );
159 my $attrs_href = $config_obj->attributes();
161 $config_obj->attributes->no_db(0);
163 open FH, '>'.$config_obj->FILE() . '.new';
164 print FH $config_obj;
172 This module is mainly used by other modules to load a configuration file.
178 Hashrefs of sections can be returned by calling a method of the object of the same name as the section.
179 They can be set by passing a hashref back to the same method. Sections will B<NOT> be autovivicated, though.
181 Here be a config file example, HAR!:
186 subsection:definition=devel_db
189 dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl;host=nsite-dev
195 dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl
201 subsection:definition=devel_rpc
213 url=https://localhost:9000/
218 SSL_key_file=client-key.pem
219 SSL_cert_file=client-cert.pem
220 SSL_ca_file=cacert.pem
224 base_dir=/home/miker/cvs/NOC/monitor_core/
234 $VERSION = do { my @r=(q$Revision$=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
237 =head2 OpenSRF::Utils::Config->load( config_file => '/some/config/file.cnf' )
239 Returns a OpenSRF::Utils::Config object representing the config file that was loaded.
240 The most recently loaded config file (hopefully the only one per app)
241 is stored at $OpenSRF::Utils::ConfigCache. Use OpenSRF::Utils::Config::current() to get at it.
248 $pkg = ref($pkg) || $pkg;
252 (my $new_pkg = $args{config_file}) =~ s/\W+/_/g;
253 $new_pkg .= "::$pkg";
254 $new_section_pkg .= "${new_pkg}::Section";
260 sub section_pkg { return '$new_section_pkg'; }
262 package $new_section_pkg;
263 use base "${pkg}::Section";
268 return $new_pkg->_load( %args );
273 $pkg = ref($pkg) || $pkg;
278 if ((exists $$self{config_file} and OpenSRF::Utils::Config->current) and (OpenSRF::Utils::Config->current->FILE eq $$self{config_file}) and (!$self->{force})) {
279 delete $$self{force};
280 return OpenSRF::Utils::Config->current();
283 $self->_sub_builder('__id');
284 $self->FILE($$self{config_file});
285 delete $$self{config_file};
286 return undef unless ($self->FILE);
288 $self->load_config();
290 $self->mangle_dirs();
291 $self->mangle_logs();
293 $OpenSRF::Utils::ConfigCache = $self unless $self->nocache;
294 delete $$self{nocache};
295 delete $$self{force};
303 my @parts = (grep { UNIVERSAL::isa($_,'OpenSRF::Utils::Config::Section') } values %$self);
305 my $must_match = scalar(keys %filters);
307 foreach my $part (@parts) {
309 for my $fkey (keys %filters) {
310 $part_count++ if ($part->$key eq $filters{$key});
312 push @ok_parts, $part if ($part_count == $must_match);
320 return $OpenSRF::Utils::ConfigCache;
324 return shift()->__id(@_);
329 my $host = hostfqdn();
331 $$self{env} = $self->section_pkg->new;
332 $$self{env}{hostname} = $host;
337 return unless ($self->logs && $self->dirs && $self->dirs->log_dir);
338 for my $i ( keys %{$self->logs} ) {
339 next if ($self->logs->$i =~ /^\//);
340 $self->logs->$i($self->dirs->log_dir."/".$self->logs->$i);
346 return unless ($self->dirs && $self->dirs->base_dir);
347 for my $i ( keys %{$self->dirs} ) {
348 if ( $i ne 'base_dir' ) {
349 next if ($self->dirs->$i =~ /^\//);
350 my $dir_tmp = $self->dirs->base_dir."/".$self->dirs->$i;
351 $dir_tmp =~ s#//#/#go;
352 $dir_tmp =~ s#/$##go;
353 $self->dirs->$i($dir_tmp);
360 my $config = new FileHandle $self->FILE, 'r';
362 OpenSRF::Utils::Logger->error("Could not open ".$self->FILE.": $!\n");
363 die "Could not open ".$self->FILE.": $!\n";
365 my @stripped_config = $self->__strip_comments($config) if (defined $config);
368 for my $line (@stripped_config) {
372 if ($line =~ /^\s*\[/ and @$chunk) {
373 my $section = $self->section_pkg->new($chunk);
375 my $sub_name = $section->SECTION;
376 $self->_sub_builder($sub_name);
377 $self->$sub_name($section);
379 #$self->{$section->SECTION} = $section;
385 if ($line =~ /^#\s*include\s+"(\S+)"\s*$/o) {
386 my $included_file = $1;
387 my $section = OpenSRF::Utils::Config->load(config_file => $included_file, nocache => 1);
389 my $sub_name = $section->FILE;
390 $self->_sub_builder($sub_name);
391 $self->$sub_name($section);
393 for my $subsect (keys %$section) {
394 next if ($subsect eq '__id');
396 $self->_sub_builder($subsect);
397 $self->$subsect($$section{$subsect});
399 #$self->$subsect($section->$subsect);
400 $self->$subsect->{__sub} = 1;
407 my $section = $self->section_pkg->new($chunk) if (@$chunk);
408 my $sub_name = $section->SECTION;
409 $self->_sub_builder($sub_name);
410 $self->$sub_name($section);
415 #------------------------------------------------------------------------------------------------------------------------------------
423 No know bugs, but report any to miker@purplefrog.com.
425 =head1 COPYRIGHT AND LICENSING
427 Mike Rylander, Copyright 2000-2004
429 The OpenSRF::Utils::Config module is free software. You may distribute under the terms
430 of the GNU General Public License version 2 or greater.