| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: Manage config YAML files in a simple manner |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package App::Basis::Config; |
|
6
|
|
|
|
|
|
|
$App::Basis::Config::VERSION = '1.0'; |
|
7
|
1
|
|
|
1
|
|
71276
|
use 5.010; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
32
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
27
|
|
|
9
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
27
|
|
|
10
|
1
|
|
|
1
|
|
783
|
use Moo; |
|
|
1
|
|
|
|
|
15307
|
|
|
|
1
|
|
|
|
|
5
|
|
|
11
|
1
|
|
|
1
|
|
2256
|
use YAML::XS qw( Load Dump); |
|
|
1
|
|
|
|
|
3264
|
|
|
|
1
|
|
|
|
|
76
|
|
|
12
|
1
|
|
|
1
|
|
9
|
use Path::Tiny; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
48
|
|
|
13
|
1
|
|
|
1
|
|
948
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
1470
|
|
|
|
1
|
|
|
|
|
52
|
|
|
14
|
1
|
|
|
1
|
|
581
|
use App::Basis; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
1597
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
19
|
|
|
|
|
|
|
# the raw config data hash |
|
20
|
|
|
|
|
|
|
has raw => ( |
|
21
|
|
|
|
|
|
|
is => 'ro', |
|
22
|
|
|
|
|
|
|
lazy => 1, |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# builder => '_load', |
|
25
|
|
|
|
|
|
|
init_arg => undef, # dont allow setting in constructor |
|
26
|
|
|
|
|
|
|
default => sub { {} }, |
|
27
|
|
|
|
|
|
|
writer => '_set_raw' |
|
28
|
|
|
|
|
|
|
); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has filename => ( |
|
31
|
|
|
|
|
|
|
is => 'ro', |
|
32
|
|
|
|
|
|
|
required => 0, |
|
33
|
|
|
|
|
|
|
writer => '_set_filename' |
|
34
|
|
|
|
|
|
|
); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has nostore => ( |
|
37
|
|
|
|
|
|
|
is => 'ro', |
|
38
|
|
|
|
|
|
|
default => sub {0} |
|
39
|
|
|
|
|
|
|
); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has die_on_error => ( |
|
42
|
|
|
|
|
|
|
is => 'ro', |
|
43
|
|
|
|
|
|
|
default => sub {0} |
|
44
|
|
|
|
|
|
|
); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has has_data => ( |
|
48
|
|
|
|
|
|
|
is => 'ro', |
|
49
|
|
|
|
|
|
|
default => sub {0}, |
|
50
|
|
|
|
|
|
|
init_arg => undef, # dont allow setting in constructor |
|
51
|
|
|
|
|
|
|
writer => '_set_has_data' |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has changed => ( |
|
56
|
|
|
|
|
|
|
is => 'rw', |
|
57
|
|
|
|
|
|
|
default => sub {0}, |
|
58
|
|
|
|
|
|
|
init_arg => undef, # dont allow setting in constructor |
|
59
|
|
|
|
|
|
|
# writer => '_set_changed' |
|
60
|
|
|
|
|
|
|
); |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has error => ( |
|
64
|
|
|
|
|
|
|
is => 'ro', |
|
65
|
|
|
|
|
|
|
default => sub {undef}, |
|
66
|
|
|
|
|
|
|
init_arg => undef, # dont allow setting in constructor |
|
67
|
|
|
|
|
|
|
writer => '_set_error' |
|
68
|
|
|
|
|
|
|
); |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub BUILD { |
|
74
|
3
|
|
|
3
|
0
|
24
|
my $self = shift; |
|
75
|
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
10
|
$self->_set_error(undef); |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# make sure that the we expand home |
|
79
|
3
|
|
|
|
|
20
|
my $fname = fix_filename( $self->filename ); |
|
80
|
|
|
|
|
|
|
|
|
81
|
3
|
50
|
|
|
|
9
|
if ( !$fname ) { |
|
82
|
0
|
|
0
|
|
|
0
|
$fname = $ENV{APP_BASIS_CFG} || fix_filename( "~/." . get_program() . ".cfg" ); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
3
|
100
|
66
|
|
|
116
|
if ( $fname && -f $fname ) { |
|
85
|
2
|
|
|
|
|
8
|
$self->_set_filename($fname); |
|
86
|
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
3
|
my $config; |
|
88
|
|
|
|
|
|
|
try { |
|
89
|
2
|
|
|
2
|
|
78
|
$config = Load( path($fname)->slurp_utf8 ); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
catch { |
|
92
|
0
|
|
|
0
|
|
0
|
$self->_set_error( |
|
93
|
|
|
|
|
|
|
"Could not read/processs config file $fname. $_"); |
|
94
|
2
|
|
|
|
|
22
|
}; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# if there was a file to read from and we had an issue then we should |
|
97
|
|
|
|
|
|
|
# report it back to the caller somehow and make sure its seen. |
|
98
|
2
|
50
|
|
|
|
2198
|
if ( $self->error ) { |
|
99
|
0
|
0
|
|
|
|
0
|
die $self->error if ( $self->die_on_error ); |
|
100
|
0
|
|
|
|
|
0
|
warn $self->error; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# if we loaded some config |
|
104
|
2
|
50
|
|
|
|
11
|
if ( keys %$config ) { |
|
105
|
2
|
|
|
|
|
6
|
$self->_set_has_data(1); |
|
106
|
2
|
|
|
|
|
70
|
$self->_set_raw($config); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
else { |
|
110
|
1
|
|
|
|
|
4
|
$self->_set_error("could not establish a config filename"); |
|
111
|
1
|
50
|
|
|
|
30
|
die $self->error if ( $self->die_on_error ); |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub store { |
|
119
|
3
|
|
|
3
|
1
|
9
|
my $self = shift; |
|
120
|
3
|
|
|
|
|
5
|
my $filename = shift; |
|
121
|
3
|
|
|
|
|
5
|
my $need_save = 0; |
|
122
|
3
|
|
|
|
|
5
|
my $status = 0; |
|
123
|
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
5
|
local $YAML::Indent = 4; |
|
125
|
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
10
|
$self->_set_error(undef); |
|
127
|
3
|
100
|
|
|
|
6
|
if ( !$filename ) { |
|
128
|
2
|
|
|
|
|
7
|
$filename = $self->filename; |
|
129
|
2
|
50
|
|
|
|
7
|
$need_save = 1 if ( $self->changed ); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
else { |
|
132
|
1
|
|
|
|
|
1
|
$need_save = 1; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# only save if we need to |
|
136
|
3
|
50
|
|
|
|
10
|
if ($need_save) { |
|
137
|
3
|
100
|
|
|
|
10
|
if ( $self->nostore ) { |
|
138
|
1
|
|
|
|
|
113
|
warn "Attempt to save config file " |
|
139
|
|
|
|
|
|
|
. $self->filename |
|
140
|
|
|
|
|
|
|
. " when nostore has been used"; |
|
141
|
1
|
|
|
|
|
5
|
return 0; |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# do the save |
|
145
|
2
|
|
|
|
|
79
|
my $cfg = $self->raw; |
|
146
|
|
|
|
|
|
|
try { |
|
147
|
|
|
|
|
|
|
# do we need to create the directory to hold the file |
|
148
|
2
|
50
|
|
2
|
|
58
|
if ( !-d path($filename)->dirname ) { |
|
149
|
0
|
|
|
|
|
0
|
path($filename)->dirname->mkpath; |
|
150
|
|
|
|
|
|
|
} |
|
151
|
2
|
|
|
|
|
169
|
path($filename)->spew_utf8( Dump($cfg) ); |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
catch { |
|
154
|
0
|
|
|
0
|
|
0
|
$self->_set_error( |
|
155
|
|
|
|
|
|
|
"Could not save config file " . $self->filename() ); |
|
156
|
0
|
|
|
|
|
0
|
$status = 0; |
|
157
|
2
|
|
|
|
|
30
|
}; |
|
158
|
2
|
50
|
33
|
|
|
1404
|
die $self->error if ( $self->error && $self->die_on_error ); |
|
159
|
2
|
|
|
|
|
8
|
$self->changed(0); |
|
160
|
2
|
|
|
|
|
3
|
$status = 1; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
2
|
|
|
|
|
6
|
return $status; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
167
|
|
|
|
|
|
|
# return a ref to a item in the config or undef |
|
168
|
|
|
|
|
|
|
# if $value is true then a path will be established and the value stored as the |
|
169
|
|
|
|
|
|
|
# final node |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub _split_path { |
|
172
|
15
|
|
|
15
|
|
20
|
my $self = shift; |
|
173
|
15
|
|
|
|
|
22
|
my ( $path, $value ) = @_; |
|
174
|
15
|
|
|
|
|
18
|
my $done = 0; |
|
175
|
15
|
|
|
|
|
17
|
my $path_separators = '/:\.'; |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# remove any leading/trailing path separators |
|
178
|
15
|
|
|
|
|
170
|
$path =~ s|^[$path_separators]?(.*)[$path_separators]?$|$1|; |
|
179
|
|
|
|
|
|
|
|
|
180
|
15
|
|
|
|
|
386
|
my $ref = $self->raw; |
|
181
|
15
|
|
|
|
|
159
|
my @items = split( /[$path_separators]/, $path ); |
|
182
|
|
|
|
|
|
|
try { |
|
183
|
15
|
|
|
15
|
|
402
|
for ( my $i = 0; $i < scalar(@items); $i++ ) { |
|
184
|
30
|
|
|
|
|
45
|
my $item = $items[$i]; |
|
185
|
30
|
100
|
|
|
|
58
|
if ( $ref->{$item} ) { |
|
186
|
20
|
|
|
|
|
30
|
$ref = $ref->{$item}; |
|
187
|
20
|
|
|
|
|
60
|
$done = 1; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
else { |
|
190
|
10
|
50
|
|
|
|
15
|
if ($value) { |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# is this the last thing? |
|
193
|
10
|
100
|
|
|
|
19
|
if ( ( $i + 1 ) == scalar(@items) ) { |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# save the value in the last node |
|
196
|
5
|
|
|
|
|
11
|
$ref->{$item} = $value; |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
else { |
|
199
|
5
|
|
|
|
|
21
|
$ref->{$item} = {}; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
10
|
|
|
|
|
16
|
$ref = $ref->{$item}; |
|
202
|
10
|
|
|
|
|
31
|
$done = 1; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
else { |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# missed item |
|
207
|
0
|
|
|
|
|
0
|
$done = 0; |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
} |
|
212
|
15
|
|
|
0
|
|
106
|
catch {}; |
|
|
0
|
|
|
|
|
0
|
|
|
213
|
|
|
|
|
|
|
|
|
214
|
15
|
50
|
|
|
|
241
|
return $done ? $ref : undef; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub get { |
|
221
|
10
|
|
|
10
|
1
|
3133
|
my $self = shift; |
|
222
|
10
|
|
|
|
|
12
|
my $path = shift; |
|
223
|
|
|
|
|
|
|
|
|
224
|
10
|
|
|
|
|
23
|
$self->_set_error(undef); |
|
225
|
|
|
|
|
|
|
|
|
226
|
10
|
|
|
|
|
25
|
my $ref = $self->_split_path($path); |
|
227
|
|
|
|
|
|
|
|
|
228
|
10
|
|
|
|
|
41
|
return $ref; |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub set { |
|
235
|
5
|
|
|
5
|
1
|
1408
|
my $self = shift; |
|
236
|
5
|
|
|
|
|
11
|
my ( $path, $value ) = @_; |
|
237
|
|
|
|
|
|
|
|
|
238
|
5
|
|
|
|
|
13
|
$self->_set_error(undef); |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# create the path to the item |
|
241
|
5
|
|
|
|
|
12
|
my $ref = $self->_split_path( $path, $value ); |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# if we loaded some config |
|
244
|
5
|
50
|
|
|
|
8
|
$self->_set_has_data(1) if ( keys %{ $self->raw() } ); |
|
|
5
|
|
|
|
|
123
|
|
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# something has changed so we may need to save it later |
|
247
|
5
|
|
|
|
|
63
|
$self->changed(1); |
|
248
|
|
|
|
|
|
|
} |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
sub clear { |
|
254
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
255
|
0
|
|
|
|
|
|
my ( $path, $value ) = @_; |
|
256
|
|
|
|
|
|
|
|
|
257
|
0
|
|
|
|
|
|
$self->_set_error(undef); |
|
258
|
|
|
|
|
|
|
|
|
259
|
0
|
|
|
|
|
|
$self->raw = {}; |
|
260
|
0
|
|
|
|
|
|
$self->_set_has_data(0); |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# something has changed so we may need to save it later |
|
263
|
0
|
|
|
|
|
|
$self->changed(1); |
|
264
|
|
|
|
|
|
|
} |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
267
|
|
|
|
|
|
|
# make sure we do any cleanup required |
|
268
|
|
|
|
|
|
|
|
|
269
|
1
|
|
|
1
|
|
935
|
END { |
|
270
|
|
|
|
|
|
|
} |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
1; |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
__END__ |