9 A parser
for JSON-based configuration files mainly used in scheduling and graph generation.
13 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
14 Copyright [2016-2024] EMBL-European Bioinformatics Institute
16 Licensed under the Apache License,
Version 2.0 (the
"License"); you may not use
this file except in compliance with the License.
17 You may obtain a copy of the License at
21 Unless required by applicable law or agreed to in writing, software distributed under the License
22 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License
for the specific language governing permissions and limitations under the License.
27 Please subscribe to the
Hive mailing list: http:
32 package Bio::EnsEMBL::Hive::Utils::Config;
40 sub default_system_config {
41 my $ehive_root_dir = $ENV{
'EHIVE_ROOT_DIR'}
42 or
throw(
"ENV{EHIVE_ROOT_DIR} is not defined");
44 return $ehive_root_dir.
'/hive_config.json';
48 sub default_user_config {
49 return $ENV{
'HOME'}.
'/.hive_config.json';
53 sub default_config_files { # a
class method, returns a list
56 my $system_config = $self->default_system_config;
57 my $user_config = $self->default_user_config;
59 return ($system_config, (-r $user_config) ? ($user_config) : ());
66 my $self = bless {}, $class;
67 $self->config_hash( {} );
69 foreach my $cfg_file ( scalar(@_) ? @_ : $self->default_config_files ) {
70 if(my $cfg_hash = $self->load_from_json($cfg_file)) {
71 $self->merge($cfg_hash);
83 $self->{_config_hash} = shift @_;
85 return $self->{_config_hash};
90 my ($self, $filename) = @_;
93 my $json_text = `cat $filename`;
94 my $json_parser = JSON->new->relaxed;
95 my $perl_hash = $json_parser->decode($json_text);
99 warn
"Can't read from '$filename'. Configuration file not loaded\n";
109 my $to = shift @_ || $self->config_hash; # only defined in subsequent recursion steps
111 if(ref($from) eq
'HASH') { # FIXME: currently we don
't merge ARRAY references (the only example we currently have is REMARK), only the HASH references
112 while(my ($key,$value) = each %$from) {
113 if(exists $to->{$key} and ref($to->{$key})) {
114 $self->merge($from->{$key}, $to->{$key});
116 $to->{$key} = $from->{$key};
125 my $option_name = pop @_;
127 my $hash_ptr = $self->config_hash;
128 my $option_value = $hash_ptr->{$option_name}; # not necessatily defined
130 foreach my $context_syll (@_) {
131 $hash_ptr = $hash_ptr->{$context_syll};
132 if(exists $hash_ptr->{$option_name}) {
133 $option_value = $hash_ptr->{$option_name};
137 return $option_value;
146 my $hash_ptr = $self->config_hash;
148 foreach my $context_syll (@_) {
149 unless(exists $hash_ptr->{$context_syll}) {
150 $hash_ptr->{$context_syll} = {};
152 $hash_ptr = $hash_ptr->{$context_syll};
155 if(ref($hash_ptr->{$key}) ne ref($value)) {
156 die "Mismatch of types in Config::set(".join(',
',@_,$key,$value).") : trying to set a ".(ref($value)||'scalar
')." instead of ".ref($hash_ptr->{$key});
158 $hash_ptr->{$key} = $value;