fixed recursive_children cvterm function, and added tests for parents and children
[cxgn-corelibs.git] / lib / CXGN / Configuration.pm
blobc6a3d7980f38e8fd6aa83939e8c963d30cab5a0b
2 =head1 NAME
4 CXGN::Configuration
6 =head1 DESCRIPTION
8 Reads in a configuration file in a certain syntax and stores this configuration for access by scripts. Takes item names and lists of values for that item. Puts values as arrayrefs into a hash with the item names as a keys.
10 #syntax example
12 #comment
13 <name1> <value1>
15 #comment
16 <name2> <value2> #another comment
18 #this example will put multiple values into name3
19 <name3> <value3.0> <value3.1> <value3.2>
21 =head1 OBJECT METHODS
23 =head2 new
25 Creates a new configuration object.
27 #example
28 use CXGN::Configuration;
29 my $config=CXGN::Configuration->new($config_file_location);
31 =head2 get_conf
33 Gives you back the FIRST value for the variable you ask for, using the information that was retrieved from the configuration file. If there are many values for this variable, and you want them, you must use get_conf_arrayref().
35 #example which gets the locally located location of your web checkout
36 my $basepath=$config->get_conf('basepath');
38 =head2 get_conf_arrayref
40 Gives you back an arrayref of the values for the variable you ask for, even if there is one value or no values.
42 =head2 get_conf_hashref_single_values
44 Gives you back a hash with all your items, and the first values in their lists.
46 =head2 parse_line
48 For internal use. Gets a variable name from the configuration file and assigns a value or values to it.
50 =head1 AUTHOR
52 john binns - John Binns <zombieite@gmail.com>
54 =cut
56 package CXGN::Configuration;
57 use strict;
58 sub new
60 my $class=shift;
61 my $self=bless({},$class);
62 $self->{conf_files}=\@_;
63 my $FILE;
64 my $files_found=0;
65 for my $file_location(@{$self->{conf_files}})#these will be done in order, so later ones will override earlier ones
67 if(-f($file_location))
69 $files_found++;
70 if(open($FILE,$file_location))
72 # warn "parsing config $file_location\n";
73 while(my $line=<$FILE>)
75 chomp($line);
76 my($name,$values)=$self->parse_line($line);
77 if($name)
79 $self->{config}->{$name}=$values;#set this element, and override any previously existing configuration setting for this element
83 else{warn"CXGN::Configuration: Cannot open file '$file_location: $!'.\n";return;}
85 #else{warn"CXGN::Configuration: File '$file_location' does not exist.\n";}
87 unless($files_found){warn"CXGN::Configuration: None of the requested configuration files were found.\n";return;}
88 return $self;
90 sub parse_line
92 my $self=shift;
93 my($line)=@_;
94 $line =~ s/#.+//; #remove any comments from consideration
95 return unless $line =~ /\S/; #ignore empty lines
96 my($name,@values)=split(/[\t ]+/,$line);
97 unless(defined($values[0])){$values[0]='';}#let's make "no value" be stored as an empty string instead
98 return($name,\@values);
100 sub get_conf#returns a single scalar value even if this item contains more than one value. use get_conf_arrayref to get all values for this item.
102 my $self=shift;
103 my($requested_conf)=@_;
104 if($requested_conf and defined($self->{config}->{$requested_conf}))
106 return $self->{config}->{$requested_conf}->[0];
108 else
110 return;
113 sub get_conf_arrayref#returns an arrayref, even if a single value is stored
115 my $self=shift;
116 my($requested_conf)=@_;
117 return $self->{config}->{$requested_conf};
119 sub get_conf_hashref_single_values#returns a hashref with single values for all items, even if multiple values are available
121 my $self=shift;
122 my %hash;
123 for my $key(keys(%{$self->{config}}))
125 $hash{$key}=$self->{config}->{$key}->[0];
127 return \%hash;