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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# $Id$
package Youri::Config;
=head1 NAME
Youri::Config - Youri configuration handler
=head1 SYNOPSIS
use Youri::Config;
my $config = Youri::Config->new(
command_spec => [
'help|h!',
],
file_spec => [
'foo=s',
],
directories => [ '/etc/youri', "$ENV{HOME}/.youri" ],
file_name => 'app.conf',
caller => $0,
);
# get configuration directive
my $foo = $config->get('foo');
# get configuration section
my %bar = $config->get_section('bar');
=head1 DESCRIPTION
This class handle configuration for all YOURI tools.
It uses distinct command line and config files specification, but merges the
two inputs transparently, command line directives overriding config file
directives with the same name.
Given directories are scanned for a file with given name, and only the first
one found is used. If B<--config> argument is given on command line, no
scanning occurs. If no readable file is found, an exception is thrown.
==head1 FORMAT
The file format used is the one from AppConfig, with the additional ability to
use YAML. Here is an exemple configuration file:
[updates]
class = Youri::Check::Check::Updates
grabbers = <<EOF
--- #YAML:1.0
debian:
class: Youri::Check::Check::Updates::Debian
aliases:
fuse: ~
cpan:
class: Youri::Check::Check::Updates::CPAN
fedora:
class: Youri::Check::Check::Updates::Fedora
gentoo:
class: Youri::Check::Check::Updates::Gentoo
freshmeat:
class: Youri::Check::Check::Updates::Freshmeat
aliases:
fuse: fuse-emulator
EOF
As a side-effect of using YAML, the use of character '~' anywhere is prohibited.
Use ${HOME} instead.
=head1 SEE ALSO
AppConfig, YAML
=cut
use strict;
use warnings;
use AppConfig qw/:argcount :expand/;
use File::Spec;
use Pod::Usage;
use Carp;
use YAML;
sub new {
my ($class, %options) = @_;
my ($command_config, $file_config);
# process command line
if ($options{command_spec}) {
$command_config = AppConfig->new(
{
CREATE => 1,
GLOBAL => {
DEFAULT => '',
EXPAND => EXPAND_VAR | EXPAND_ENV,
ARGCOUNT => ARGCOUNT_ONE,
}
},
@{$options{command_spec}}
);
$command_config->args();
pod2usage(
-input => $options{caller},
-verbose => 0
) if $command_config->get('help');
}
# process config file
$file_config = AppConfig->new(
{
CREATE => 1,
GLOBAL => {
DEFAULT => '',
EXPAND => EXPAND_VAR | EXPAND_ENV,
ARGCOUNT => ARGCOUNT_ONE,
}
},
@{$options{file_spec}}
);
# find configuration file to use
my $main_file;
if ($command_config) {
my $file = $command_config->get('config');
if ($file) {
if (! -f $file) {
carp "Non-existing file $file, skipping";
} elsif (! -r $file) {
carp "Non-readable file $file, skipping";
} else {
$main_file = $file;
}
};
}
unless ($main_file) {
foreach my $directory (@{$options{directories}}) {
my $file = "$directory/$options{file_name}";
next unless -f $file && -r $file;
$main_file = $file;
last;
}
}
croak 'No config file found, aborting' unless $main_file;
$file_config->file($main_file);
# process inclusions
my $need_rescan;
foreach my $include_file (split(/\s+/, $file_config->get('includes'))) {
# convert relative path to absolute ones
$include_file = File::Spec->rel2abs(
$include_file, (File::Spec->splitpath($main_file))[1]
);
if (! -f $include_file) {
warn "Non-existing file $include_file, skipping";
} elsif (! -r $include_file) {
warn "Non-readable file $include_file, skipping";
} else {
$file_config->file($include_file);
$need_rescan = 1;
}
}
$file_config->file($main_file) if $need_rescan;
# merge command line configuration
if ($command_config) {
my %command_vars = $command_config->varlist('.*');
while (my ($key, $value) = each %command_vars) {
$file_config->set($key, $value);
}
}
my $self = bless {
_appconfig => $file_config
}, $class;
return $self;
}
=head2 get_section($id)
Simple wrapper around $config->varlist(), throwing a warning if section I<$id> doesn't exists.
=cut
sub get_section {
my ($self, $id) = @_;
croak "Not a class method" unless ref $self;
my %values = $self->{_appconfig}->varlist('^' . $id . '_', 1);
carp "No such section $id" unless %values;
foreach my $value (values %values) {
$value = _yamlize($value);
}
return %values;
}
sub get {
my ($self, $variable) = @_;
croak "Not a class method" unless ref $self;
return _yamlize($self->{_appconfig}->get($variable));
}
sub _yamlize {
my ($value) = @_;
if ($value =~ /^--- #YAML:1.0/) {
eval {
$value = Load($value . "\n");
};
$value = undef if $@;
}
return $value;
}
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2002-2006, YOURI project
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
1;
|