This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMdpConfig.pm
367 lines (242 loc) · 9.5 KB
/
MdpConfig.pm
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package MdpConfig;
=head1 NAME
MdpConfig (config)
=head1 DESCRIPTION
Wraps Config::Tiny to read and parse a config file.
Typically the $primary_config_filename is the most global followed by
a more local $secondary_config_filename. So, for example the
$primary_config_filename might be the configuration for a suite of
applications and the $secondary_config_filename might be the
supplements for a single application.
The $tertiary_config_filename could be used to override setting in
either the $primary_config_filename or $secondary_config_filename for
development purposes.
Either $secondary_config_filename or $tertiary_config_filename are
optional. Use undef for $secondary_config_filename if not required and
a $tertiary_config_filename is specified. The variations on this theme
are apparent.
=head1 SYNOPSIS
use MdpConfig;
my $config = new MdpConfig($primary_config_filename,
[$secondary_config_filename, $tertiary_config_filename]);
# What do we have?
if ($config->has("possible_key")) { ... }
my $keys = $config->keys();
# Get a value
$config->get('db_hostname');
# Need to override a value?
$config->override('db_hostname', $ENV[DB_HOSTNAME]);
$config->override_from_hashref(\%kv_pairs);
=head1 METHODS
=over 8
=cut
use strict;
use Config::Tiny;
use Utils;
use Debug::DUtils;
sub new {
my $package = shift;
my $primary_config_filename = shift;
my $secondary_config_filename = shift;
my $tertiary_config_filename = shift;
my $self = { config => {} };
bless($self, $package);
$self->{primary_config_file} = $primary_config_filename;
$self->add_config_from_file($primary_config_filename, 'primary_config');
if (defined($secondary_config_filename) && -e $secondary_config_filename) {
$self->{secondary_config_file} = $secondary_config_filename;
$self->add_config_from_file($secondary_config_filename, 'secondary_config');
}
if (defined($tertiary_config_filename) && -e $tertiary_config_filename) {
$self->{tertiary_config_file} = $tertiary_config_filename;
$self->add_config_from_file( $tertiary_config_filename, 'tertiary_config' );
}
return $self;
}
# ---------------------------------------------------------------------
=item add_config_from_file($config_file, $optional_config_key)
Add configuration values from the given file, doing ENV[key] expansion.
If the optional config_key is given, the Config::Tiny object resulting
from loading that file will be stored in $self->{$optional_config_key}
=cut
# ---------------------------------------------------------------------
sub add_config_from_file {
my $self = shift;
my $config_file = shift;
my $config_key = shift;
ASSERT(-e "$config_file",
qq{"Could not find config file $config_file"});
my $config = Config::Tiny->read($config_file);
ASSERT($config, qq{Error in config file $config_file: } . Config::Tiny::errstr);
if (defined($config_key)) {
$self->{$config_key} = $config;
}
$self->merge_from_config_tiny($config);
$self->override_from_ENV($config_file);
}
sub merge_from_config_tiny {
my $self = shift;
my $ct = shift;
$self->override_from_hashref($ct->{_});
}
# ---------------------------------------------------------------------
=item merge($config)
Description
=cut
# ---------------------------------------------------------------------
sub merge {
my $self = shift;
my $M_config = shift;
my $mtype = ref $M_config;
ASSERT(($mtype eq 'MdpConfig'), qq{"Invalid argument: MdpConfig merge given '$mtype' instead of MdpConfig object"});
foreach my $key (CORE::keys %{$M_config->{'config'}{_}}) {
# Do not overwrite any key value in the main config that came
# from a tertiary config file. Those are (typically)
# debugging values that would be stomped by defaults from the
# to-be-merged-in M_config
if (defined $self->{'tertiary_config'}->{_}{$key}) {
if ($self->{'tertiary_config'}->{_}{$key} ne $M_config->{'config'}->{_}{$key}) {
next;
}
}
$self->{'config'}->{_}{$key} = $M_config->{'config'}->{_}{$key};
}
}
# ---------------------------------------------------------------------
=item get ($config_var_name)
Get a config variable value
=cut
# ---------------------------------------------------------------------
sub get {
my ($self, $var_name) = @_;
my $val = $self->{'config'}->{_}{$var_name};
ASSERT(defined($val), qq{config key="$var_name" does not have a value});
if (wantarray) {
return split(/\|/, $val);
}
else {
return $val;
}
}
# ---------------------------------------------------------------------
=item keys
Get all the keys. MdpConfig doesn't allow any hierarchy at all,
so, it's all top-level
=cut
# ---------------------------------------------------------------------
sub keys {
my $self = shift;
return CORE::keys(%{$self->{'config'}->{_}});
}
# ---------------------------------------------------------------------
=item override($key, $val)
Set (and thus override) a configuation parameter
=cut
# ---------------------------------------------------------------------
sub override {
my $self = shift;
my $key = shift;
my $value = shift;
my $config = $self->{'config'};
$config->{_}{$key} = $value;
return $value;
}
# ---------------------------------------------------------------------
=item override_from_hashref($hashref)
Set (and thus potentially overrides) key/value pairs from the given hashref
=cut
# ---------------------------------------------------------------------
sub override_from_hashref {
my $self = shift;
my $hr = shift;
foreach my $key (CORE::keys %$hr) {
$self->override($key, $hr->{$key});
}
}
# ---------------------------------------------------------------------
=item override_from_ENV
Any value of the form ENV[keyname] will be overwritten with the
corresponding environment variable.
If the environment variable is unset, throw a warning and move on.
=cut
# ---------------------------------------------------------------------
sub override_from_ENV {
my $self = shift;
my $filename = shift;
$filename || ($filename = "<unknown>");
foreach my $key ($self->keys()) {
my $val = $self->get($key);
if ($val =~ /ENV\[["']?(.+?)["']?\]/) {
my $env_var = $1;
my $env_val = $ENV{$env_var};
if ($env_val) {
$self->override($key, $env_val);
}
else {
warn("Config $filename references ENV[$env_var] which is unset");
}
}
}
}
# ---------------------------------------------------------------------
=item has ($config_var_name)
Test that a config variable has a value
=cut
# ---------------------------------------------------------------------
sub has {
my ($self, $var_name) = @_;
my $val = $self->{'config'}->{_}{$var_name};
return defined($val);
}
1;
# ---------------------------------------------------------------------
=item __config_debug
Description
=cut
# ---------------------------------------------------------------------
sub __config_debug {
my $self = shift;
my $config = $self->{'config'};
my $primary_config = $self->{'primary_config'};
my $secondary_config = $self->{'secondary_config'};
my $tertiary_config = $self->{'tertiary_config'};
DEBUG('all,conf',
sub {
my $s;
$s .= q{primary config file=} . $self->{'primary_config_file'} . qq{<br/>\n};
$s .= q{secondary config file=} . $self->{'secondary_config_file'} . qq{<br/>\n};
$s .= q{tertiary config file=} . $self->{'tertiary_config_file'} . qq{<br/>\n};
foreach my $key (sort CORE::keys %{$config->{_}}) {
my $res = $config->{_}{$key};
my $pri = $primary_config->{_}{$key};
my $sec = $secondary_config->{_}{$key} if ($secondary_config);
my $ter = $tertiary_config->{_}{$key} if ($tertiary_config);
my $from = (defined($ter) ? 'tertiary' : (defined($sec) ? 'secondary' : 'primary'));
$s .= qq{$key = $res from $from config<br/>\n}
}
return $s;
});
}
__END__
=head1 AUTHOR
Original: Jessica Feeman, University of Michigan, jhovater@umich.edu
Modified: Phillip Farber, University of Michigan, pfarber@umich.edu
=head1 COPYRIGHT
Copyright 2007-2010 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut