[OPEN-ILS-DEV] PATCH: Throw away bootstrap.conf and use opensrf_core.xml

Dan Scott denials at gmail.com
Thu Jun 28 00:02:28 EDT 2007


On 27/06/07, Mike Rylander <mrylander at gmail.com> wrote:
> On 6/27/07, Dan Scott <denials at gmail.com> wrote:
> > Building on Nathan Eady's suggestion / intention from December
>
> Per discussion (and Dan's suggestion) in IRC, we're holding off on
> this patch for further refinement of the in-module documentation (and
> a DCO).

Now featuring up-to-date perldoc _AND_ a DCO. Still not sure this is
exactly what you want technically, though. The major limitation is
that I've hardcoded /config/opensrf to appear as the 'bootstrap'
section of config for compatibility with the expectations of the rest
of OpenSRF. A broader patch would convert all of those other calls
from config->bootstrap->blah to config->opensrf->blah and make the
siblings of /config/opensrf visible in the config as well, as a more
generally useful approach.

My belief at this point however is that this patch serves the present
goal of eliminating the need for bootsrap.conf; I can always expand
this later.

So... DCO time:

Developer's Certificate of Origin 1.1 By making a contribution to this
project, I certify that:

(a) The contribution was created in whole or in part by me and I have
the right to submit it under the open source license indicated in the
file; or

(b) The contribution is based upon previous work that, to the best of
my knowledge, is covered under an appropriate open source license and
I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
same open source license (unless I am permitted to submit under a
different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person
who certified (a), (b) or (c) and I have not modified it; and

(d) In the case of each of (a), (b), or (c), I understand and agree
that this project and the contribution are public and that a record of
the contribution (including all personal information I submit with it,
including my sign-off) is maintained indefinitely and may be
redistributed consistent with this project or the open source license
indicated in the file.

-- 
Dan Scott
Laurentian University
-------------- next part --------------
Index: perlmods/OpenSRF/Utils/Config.pm
===================================================================
--- perlmods/OpenSRF/Utils/Config.pm	(revision 976)
+++ perlmods/OpenSRF/Utils/Config.pm	(working copy)
@@ -26,58 +26,22 @@
 
 	$self = bless {}, $class;
 
-	my $lines = shift;
+	$self->_sub_builder('__id');
+	# Hard-code this to match old bootstrap.conf section name
+	$self->__id('bootstrap');
 
-	for my $line (@$lines) {
+	my $bootstrap = shift;
 
-		#($line) = split(/\s+\/\//, $line);
-		#($line) = split(/\s+#/, $line);
-
-		if ($line =~ /^\s*\[([^\[\]]+)\]/) {
-			$self->_sub_builder('__id');
-			$self->__id( $1 );
-			next;
+	foreach my $key (sort keys %$bootstrap) {
+		$self->_sub_builder($key);
+		if (ref($key) eq 'ARRAY') {
+			$self->$key([$bootstrap->{$key}]);
 		}
-
-		my ($protokey,$value,$keytype,$key);
-		if ($line =~ /^([^=\s]+)\s*=\s*(.*)\s*$/s) {
-			($protokey,$value) = ($1,$2);
-			($keytype,$key) = split(/:/,$protokey);
+		else {
+			$self->$key($bootstrap->{$key});
 		}
-
-		$key = $protokey unless ($key);
-
-		if ($keytype ne $key) {
-			$keytype = lc $keytype;
-			if ($keytype eq 'list') {
-				$value = [split /\s*,\s*/, $value];
-			} elsif ($keytype eq 'bool') {
-				$value = do{ $value =~ /^t|y|1/i ? 1 : 0; };
-			} elsif ($keytype eq 'interval') {
-				$value = interval_to_seconds($value);
-			} elsif ($keytype eq 'subsection') {
-				if (exists $SECTIONCACHE{$value}) {
-					$value = $SECTIONCACHE{$value};
-				} else {
-					$SUBSECTION_FIXUP{$value}{$self->SECTION} = $key ;
-					next;
-				}
-			}
-		}
-
-		$self->_sub_builder($key);
-		$self->$key($value);
 	}
 
-	no warnings;
-	if (my $parent_def = $SUBSECTION_FIXUP{$self->SECTION}) {
-		my ($parent_section, $parent_key) = each %$parent_def;
-		$SECTIONCACHE{$parent_section}->{$parent_key} = $self;
-		delete $SUBSECTION_FIXUP{$self->SECTION};
-	}
-
-	$SECTIONCACHE{$self->SECTION} = $self;
-
 	return $self;
 }
 
@@ -87,6 +51,7 @@
 push @ISA, qw/OpenSRF::Utils/;
 
 use FileHandle;
+use XML::LibXML;
 use OpenSRF::Utils (':common');  
 use OpenSRF::Utils::Logger;
 use Net::Domain qw/hostfqdn/;
@@ -157,9 +122,9 @@
 
   my $config_obj = OpenSRF::Utils::Config->load( config_file   => '/config/file.cnf' );
 
-  my $attrs_href = $config_obj->attributes();
+  my $attrs_href = $config_obj->bootstrap();
 
-  $config_obj->attributes->no_db(0);
+  $config_obj->bootstrap->loglevel(0);
 
   open FH, '>'.$config_obj->FILE() . '.new';
   print FH $config_obj;
@@ -170,62 +135,57 @@
 =head1 DESCRIPTION
 
  
-This module is mainly used by other modules to load a configuration file.
- 
+This module is mainly used by other OpenSRF modules to load an OpenSRF configuration file.
+OpenSRF configuration files are XML files that contain a C<< <config> >> root element and an C<< <opensrf> >>
+child element (in XPath notation, C</config/opensrf/>). Each child element is converted into a
+hash key=>value pair. Elements that contain other XML elements are pushed into arrays and added
+as an array reference to the hash. Scalar values have whitespace trimmed from the left and right
+sides.
 
-=head1 NOTES
+Child elements of C<< <config> >> other than C<< <opensrf> >> are currently ignored by this module.
 
+=head1 EXAMPLE
  
-Hashrefs of sections can be returned by calling a method of the object of the same name as the section.
-They can be set by passing a hashref back to the same method.  Sections will B<NOT> be autovivicated, though.
+Given an OpenSRF configuration file named F<opensrf_core.xml> with the following content:
 
-Here be a config file example, HAR!:
+  <?xml version='1.0'?>
+  <config>
+    <opensrf>
+      <router_name>router</router_name>
 
- [datasource]
- # backend=XMLRPC
- backend=DBI
- subsection:definition=devel_db
+      <routers> 
+	<router>localhost</router>
+	<router>otherhost</router>
+      </routers>
 
- [devel_db]
- dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl;host=nsite-dev
- user=postgres
- pw=postgres
- #readonly=1
- 
- [live_db]
- dsn=dbi:Pg(RaiseError => 0, AutoCommit => 1):dbname=dcl
- user=n2dcl
- pw=dclserver
- #readonly=1
+      <logfile>/openils/var/log/osrfsys.log</logfile>
+    </opensrf>
+  </config>
 
- [devel_xmlrpc]
- subsection:definition=devel_rpc
- 
- [logs]
- base=/var/log/nsite
- debug=debug.log
- error=error.log
- 
- [debug]
- enabled=1
- level=ALL
- 
- [devel_rpc]
- url=https://localhost:9000/
- proto=SSL
- SSL_cipher_list=ALL
- SSL_verify_mode=5
- SSL_use_cert=1
- SSL_key_file=client-key.pem
- SSL_cert_file=client-cert.pem
- SSL_ca_file=cacert.pem
- log_level=4
- 
- [dirs]
- base_dir=/home/miker/cvs/NOC/monitor_core/
- cert_dir=certs/
- 
+... calling C<< OpenSRF::Utils::Config->load(config_file => 'opensrf_core.xml') >> will create a hash
+with the following structure:
 
+  {
+    router_name => 'router',
+    routers => ['localhost', 'otherhost'],
+    logfile => '/openils/var/log/osrfsys.log'
+  }
+
+You can retrieve any of these values by name from the bootstrap section of C<$config_obj>; for example:
+
+  $config_obj->bootstrap->router_name
+
+=head1 NOTES
+
+For compatibility with a previous version of OpenSRF configuration files, the F</config/opensrf/> section
+has a hardcoded name of B<bootstrap>. However, future iterations of this module may extend the ability
+of the module to parse the entire OpenSRF configuration file and provide sections named after the sibling
+elements of C</config/opensrf>.
+
+Hashrefs of sections can be returned by calling a method of the object of the same name as the section.
+They can be set by passing a hashref back to the same method.  Sections will B<NOT> be autovivicated, though.
+
+
 =head1 METHODS
 
 
@@ -358,60 +318,52 @@
 
 sub load_config {
 	my $self = shift;
-	my $config = new FileHandle $self->FILE, 'r';
+	my $parser = XML::LibXML->new();
+
+	# Hash of config values
+	my %bootstrap;
+	
+	# Return an XML::LibXML::Document object
+	my $config = $parser->parse_file($self->FILE);
+
 	unless ($config) {
 		OpenSRF::Utils::Logger->error("Could not open ".$self->FILE.": $!\n");
 		die "Could not open ".$self->FILE.": $!\n";
 	}
-	my @stripped_config = $self->__strip_comments($config) if (defined $config);
 
-	my $chunk = [];
-	for my $line (@stripped_config) {
-		no warnings;
-		next unless ($line);
+	# Return an XML::LibXML::NodeList object matching all child elements
+	# of <config><opensrf>...
+	my $osrf_cfg = $config->findnodes('/config/opensrf/child::*');
 
-		if ($line =~ /^\s*\[/ and @$chunk) {
-			my $section = $self->section_pkg->new($chunk);
+	# Iterate through the nodes to pull out key=>value pairs of config settings
+	foreach my $node ($osrf_cfg->get_nodelist()) {
+		my $child_state = 0;
+		foreach my $child_node ($node->childNodes) {
+			# from libxml/tree.h: nodeType 1 = ELEMENT_NODE
+			next if $child_node->nodeType() != 1;
 
-			my $sub_name = $section->SECTION;
-			$self->_sub_builder($sub_name);
-			$self->$sub_name($section);
-
-			#$self->{$section->SECTION} = $section;
-
-			$chunk = [];
-			push @$chunk,$line;
-			next;
-		} 
-		if ($line =~ /^#\s*include\s+"(\S+)"\s*$/o) {
-                        my $included_file = $1;
-			my $section = OpenSRF::Utils::Config->load(config_file => $included_file, nocache => 1);
-
-			my $sub_name = $section->FILE;
-			$self->_sub_builder($sub_name);
-			$self->$sub_name($section);
-
-			for my $subsect (keys %$section) {
-				next if ($subsect eq '__id');
-
-				$self->_sub_builder($subsect);
-				$self->$subsect($$section{$subsect});
-
-				#$self->$subsect($section->$subsect);
-				$self->$subsect->{__sub} = 1;
-			}
-			next;
+			# If the child node is an element, this element may
+			# have multiple values; therefore, push it into an array
+			push @{$bootstrap{$node->nodeName()}}, OpenSRF::Utils::Config::extract_text($child_node->textContent);
+			$child_state = 1;
 		}
-
-		push @$chunk,$line;
+		if (!$child_state) {
+			$bootstrap{$node->nodeName()} = OpenSRF::Utils::Config::extract_text($node->textContent);
+		}
 	}
-	my $section = $self->section_pkg->new($chunk) if (@$chunk);
+
+	my $section = $self->section_pkg->new(\%bootstrap);
 	my $sub_name = $section->SECTION;
 	$self->_sub_builder($sub_name);
 	$self->$sub_name($section);
 
 }
 
+sub extract_text {
+	my $self = shift;
+	$self =~ s/^\s*([.*?])\s*$//m;
+	return $self;
+}
 
 #------------------------------------------------------------------------------------------------------------------------------------
 
@@ -419,13 +371,25 @@
 
 	OpenSRF::Utils
 
+=head1 LIMITATIONS
+
+Elements containing heterogeneous child elements are treated as though they have the same element name;
+for example:
+  <routers>
+    <router>localhost</router>
+    <furniture>chair</furniture>
+  </routers>
+
+... will simply generate a key=>value pair of C<< routers => ['localhost', 'chair'] >>.
+
 =head1 BUGS
 
-No know bugs, but report any to mrylander at gmail.com.
+No known bugs, but report any to mrylander at gmail.com.
 
 =head1 COPYRIGHT AND LICENSING
 
 Mike Rylander, Copyright 2000-2007
+Copyright (C) 2007, Laurentian University, Dan Scott <dscott at laurentian.ca>
 
 The OpenSRF::Utils::Config module is free software. You may distribute under the terms
 of the GNU General Public License version 2 or greater.
Index: perlmods/OpenSRF/Utils/Logger.pm
===================================================================
--- perlmods/OpenSRF/Utils/Logger.pm	(revision 932)
+++ perlmods/OpenSRF/Utils/Logger.pm	(working copy)
@@ -39,7 +39,6 @@
 my $act_syslog_enabled = 0;	# is syslog enabled?
 my $logfile_enabled = 1;		# are we logging to a file?
 my $act_logfile_enabled = 1;	# are we logging to a file?
-my $logdir;							# log file directory
 
 our $logger = "OpenSRF::Utils::Logger";
 
@@ -66,16 +65,14 @@
 		warn "*** Logger found no config.  Using STDERR ***\n";
 	}
 
-	$loglevel =  $config->bootstrap->debug; 
-	if($loglevel =~ /error/i){ $loglevel = ERROR(); }
-	elsif($loglevel =~ /warn/i){ $loglevel = WARN(); }
-	elsif($loglevel =~ /info/i){ $loglevel = INFO(); }
-	elsif($loglevel =~ /debug/i){ $loglevel = DEBUG(); }
-	elsif($loglevel =~ /internal/i){ $loglevel = INTERNAL(); }
+	$loglevel =  $config->bootstrap->loglevel; 
+	if($loglevel = 1){ $loglevel = ERROR(); }
+	elsif($loglevel = 2){ $loglevel = WARN(); }
+	elsif($loglevel = 3){ $loglevel = INFO(); }
+	elsif($loglevel = 4){ $loglevel = DEBUG(); }
+	elsif($loglevel = 5){ $loglevel = INTERNAL(); }
 	else{$loglevel= INFO(); }
 
-	my $logdir = $config->bootstrap->log_dir;
-
 	$logfile = $config->bootstrap->logfile;
 	if($logfile =~ /^syslog/) {
 		$syslog_enabled = 1;
@@ -86,7 +83,7 @@
 		$facility = _fac_to_const($facility);
 		openlog($service, 0, $facility);
 
-	} else { $logfile = "$logdir/$logfile"; }
+	} else { $logfile = "$logfile"; }
 
 	$actfile = $config->bootstrap->actlog;
 	if($actfile =~ /^syslog/) {
@@ -97,7 +94,7 @@
 		$actfile = undef;
 		$actfac = _fac_to_const($actfac);
 
-	} else { $actfile = "$logdir/$actfile"; }
+	} else { $actfile = "$actfile"; }
 
 	$isclient = (OpenSRF::Utils::Config->current->bootstrap->client =~ /^true$/iog) ?  1 : 0;
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Config.pm
Type: text/x-perl
Size: 10395 bytes
Desc: not available
Url : http://list.georgialibraries.org/pipermail/open-ils-dev/attachments/20070628/39bcaebc/Config-0001.bin


More information about the Open-ils-dev mailing list