source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Conf.pm@ 409

Last change on this file since 409 was 409, checked in by Bruno Cornec, 16 years ago
  • 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 size: 7.0 KB
Line 
1#!/usr/bin/perl -w
2#
3# ProjectBuilder Conf module
4# Conf files subroutines brought by the the Project-Builder project
5# which can be easily used by wahtever perl project
6#
7# $Id$
8#
9
10package ProjectBuilder::Conf;
11
12use strict;
13use Data::Dumper;
14use ProjectBuilder::Base;
15
16# Inherit from the "Exporter" module which handles exporting functions.
17
18use Exporter;
19
20# Export, by default, all the functions into the namespace of
21# any code which uses this module.
22
23our @ISA = qw(Exporter);
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 = ();
28
29=pod
30
31=head1 NAME
32
33ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
34
35=head1 DESCRIPTION
36
37This modules provides functions dealing with configuration files.
38
39=head1 SYNOPSIS
40
41 use ProjectBuilder::Conf;
42
43 #
44 # Read hash codes of values from a configuration file and return table of pointers
45 #
46 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
47 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
48
49=head1 USAGE
50
51=over 4
52
53=item B<pb_conf_init>
54
55This function initializes the configuration files analysis.
56Pass the configuration files to consider in order of importance (the most important first).
57
58=cut
59
60sub pb_conf_init {
61
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");
76}
77
78=item B<pb_conf_read_if>
79
80This function returns a table of pointers on hashes
81corresponding to the keys in a configuration file passed in parameter.
82If that file doesn't exist, it returns undef.
83
84The format of the configuration file is as follows:
85
86key tag = value1,value2,...
87
88Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
89
90 $ cat $HOME/.pbrc
91 pbver pb = 3
92 pbver default = 1
93 pblist pb = 12,25
94
95calling it like this:
96
97 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
98
99will allow to get the mapping:
100
101 $k1->{'pb'} contains 3
102 $k1->{'default'} contains 1
103 $k2->{'pb'} contains 12,25
104
105Valid chars for keys and tags are letters, numbers, '-' and '_'.
106
107=cut
108
109sub pb_conf_read_if {
110
111my $conffile = shift;
112my @param = @_;
113
114open(CONF,$conffile) || return((undef));
115close(CONF);
116return(pb_conf_read($conffile,@param));
117}
118
119=item B<pb_conf_read>
120
121This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
122
123=cut
124
125sub pb_conf_read {
126
127my $conffile = shift;
128my @param = @_;
129my $trace;
130my @ptr;
131my %h;
132
133open(CONF,$conffile) || die "Unable to open $conffile";
134while(<CONF>) {
135 if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
136 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
137 $h{$1}{$2}=$3;
138 }
139}
140close(CONF);
141
142for my $param (@param) {
143 push @ptr,$h{$param};
144}
145return(@ptr);
146}
147
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
179sub pb_conf_get_if {
180
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;
209my @param = @_;
210
211# Everything is returned via ptr1
212my @ptr1 = ();
213my @ptr2 = ();
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);
218
219my $p1;
220my $p2;
221
222pb_log(2,"DEBUG: pb_conf_get param1: ".Dumper(@ptr1)."\n");
223pb_log(2,"DEBUG: pb_conf_get param2: ".Dumper(@ptr2)."\n");
224
225foreach my $i (0..$#param) {
226 $p1 = $ptr1[$i];
227 $p2 = $ptr2[$i];
228 # Always try to take the param from ptr1
229 # in order to mask what could be defined already in ptr2
230 if (not defined $p2) {
231 # No ref in p2 so use p1
232 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
233 } else {
234 # Ref found in p2
235 if (not defined $p1) {
236 # No ref in p1 so use p2's value
237 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
238 $p1 = $p2;
239 } else {
240 # Both are defined - handling the overloading
241 if (not defined $p1->{'default'}) {
242 if (defined $p2->{'default'}) {
243 $p1->{'default'} = $p2->{'default'};
244 }
245 }
246
247 if (not defined $p1->{$ENV{'PBPROJ'}}) {
248 if (defined $p2->{$ENV{'PBPROJ'}}) {
249 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
250 } else {
251 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
252 }
253 }
254 # Now copy back into p1 all p2 content which doesn't exist in p1
255 # p1 content always has priority over p2
256 foreach my $k (keys %$p2) {
257 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
258 }
259 }
260 }
261 $ptr1[$i] = $p1;
262}
263pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n");
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}
285
286=back
287
288=head1 WEB SITES
289
290The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
291
292=head1 USER MAILING LIST
293
294None exists for the moment.
295
296=head1 AUTHORS
297
298The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
299
300=head1 COPYRIGHT
301
302Project-Builder.org is distributed under the GPL v2.0 license
303described in the file C<COPYING> included with the distribution.
304
305=cut
306
307
3081;
Note: See TracBrowser for help on using the repository browser.