Commit d6bb2bf4 authored by Leo Gordon's avatar Leo Gordon
Browse files

a new JSON-based configuration file and parser

parent c4e7aecf
{
"Graph" : {
"Colours" : {
"Status" : {
"BLOCKED" : "grey",
"LOADING" : "yellow",
"SYNCHING" : "yellow",
"READY" : "yellow",
"WORKING" : "yellow",
"ALL_CLAIMED" : "yellow",
"DONE" : "green",
"FAILED" : "red",
"OTHER" : "white",
"TABLE" : "black",
},
"Flows" : {
"data" : "blue",
"control" : "red",
"semablock" : "red",
},
},
"Fonts" : {
"node" : "Helvetica",
"edge" : "Helvetica",
},
"DisplayDetails" : 1,
"DisplaySemaphoreBoxes" : 1,
"DisplayStretched" : 0,
}
}
#!/usr/bin/env perl
package Bio::EnsEMBL::Hive::Utils::Config;
use JSON;
sub new {
my $class = shift @_;
my $self = bless {}, $class;
$self->config_hash( {} );
foreach my $cfg_file (@_) {
if(my $cfg_hash = $self->load_from_json($cfg_file)) {
$self->merge($cfg_hash);
}
}
return $self;
}
sub config_hash {
my $self = shift @_;
if(@_) {
$self->{_config_hash} = shift @_;
}
return $self->{_config_hash};
}
sub load_from_json {
my ($self, $filename) = @_;
if(-r $filename) {
my $json_text = `cat $filename`;
my $json_parser = JSON->new->relaxed;
my $perl_hash = $json_parser->decode($json_text);
return $perl_hash;
} else {
warn "Can't read from '$filename'";
return undef;
}
}
sub merge {
my $self = shift @_;
my $from = shift @_;
my $to = shift @_ || $self->config_hash; # only defined in subsequent recursion steps
while(my ($key,$value) = each %$from) {
if(exists $to->{$key} and ref($to->{$key})) {
$self->merge($from->{$key}, $to->{$key});
} else {
$to->{$key} = $from->{$key};
}
}
}
sub get {
my $self = shift @_;
my $option_name = pop @_;
my $hash_ptr = $self->config_hash;
my $option_value = $hash_ptr->{$option_name}; # not necessatily defined
foreach my $context_syll (@_) {
$hash_ptr = $hash_ptr->{$context_syll};
if(exists $hash_ptr->{$option_name}) {
$option_value = $hash_ptr->{$option_name};
}
}
return $option_value;
}
1;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment