ensembl-hive  2.6
Config.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 DESCRIPTION
8 
9  A parser for JSON-based configuration files mainly used in scheduling and graph generation.
10 
11 =head1 LICENSE
12 
13  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
14  Copyright [2016-2024] EMBL-European Bioinformatics Institute
15 
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
18 
19  http://www.apache.org/licenses/LICENSE-2.0
20 
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.
24 
25 =head1 CONTACT
26 
27  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
28 
29 =cut
30 
31 
32 package Bio::EnsEMBL::Hive::Utils::Config;
33 
34 use strict;
35 use warnings;
36 use JSON;
37 use Bio::EnsEMBL::Hive::Utils ('throw');
38 
39 
40 sub default_system_config {
41  my $ehive_root_dir = $ENV{'EHIVE_ROOT_DIR'}
42  or throw("ENV{EHIVE_ROOT_DIR} is not defined");
43 
44  return $ehive_root_dir.'/hive_config.json';
45 }
46 
47 
48 sub default_user_config {
49  return $ENV{'HOME'}.'/.hive_config.json';
50 }
51 
52 
53 sub default_config_files { # a class method, returns a list
54  my $self = shift @_;
55 
56  my $system_config = $self->default_system_config;
57  my $user_config = $self->default_user_config;
58 
59  return ($system_config, (-r $user_config) ? ($user_config) : ());
60 }
61 
62 
63 sub new {
64  my $class = shift @_;
65 
66  my $self = bless {}, $class;
67  $self->config_hash( {} );
68 
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);
72  }
73  }
74 
75  return $self;
76 }
77 
78 
79 sub config_hash {
80  my $self = shift @_;
81 
82  if(@_) {
83  $self->{_config_hash} = shift @_;
84  }
85  return $self->{_config_hash};
86 }
87 
88 
89 sub load_from_json {
90  my ($self, $filename) = @_;
91 
92  if(-r $filename) {
93  my $json_text = `cat $filename`;
94  my $json_parser = JSON->new->relaxed;
95  my $perl_hash = $json_parser->decode($json_text);
96 
97  return $perl_hash;
98  } else {
99  warn "Can't read from '$filename'. Configuration file not loaded\n";
100 
101  return undef;
102  }
103 }
104 
105 
106 sub merge {
107  my $self = shift @_;
108  my $from = shift @_;
109  my $to = shift @_ || $self->config_hash; # only defined in subsequent recursion steps
110 
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});
115  } else {
116  $to->{$key} = $from->{$key};
117  }
118  }
119  }
120 }
121 
122 
123 sub get {
124  my $self = shift @_;
125  my $option_name = pop @_;
126 
127  my $hash_ptr = $self->config_hash;
128  my $option_value = $hash_ptr->{$option_name}; # not necessatily defined
129 
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};
134  }
135  }
136 
137  return $option_value;
138 }
139 
140 
141 sub set {
142  my $self = shift @_;
143  my $value = pop @_;
144  my $key = pop @_;
145 
146  my $hash_ptr = $self->config_hash;
147 
148  foreach my $context_syll (@_) {
149  unless(exists $hash_ptr->{$context_syll}) {
150  $hash_ptr->{$context_syll} = {};
151  }
152  $hash_ptr = $hash_ptr->{$context_syll};
153  }
154 
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});
157  } else {
158  $hash_ptr->{$key} = $value;
159  }
160 }
161 
162 1;
Bio::EnsEMBL::Hive::Utils
Definition: Collection.pm:4
Bio::EnsEMBL::Hive::Utils::Config
Definition: Config.pm:12
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive
Definition: Hive.pm:38