line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CTK::Configuration; # $Id: Configuration.pm 230 2019-05-03 17:27:05Z minus $ |
2
|
3
|
|
|
3
|
|
70621
|
use strict; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
92
|
|
3
|
3
|
|
|
3
|
|
622
|
use utf8; |
|
3
|
|
|
|
|
19
|
|
|
3
|
|
|
|
|
14
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
CTK::Configuration - Configuration of CTK |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 1.00 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use CTK::Configuration; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $config = new CTK::Configuration( |
20
|
|
|
|
|
|
|
config => "foo.conf", |
21
|
|
|
|
|
|
|
confdir => "conf", |
22
|
|
|
|
|
|
|
options => {... Config::General options ...}, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
The module works with the configuration |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 new |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $config = new CTK::Configuration( |
32
|
|
|
|
|
|
|
config => "/path/to/config/file.conf", |
33
|
|
|
|
|
|
|
confdir => "/path/to/config/directory", |
34
|
|
|
|
|
|
|
options => {... Config::General options ...}, |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Example foo.conf file: |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Foo 1 |
40
|
|
|
|
|
|
|
Bar test |
41
|
|
|
|
|
|
|
Flag true |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Example of the "conf" structure of $config object: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
print Dumper($config->{conf}); |
46
|
|
|
|
|
|
|
$VAR1 = { |
47
|
|
|
|
|
|
|
'foo' => 1 |
48
|
|
|
|
|
|
|
'bar' => 'test', |
49
|
|
|
|
|
|
|
'flag' => 1, |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=over 8 |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Specifies absolute or relative path to config-file. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item B |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Specifies absolute or relative path to config-dir. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item B |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Options of L |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=back |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 METHODS |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=over 8 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item B |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $error = $config->error; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Returns error string if occurred any errors while creating the object or reading the configuration file |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item B |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $value = $config->conf( 'key' ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Gets value from config structure by key |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $config_hash = $config->conf; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Returns config hash structure |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item B |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $value = $config->get( 'key' ); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Gets value from config structure by key |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item B |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $config_hash = $config->getall; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Returns config hash structure |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item B |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$config->set( 'key', 'value' ); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Sets value to config structure by key. Returns setted value |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item B |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
print $config->error unless $config->status; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Returns boolean status of loading config file |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=back |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 HISTORY |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over 8 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item B<1.00 Mon Apr 29 10:36:06 MSK 2019> |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Init version |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
See C file |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L, L |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 TO DO |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
See C file |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 BUGS |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
* none noted |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 SEE ALSO |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
L |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COPYRIGHT |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 LICENSE |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
153
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
See C file and L |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
3
|
|
|
3
|
|
153
|
use vars qw($VERSION); |
|
3
|
|
|
|
|
44
|
|
|
3
|
|
|
|
|
172
|
|
160
|
|
|
|
|
|
|
$VERSION = '1.00'; |
161
|
|
|
|
|
|
|
|
162
|
3
|
|
|
3
|
|
19
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
184
|
|
163
|
3
|
|
|
3
|
|
2255
|
use Config::General; |
|
3
|
|
|
|
|
50289
|
|
|
3
|
|
|
|
|
188
|
|
164
|
3
|
|
|
3
|
|
579
|
use Try::Tiny; |
|
3
|
|
|
|
|
2022
|
|
|
3
|
|
|
|
|
182
|
|
165
|
3
|
|
|
3
|
|
599
|
use Time::HiRes qw/gettimeofday/; |
|
3
|
|
|
|
|
1407
|
|
|
3
|
|
|
|
|
28
|
|
166
|
3
|
|
|
3
|
|
440
|
use Cwd qw/getcwd/; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
138
|
|
167
|
3
|
|
|
3
|
|
19
|
use File::Spec (); |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
84
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
use constant { |
170
|
3
|
|
|
|
|
2676
|
CONF_DIR => "conf", |
171
|
|
|
|
|
|
|
LOCKED_KEYS => [qw/hitime loadstatus/], |
172
|
3
|
|
|
3
|
|
17
|
}; |
|
3
|
|
|
|
|
8
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub new { |
175
|
4
|
|
|
4
|
1
|
565
|
my $class = shift; |
176
|
4
|
|
|
|
|
14
|
my %args = @_; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Create object |
179
|
4
|
|
|
|
|
48
|
my $self = bless { |
180
|
|
|
|
|
|
|
status => 0, |
181
|
|
|
|
|
|
|
dirs => [], |
182
|
|
|
|
|
|
|
error => "", |
183
|
|
|
|
|
|
|
files => [], |
184
|
|
|
|
|
|
|
created => time(), |
185
|
|
|
|
|
|
|
conf => { |
186
|
|
|
|
|
|
|
hitime => gettimeofday() * 1, |
187
|
|
|
|
|
|
|
loadstatus => 0, # == $self->{status} |
188
|
|
|
|
|
|
|
}, |
189
|
|
|
|
|
|
|
}, $class; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Set dirs |
192
|
4
|
|
|
|
|
12
|
my @dirs = (); |
193
|
4
|
|
66
|
|
|
19
|
my $mydir = $args{confdir} // $args{dir}; |
194
|
4
|
|
|
|
|
65
|
my $root = getcwd(); |
195
|
4
|
|
|
|
|
12
|
my $confdir; |
196
|
4
|
100
|
|
|
|
18
|
if ($mydir) { |
197
|
2
|
100
|
|
|
|
49
|
$confdir = File::Spec->file_name_is_absolute($mydir) |
198
|
|
|
|
|
|
|
? $mydir |
199
|
|
|
|
|
|
|
: File::Spec->catdir($root, $mydir); |
200
|
2
|
100
|
|
|
|
16
|
push (@dirs, $root) unless File::Spec->file_name_is_absolute($mydir); |
201
|
|
|
|
|
|
|
} else { |
202
|
2
|
|
|
|
|
23
|
$confdir = File::Spec->catdir($root, CONF_DIR); |
203
|
2
|
|
|
|
|
7
|
push (@dirs, $root); |
204
|
|
|
|
|
|
|
} |
205
|
4
|
50
|
|
|
|
15
|
push(@dirs, $confdir) if length($confdir); |
206
|
4
|
50
|
|
|
|
13
|
push(@dirs, CONF_DIR) if $confdir ne CONF_DIR; |
207
|
4
|
|
|
|
|
23
|
$self->{dirs} = [@dirs]; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
# Set files |
210
|
4
|
|
66
|
|
|
19
|
my $fileconf = $args{config} // $args{file} // $args{fileconf}; |
|
|
|
33
|
|
|
|
|
211
|
4
|
100
|
|
|
|
11
|
unless ($fileconf) { |
212
|
1
|
|
|
|
|
2
|
$self->{error} = "Config file not specified"; |
213
|
1
|
|
|
|
|
5
|
return $self; |
214
|
|
|
|
|
|
|
} |
215
|
3
|
100
|
|
|
|
41
|
$fileconf = File::Spec->catfile($root, $fileconf) |
216
|
|
|
|
|
|
|
unless File::Spec->file_name_is_absolute($fileconf); |
217
|
3
|
|
|
|
|
11
|
$self->{files} = [$fileconf]; |
218
|
3
|
100
|
|
|
|
114
|
unless (-e $fileconf) { |
219
|
1
|
|
|
|
|
8
|
$self->{error} = sprintf("Config file not found: %s", $fileconf); |
220
|
1
|
|
|
|
|
6
|
return $self; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# Loading |
224
|
2
|
|
50
|
|
|
14
|
my $tmpopts = $args{options} || {}; |
225
|
2
|
|
|
|
|
9
|
my %options = %$tmpopts; |
226
|
2
|
|
|
|
|
6
|
$options{"-ConfigFile"} = $fileconf; |
227
|
2
|
|
50
|
|
|
15
|
$options{"-ConfigPath"} ||= [@dirs]; |
228
|
2
|
50
|
|
|
|
7
|
$options{"-ApacheCompatible"} = 1 unless exists $options{"-ApacheCompatible"}; |
229
|
2
|
50
|
|
|
|
8
|
$options{"-LowerCaseNames"} = 1 unless exists $options{"-LowerCaseNames"}; |
230
|
2
|
50
|
|
|
|
7
|
$options{"-AutoTrue"} = 1 unless exists $options{"-AutoTrue"}; |
231
|
2
|
|
|
|
|
4
|
my $cfg; |
232
|
|
|
|
|
|
|
try { |
233
|
2
|
|
|
2
|
|
166
|
$cfg = new Config::General( %options ); |
234
|
|
|
|
|
|
|
} catch { |
235
|
0
|
|
|
0
|
|
0
|
$self->{error} = $_; |
236
|
0
|
|
|
|
|
0
|
return $self; |
237
|
2
|
|
|
|
|
31
|
}; |
238
|
2
|
50
|
33
|
|
|
6190
|
my %newconfig = $cfg->getall if $cfg && $cfg->can('getall'); |
239
|
2
|
50
|
33
|
|
|
46
|
$self->{files} = [$cfg->files] if $cfg && $cfg->can('files'); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Set only unlocked keys |
242
|
2
|
|
|
|
|
23
|
my %lkeys = (); |
243
|
2
|
|
|
|
|
4
|
foreach my $k (@{(LOCKED_KEYS)}) { $lkeys{$k} = 1 } |
|
2
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
9
|
|
244
|
2
|
50
|
33
|
|
|
7
|
foreach my $k (keys(%newconfig)) { $self->{conf}->{$k} = $newconfig{$k} if $k && !$lkeys{$k} } |
|
12
|
|
|
|
|
60
|
|
245
|
2
|
|
|
|
|
7
|
$self->{status} = 1; |
246
|
2
|
|
|
|
|
4
|
$self->{error} = ""; |
247
|
2
|
|
|
|
|
4
|
$self->{conf}->{loadstatus} = 1; |
248
|
|
|
|
|
|
|
|
249
|
2
|
|
|
|
|
39
|
return $self; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
sub error { |
252
|
3
|
|
|
3
|
1
|
8
|
my $self = shift; |
253
|
3
|
|
50
|
|
|
12
|
return $self->{error} // ''; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
sub status { |
256
|
3
|
|
|
3
|
1
|
995
|
my $self = shift; |
257
|
3
|
100
|
|
|
|
18
|
return $self->{status} ? 1 : 0; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
sub set { |
260
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
261
|
1
|
|
|
|
|
2
|
my $key = shift; |
262
|
1
|
50
|
33
|
|
|
7
|
return undef unless defined($key) && length($key); |
263
|
1
|
|
|
|
|
2
|
my $val = shift; |
264
|
1
|
|
|
|
|
18
|
$self->{conf}->{$key} = $val; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
sub get { |
267
|
6
|
|
|
6
|
1
|
14
|
my $self = shift; |
268
|
6
|
|
|
|
|
13
|
my $key = shift; |
269
|
6
|
50
|
33
|
|
|
41
|
return undef unless defined($key) && length($key); |
270
|
6
|
|
|
|
|
43
|
return $self->{conf}->{$key}; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
sub getall { |
273
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
274
|
1
|
|
|
|
|
4
|
return $self->{conf}; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
sub conf { |
277
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
278
|
1
|
|
|
|
|
1
|
my $key = shift; |
279
|
1
|
50
|
|
|
|
3
|
return undef unless $self->{conf}; |
280
|
1
|
50
|
|
|
|
16
|
return $self->{conf} unless defined $key; |
281
|
0
|
|
|
|
|
|
return $self->{conf}->{$key}; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
1; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
__END__ |