GRNOC-Config
view release on metacpan or search on metacpan
etc/bad-xml.xml view on Meta::CPAN
<config>
<db name='somedb'>
<credentials user='readonly' password='password' />
<host address='127.0.0.1' port='3306' />
</dbasdfasdf>
<!-- <something>else</something> -->
<something>else again</something>
<yet>again</yet1>
</config>
etc/example.xml view on Meta::CPAN
<?xml version="1.0"?>
<config>
<db name="Test">
<credentials user="readonly" password="password"/>
<host address="127.0.0.1" port="3306"/>
</db>
<tester>this is</tester>
<tester>an array</tester>
<yet>again</yet>
</config>
etc/example_schema.xsd view on Meta::CPAN
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="credentialsType">
<xs:attribute name="user" type="xs:string" use="required" />
<xs:attribute name="password" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="hostType">
<xs:attribute name="address" type="xs:string" use="required" />
<xs:attribute name="port" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="dbInfoType">
<xs:sequence>
<xs:element name="credentials" type="credentialsType" />
<xs:element name="host" type="hostType" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional" />
</xs:complexType>
<xs:complexType name="configType">
<xs:sequence>
<xs:element name="db" type="dbInfoType" />
<xs:element name="something" type="xs:string" maxOccurs="unbounded" minOccurs="0"/>
<xs:element name="tester" type="xs:string" maxOccurs="unbounded"/>
lib/GRNOC/Config.pm view on Meta::CPAN
Uses XML::XPath and XML::Simple to parse our XML files, and stores all configs it has access to in this module
Setting debug to true will give you extra debugging output (default is off)
Setting force_array to true will return everything in an array even if there is only 1 object returned (default is on)
When asking for attributes denoted by '@' it will return only the attribute(s) without the hash
use GRNOC::Config;
my $foo = GRNOC::Config->new(config_file => '/etc/grnoc/db.xml', debug => 0, force_array => 0 schema => '/path/to/schema.csd')
my $db_username = $foo->get("/config/db/credentials");
print $db_username->{'user'};
print $db_username->{'password'};
#just 1 attribute
my $user = $foo->get("/config/db/credentials[1]/@user");
my $password = $foo->get("/config/db/credentials[1]/@password");
#if I have more than 1 result I get an array
my $hosts = $foo->get("/config/hosts") or die Dumper($foo->get_error());
foreach my $host ($hosts){
print $host->{'hostname'};
}
##turn debugging on
$foo->{'debug'} = 1;
##get errors
print Dumper($foo->get_errors());
# I always want an array... even if I only get 1 result
$foo->{'force_array'} = 1;
$db_username = $foo->get("/config/db/credentials") or die Dumper($foo->get_error());
print @{db_username}[0]->{'user'}
print @{db_username}[0]->{'password'}
$user = $foo->get("/config/db/credentials[1]/@user") or die Dumper($foo->get_error());
$password = $foo->get("/config/db/credentials[1]/@password") or die Dumper($foo->get_error());
$user = @{$user}[0];
$password = @{$password}[0];
my $valid = $foo->validate();
if(!$valid){
print STDERR Dumper($foo->get_error());
}
my $valid2 = $foo->validate("/path/to/new/schema.xsd");
t/functional.t view on Meta::CPAN
use strict;
use Test::Simple tests=>19;
use Data::Dumper;
use GRNOC::Config;
my $config = GRNOC::Config->new(config_file => "etc/example.xml", force_array => 0);
ok(defined $config, "load config");
my $result = $config->get("/config/db/credentials");
ok(defined $result, "Retrieved Config");
ok($result->{'user'} eq 'readonly');
ok($result->{'password'} eq 'password');
my $result2 = $config->get2("/config/db/credentials");
ok(defined $result2, "Retreived Config");
my $bad = $config->get("/config/db/test");
ok(!(defined($bad)), "Bad path graceful");
my $error = $config->get_error();
ok($error->{'msg'} eq '/config/db/test does not exist in the config', "return an error if path does not exist");
my $user = $config->get('/config/db/credentials[1]/@user');
ok(defined $user, "user defined");
ok($user eq 'readonly', "get the user");
my $text = $config->get('/config/yet');
ok(defined $text, "text defined");
ok($text eq 'again', "text check");
#turn on force array
$config->{'force_array'} = 1;
my $user_array = $config->get('/config/db/credentials[1]/@user');
ok(defined $user_array, "user_array defined");
ok(@{$user_array}[0] eq 'readonly', "get the user in the array");
my $creds = $config->get('/config/db/credentials');
ok(defined $creds, "creds defined");
ok(@{$creds}[0]->{'user'} eq 'readonly', "user check");
ok(@{$creds}[0]->{'password'} eq 'password', "password check");
my $textarr = $config->get('/config/tester');
ok(defined $textarr, "array text defined");
ok(@{$textarr}[0] eq 'this is', "array text check");
ok(@{$textarr}[1] eq 'an array', "array text check");
( run in 0.268 second using v1.01-cache-2.11-cpan-4d50c553e7e )