Config.pm 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::Utils::Config

=head1 DESCRIPTION

    A parser for JSON-based configuration files mainly used in scheduling and graph generation.

=head1 LICENSE

13
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
14
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
15 16 17 18 19 20 21 22 23 24 25 26

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

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 30 31

=cut


32 33 34 35
package Bio::EnsEMBL::Hive::Utils::Config;

use JSON;

36 37
sub default_config_files {  # a class method, returns a list

38
    my $system_config   = $ENV{'EHIVE_ROOT_DIR'}.'/hive_config.json';
39 40 41 42 43 44
    my $user_config     = $ENV{'HOME'}.'/.hive_config.json';

    return ($system_config, (-r $user_config) ? ($user_config) : ());
}


45 46 47 48 49 50
sub new {
    my $class = shift @_;

    my $self = bless {}, $class;
    $self->config_hash( {} );

51
    foreach my $cfg_file ( scalar(@_) ? @_ : $self->default_config_files ) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        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;
}


Leo Gordon's avatar
Leo Gordon committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
sub set {
    my $self        = shift @_;
    my $value       = pop @_;
    my $key         = pop @_;

    my $hash_ptr    = $self->config_hash;

    foreach my $context_syll (@_) {
        unless(exists $hash_ptr->{$context_syll}) {
            $hash_ptr->{$context_syll} = {};
        }
        $hash_ptr = $hash_ptr->{$context_syll};
    }

    if(ref($hash_ptr->{$key}) ne ref($value)) {
        die "Mismatch of types in Config::set(".join(',',@_,$key,$value).") : trying to set a ".(ref($value)||'scalar')." instead of ".ref($hash_ptr->{$key});
    } else {
        $hash_ptr->{$key} = $value;
    }
}

1;