Debian Codegraph @

mysql-test/lib/v1/My/Config.pm

raw
1 # -*- cperl -*-
2
3 # Copyright (c) 2008 Sun Microsystems, Inc.
4 # Use is subject to license terms.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
18
19 package My::Config::Option;
20
21 use strict;
22 use warnings;
23
24
25 sub new {
26 my ($class, $option_name, $option_value)= @_;
27 my $self= bless { name => $option_name,
28 value => $option_value
29 }, $class;
30 return $self;
31 }
32
33
34 sub name {
35 my ($self)= @_;
36 return $self->{name};
37 }
38
39
40 sub value {
41 my ($self)= @_;
42 return $self->{value};
43 }
44
45
46 package My::Config::Group;
47
48 use strict;
49 use warnings;
50
51
52 sub new {
53 my ($class, $group_name)= @_;
54 my $self= bless { name => $group_name,
55 options => [],
56 options_by_name => {},
57 }, $class;
58 return $self;
59 }
60
61
62 sub insert {
63 my ($self, $option_name, $value, $if_not_exist)= @_;
64 my $option= $self->option($option_name);
65 if (defined($option) and !$if_not_exist) {
66 $option->{value}= $value;
67 }
68 else {
69 my $option= My::Config::Option->new($option_name, $value);
70 # Insert option in list
71 push(@{$self->{options}}, $option);
72 # Insert option in hash
73 $self->{options_by_name}->{$option_name}= $option;
74 }
75 return $option;
76 }
77
78 sub remove {
79 my ($self, $option_name)= @_;
80
81 # Check that option exists
82 my $option= $self->option($option_name);
83
84 return undef unless defined $option;
85
86 # Remove from the hash
87 delete($self->{options_by_name}->{$option_name}) or die;
88
89 # Remove from the array
90 @{$self->{options}}= grep { $_->name ne $option_name } @{$self->{options}};
91
92 return $option;
93 }
94
95
96 sub options {
97 my ($self)= @_;
98 return @{$self->{options}};
99 }
100
101
102 sub name {
103 my ($self)= @_;
104 return $self->{name};
105 }
106
107
108 #
109 # Return a specific option in the group
110 #
111 sub option {
112 my ($self, $option_name)= @_;
113
114 return $self->{options_by_name}->{$option_name};
115 }
116
117
118 #
119 # Return a specific value for an option in the group
120 #
121 sub value {
122 my ($self, $option_name)= @_;
123 my $option= $self->option($option_name);
124
125 die "No option named '$option_name' in this group"
126 if ! defined($option);
127
128 return $option->value();
129 }
130
131
132 package My::Config;
133
134 use strict;
135 use warnings;
136 use IO::File;
137 use File::Basename;
138
139 #
140 # Constructor for My::Config
141 # - represents a my.cnf config file
142 #
143 # Array of arrays
144 #
145 sub new {
146 my ($class, $path)= @_;
147 my $group_name= undef;
148
149 my $self= bless { groups => [] }, $class;
150 my $F= IO::File->new($path, "<")
151 or die "Could not open '$path': $!";
152
153 while ( my $line= <$F> ) {
154 chomp($line);
155
156 # [group]
157 if ( $line =~ /\[(.*)\]/ ) {
158 # New group found
159 $group_name= $1;
160 #print "group: $group_name\n";
161
162 $self->insert($group_name, undef, undef);
163 }
164
165 # Magic #! comments
166 elsif ( $line =~ /^#\!/) {
167 my $magic= $line;
168 die "Found magic comment '$magic' outside of group"
169 unless $group_name;
170
171 #print "$magic\n";
172 $self->insert($group_name, $magic, undef);
173 }
174
175 # Comments
176 elsif ( $line =~ /^#/ || $line =~ /^;/) {
177 # Skip comment
178 next;
179 }
180
181 # Empty lines
182 elsif ( $line =~ /^$/ ) {
183 # Skip empty lines
184 next;
185 }
186
187 # !include <filename>
188 elsif ( $line =~ /^\!include\s*(.*?)\s*$/ ) {
189 my $include_file_name= dirname($path)."/".$1;
190 # Check that the file exists
191 die "The include file '$include_file_name' does not exist"
192 unless -f $include_file_name;
193
194 $self->append(My::Config->new($include_file_name));
195 }
196
197 # <option>
198 elsif ( $line =~ /^([\@\w-]+)\s*$/ ) {
199 my $option= $1;
200
201 die "Found option '$option' outside of group"
202 unless $group_name;
203
204 #print "$option\n";
205 $self->insert($group_name, $option, undef);
206 }
207
208 # <option>=<value>
209 elsif ( $line =~ /^([\@\w-]+)\s*=\s*(.*?)\s*$/ ) {
210 my $option= $1;
211 my $value= $2;
212
213 die "Found option '$option=$value' outside of group"
214 unless $group_name;
215
216 #print "$option=$value\n";
217 $self->insert($group_name, $option, $value);
218 } else {
219 die "Unexpected line '$line' found in '$path'";
220 }
221
222 }
223 undef $F; # Close the file
224
225 return $self;
226 }
227
228 #
229 # Insert a new group if it does not already exist
230 # and add option if defined
231 #
232 sub insert {
233 my ($self, $group_name, $option, $value, $if_not_exist)= @_;
234 my $group;
235
236 # Create empty array for the group if it doesn't exist
237 if ( !$self->group_exists($group_name) ) {
238 $group= $self->_group_insert($group_name);
239 }
240 else {
241 $group= $self->group($group_name);
242 }
243
244 if ( defined $option ) {
245 #print "option: $option, value: $value\n";
246
247 # Add the option to the group
248 $group->insert($option, $value, $if_not_exist);
249 }
250 }
251
252 #
253 # Remove a option, given group and option name
254 #
255 sub remove {
256 my ($self, $group_name, $option_name)= @_;
257 my $group= $self->group($group_name);
258
259 die "group '$group_name' does not exist"
260 unless defined($group);
261
262 $group->remove($option_name) or
263 die "option '$option_name' does not exist";
264 }
265
266
267
268 #
269 # Check if group with given name exists in config
270 #
271 sub group_exists {
272 my ($self, $group_name)= @_;
273
274 foreach my $group ($self->groups()) {
275 return 1 if $group->{name} eq $group_name;
276 }
277 return 0;
278 }
279
280
281 #
282 # Insert a new group into config
283 #
284 sub _group_insert {
285 my ($self, $group_name)= @_;
286 caller eq __PACKAGE__ or die;
287
288 # Check that group does not already exist
289 die "Group already exists" if $self->group_exists($group_name);
290
291 my $group= My::Config::Group->new($group_name);
292 push(@{$self->{groups}}, $group);
293 return $group;
294 }
295
296
297 #
298 # Append a configuration to current config
299 #
300 sub append {
301 my ($self, $from)= @_;
302
303 foreach my $group ($from->groups()) {
304 foreach my $option ($group->options()) {
305 $self->insert($group->name(), $option->name(), $option->value());
306 }
307
308 }
309 }
310
311
312 #
313 # Return a list with all the groups in config
314 #
315 sub groups {
316 my ($self)= @_;
317 return ( @{$self->{groups}} );
318 }
319
320
321 #
322 # Return a list of all the groups in config
323 # starting with the given string
324 #
325 sub like {
326 my ($self, $prefix)= @_;
327 return ( grep ( $_->{name} =~ /^$prefix/, $self->groups()) );
328 }
329
330
331 #
332 # Return the first group in config
333 # starting with the given string
334 #
335 sub first_like {
336 my ($self, $prefix)= @_;
337 return ($self->like($prefix))[0];
338 }
339
340
341 #
342 # Return a specific group in the config
343 #
344 sub group {
345 my ($self, $group_name)= @_;
346
347 foreach my $group ( $self->groups() ) {
348 return $group if $group->{name} eq $group_name;
349 }
350 return undef;
351 }
352
353
354 #
355 # Return a list of all options in a specific group in the config
356 #
357 sub options_in_group {
358 my ($self, $group_name)= @_;
359
360 my $group= $self->group($group_name);
361 return () unless defined $group;
362 return $group->options();
363 }
364
365
366 #
367 # Return a value given group and option name
368 #
369 sub value {
370 my ($self, $group_name, $option_name)= @_;
371 my $group= $self->group($group_name);
372
373 die "group '$group_name' does not exist"
374 unless defined($group);
375
376 my $option= $group->option($option_name);
377 die "option '$option_name' does not exist"
378 unless defined($option);
379
380 return $option->value();
381 }
382
383
384 #
385 # Check if an option exists
386 #
387 sub exists {
388 my ($self, $group_name, $option_name)= @_;
389 my $group= $self->group($group_name);
390
391 die "group '$group_name' does not exist"
392 unless defined($group);
393
394 my $option= $group->option($option_name);
395 return defined($option);
396 }
397
398
399 # Overload "to string"-operator with 'stringify'
400 use overload
401 '""' => \&stringify;
402
403 #
404 # Return the config as a string in my.cnf file format
405 #
406 sub stringify {
407 my ($self)= @_;
408 my $res;
409
410 foreach my $group ($self->groups()) {
411 $res .= "[$group->{name}]\n";
412
413 foreach my $option ($group->options()) {
414 $res .= $option->name();
415 my $value= $option->value();
416 if (defined $value) {
417 $res .= "=$value";
418 }
419 $res .= "\n";
420 }
421 $res .= "\n";
422 }
423 return $res;
424 }
425
426
427 #
428 # Save the config to named file
429 #
430 sub save {
431 my ($self, $path)= @_;
432 my $F= IO::File->new($path, ">")
433 or die "Could not open '$path': $!";
434 print $F $self;
435 undef $F; # Close the file
436 }
437
438 1;