Changeset 409 in ProjectBuilder for devel/pb-modules/lib/ProjectBuilder/Conf.pm


Ignore:
Timestamp:
Apr 25, 2008, 3:13:55 PM (16 years ago)
Author:
Bruno Cornec
Message:
  • Document all reusable functions in pb
  • remove the useless pbproj parameter from pb_filter functions
  • Addition and use of pb_conf_init and pb_conf_add in pb
  • Addition and use of pb_conf_fromfile_if in Conf.pm
  • preparation for 0.9.1
  • Update of pbinit files for mondo to support the new interface of pb_filter functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb-modules/lib/ProjectBuilder/Conf.pm

    r405 r409  
    2222 
    2323our @ISA = qw(Exporter);
    24 our @EXPORT = qw(pb_conf_init pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if);
     24our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if);
     25
     26# Global list of conf files
     27our @pbconffiles = ();
    2528
    2629=pod
     
    5760sub pb_conf_init {
    5861
    59 my @conffiles = @_;
     62@pbconffiles = @_;
     63}
     64
     65=item B<pb_conf_add>
     66
     67This function adds the configuration file to the list last.
     68
     69=cut
     70
     71sub pb_conf_add {
     72
     73my $f = shift;
     74
     75push(@pbconffiles,"$f");
    6076}
    6177
     
    84100
    85101  $k1->{'pb'}  contains 3
    86   $ka->{'default'} contains 1
     102  $k1->{'default'} contains 1
    87103  $k2->{'pb'} contains 12,25
    88104
     
    130146}
    131147
    132 
    133 # Function which returns a pointer on a table
    134 # corresponding to a set of values queried in the conf file
    135 # and test the returned vaue as they need to exist in that case
    136 sub pb_conf_get {
    137 
    138 my @param = @_;
    139 my @return = pb_conf_get_if(@param);
    140 
    141 die "No params found for $ENV{'PBPROJ'}" if (not @return);
    142 
    143 foreach my $i (0..$#param) {
    144     die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
    145 }
    146 return(@return);
    147 }
    148 
    149 # Function which returns a pointer on a table
    150 # corresponding to a set of values queried in the conf file
    151 # Those value may be undef if they do not exist
     148=item B<pb_conf_get_if>
     149
     150This function returns a table, corresponding to a set of values querried in the conf files or undef if it doen't exist. It takes a table of keys as an input parameter.
     151
     152The format of the configurations file is as follows:
     153
     154key tag = value1,value2,...
     155
     156It will gather the values from all the configurations files passed to pb_conf_init, and return the values for the keys, taking in account the order of conf files, to manage overloading.
     157
     158  $ cat $HOME/.pbrc
     159  pbver pb = 1
     160  pblist pb = 4
     161  $ cat $HOME/.pbrc2
     162  pbver pb = 3
     163  pblist default = 5
     164
     165calling it like this:
     166
     167  pb_conf_init("$HOME/.pbrc","$HOME/.pbrc2");
     168  my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
     169
     170will allow to get the mapping:
     171
     172  $k1->{'pb'} contains 3
     173  $k2->{'pb'} contains 4
     174
     175Valid chars for keys and tags are letters, numbers, '-' and '_'.
     176
     177=cut
     178
    152179sub pb_conf_get_if {
    153180
     181my @param = @_;
     182
     183my $ptr = undef;
     184
     185# the most important conf file is first, so read them in revers order
     186foreach my $f (reverse @pbconffiles) {
     187    $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param);
     188}
     189
     190return(@$ptr);
     191}
     192
     193=item B<pb_conf_fromfile_if>
     194
     195This function returns a pointer on a table, corresponding to a merge of values querried in the conf file and the pointer on another table passed as parameter. It takes a table of keys as last input parameter.
     196
     197  my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist");
     198  my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist");
     199
     200It is used internally by pb_conf_get_if and is not exported yet.
     201
     202=cut
     203
     204
     205sub pb_conf_get_fromfile_if {
     206
     207my $conffile = shift;
     208my $ptr2 = shift || undef;
    154209my @param = @_;
    155210
     
    157212my @ptr1 = ();
    158213my @ptr2 = ();
    159 @ptr1 = pb_conf_read_if("$ENV{'PBETC'}", @param) if (defined $ENV{'PBETC'});
    160 @ptr2 = pb_conf_read_if("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb", @param) if ((defined $ENV{'PBROOTDIR'}) and (defined $ENV{'PBPROJ'}));
     214
     215# @ptr1 contains the values overloading what @ptr2 may contain.
     216@ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile);
     217@ptr2 = @$ptr2 if (defined $ptr2);
    161218
    162219my $p1;
     
    169226    $p1 = $ptr1[$i];
    170227    $p2 = $ptr2[$i];
    171     # Always try to take the param from the home dir conf file in priority
    172     # in order to mask what could be defined under the CMS to allow for overloading
     228    # Always try to take the param from ptr1
     229    # in order to mask what could be defined already in ptr2
    173230    if (not defined $p2) {
    174         # No ref in CMS project conf file so use the home dir one.
     231        # No ref in p2 so use p1
    175232        $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
    176233    } else {
    177         # Ref found in CMS project conf file
     234        # Ref found in p2
    178235        if (not defined $p1) {
    179             # No ref in home dir project conf file so use the CMS one.
     236            # No ref in p1 so use p2's value
    180237            $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
    181238            $p1 = $p2;
     
    196253            }
    197254            # Now copy back into p1 all p2 content which doesn't exist in p1
    198             # p1 content (local) always has priority over p2 (project)
     255            # p1 content always has priority over p2
    199256            foreach my $k (keys %$p2) {
    200257                $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
     
    205262}
    206263pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n");
    207 return(@ptr1);
    208 }
    209 
     264return(\@ptr1);
     265}
     266
     267=item B<pb_conf_get>
     268
     269This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
     270
     271=cut
     272
     273sub pb_conf_get {
     274
     275my @param = @_;
     276my @return = pb_conf_get_if(@param);
     277
     278die "No params found for $ENV{'PBPROJ'}" if (not @return);
     279
     280foreach my $i (0..$#param) {
     281    die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
     282}
     283return(@return);
     284}
    210285
    211286=back
Note: See TracChangeset for help on using the changeset viewer.