File: Config.pm

package info (click to toggle)
libchado-perl 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,976 kB
  • ctags: 10,378
  • sloc: xml: 192,540; sql: 165,945; perl: 28,339; sh: 101; python: 73; makefile: 46
file content (127 lines) | stat: -rw-r--r-- 2,579 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
121
122
123
124
125
126
127
package Bio::Chado::Config;

=head1 NAME

Chado::Config

=head1 SYNOPSIS

  my $config  = Bio::Chado::Config->new;
  my $db_name = $config->{'database'}{'db_name'};

=head1 DESCRIPTION

This module is a simple interface to the Chado installation 
configuration information.

=head1 METHODS

=cut

use strict;
use Carp;
use FindBin '$Bin';
use File::Spec::Functions;
use XML::Simple;

use constant DEFAULT_FILENAME => catfile( $Bin, 'load', 'etc', 'load.conf' );

# ---------------------------------------------------------
=pod

=head2 new

Instantiates a new object.  Takes an optional (but preferred) 
argument of the configuration file's path.  The filename should be 
indicated as a key/value pair, but, if only one argument is passed
in, it is assumed to be the filename.

  my $conf = Bio::Chado::Config->new;
  my $conf = Bio::Chado::Config->new( <filename> );
  my $conf = Bio::Chado::Config->new( filename => <filename> );

=cut

sub new {
    my $class = shift;
    my $args  = defined $_[0] && UNIVERSAL::isa( $_[0], 'HASH' ) ? shift
                : scalar @_ == 1 ? { filename => shift }
                : { @_ };
    my $self  = bless {}, $class;

    if ( $args->{'filename'} ) {
        $self->filename( $args->{'filename'} ); 
    }

    return $self;
}

# ---------------------------------------------------------
=pod

=head2 filename

Gets/sets the location of the configuration file.

  my $file = $conf->filename( <filename> );

=cut

sub filename {
    my ( $self, $arg ) = @_;

    if ( ! defined $self->{'filename'} && ! $arg ) {
        $arg = DEFAULT_FILENAME if -e DEFAULT_FILENAME;
    } 

    if ( $arg ) {
        if ( -e $arg && -r _ ) {
            $self->{'filename'} = $arg; 
        }
        else {
            croak("The file '$arg' does not exist or is not readable");
        }
    }

    return $self->{'filename'};
}

# ---------------------------------------------------------
=pod

=head2 config 

Returns the configuration information as parsed by XML::Simple.

  my $options = $conf->config;

=cut

sub config {
    my $self = shift;
    unless ( defined $self->{'config'} ) {
        my $file = $self->filename;
        $self->{'config'} = XMLin(  
            $file,
#            ForceArray => [ qw( template token path file) ],
#            KeyAttr    => [ qw( token name file) ],
            ContentKey => '-value',
        );
    }
    return $self->{'config'};
}

# ---------------------------------------------------------
=pod

=head1 SEE ALSO

XML::Simple.

=head1 AUTHOR

Ken Y. Clark E<lt>kclark@cshl.orgE<gt>.

=cut

1;