line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2005-2022 by Dominique Dumont. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Config::Model::Backend::CdsFile 2.153; # TRIAL |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
15
|
use 5.10.1; |
|
1
|
|
|
|
|
4
|
|
13
|
1
|
|
|
1
|
|
8
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
100
|
|
14
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
15
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
16
|
1
|
|
|
1
|
|
7
|
use Config::Model::Exception; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
17
|
1
|
|
|
1
|
|
5
|
use File::Path; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
79
|
|
18
|
1
|
|
|
1
|
|
7
|
use Log::Log4perl qw(get_logger :levels); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
166
|
use base qw/Config::Model::Backend::Any/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
493
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $logger = get_logger("Backend::CdsFile"); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub read { |
25
|
2
|
|
|
2
|
1
|
10
|
my $self = shift; |
26
|
2
|
|
|
|
|
18
|
my %args = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# args is: |
29
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
30
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
31
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
32
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
33
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
34
|
|
|
|
|
|
|
# check => yes|no|skip |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
4
|
my $file_path = $args{file_path}; |
37
|
2
|
50
|
|
|
|
8
|
return 0 unless $file_path->exists; |
38
|
2
|
|
|
|
|
51
|
$logger->info("Read cds data from $file_path"); |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
47
|
$self->node->load( step => $file_path->slurp_utf8 ); |
41
|
2
|
|
|
|
|
16
|
return 1; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub write { |
45
|
1
|
|
|
1
|
1
|
11
|
my $self = shift; |
46
|
1
|
|
|
|
|
9
|
my %args = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# args is: |
49
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
50
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
51
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
52
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
53
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
54
|
|
|
|
|
|
|
# check => yes|no|skip |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
3
|
my $file_path = $args{file_path}; |
57
|
1
|
|
|
|
|
6
|
$logger->info("Write cds data to $file_path"); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
33
|
my $dump = $self->node->dump_tree( skip_auto_write => 'cds_file', check => $args{check} ); |
60
|
1
|
|
|
|
|
11
|
$file_path->spew_utf8($dump); |
61
|
1
|
|
|
|
|
786
|
return 1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# ABSTRACT: Read and write config as a Cds data structure |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__END__ |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=pod |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=encoding UTF-8 |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 NAME |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Config::Model::Backend::CdsFile - Read and write config as a Cds data structure |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 VERSION |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
version 2.153 |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 SYNOPSIS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use Config::Model ; |
85
|
|
|
|
|
|
|
use Data::Dumper ; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# define configuration tree object |
88
|
|
|
|
|
|
|
my $model = Config::Model->new ; |
89
|
|
|
|
|
|
|
$model ->create_config_class ( |
90
|
|
|
|
|
|
|
name => "MyClass", |
91
|
|
|
|
|
|
|
element => [ |
92
|
|
|
|
|
|
|
[qw/foo bar/] => { |
93
|
|
|
|
|
|
|
type => 'leaf', |
94
|
|
|
|
|
|
|
value_type => 'string' |
95
|
|
|
|
|
|
|
}, |
96
|
|
|
|
|
|
|
baz => { |
97
|
|
|
|
|
|
|
type => 'hash', |
98
|
|
|
|
|
|
|
index_type => 'string' , |
99
|
|
|
|
|
|
|
cargo => { |
100
|
|
|
|
|
|
|
type => 'leaf', |
101
|
|
|
|
|
|
|
value_type => 'string', |
102
|
|
|
|
|
|
|
}, |
103
|
|
|
|
|
|
|
}, |
104
|
|
|
|
|
|
|
], |
105
|
|
|
|
|
|
|
rw_config => { |
106
|
|
|
|
|
|
|
backend => 'cds_file' , |
107
|
|
|
|
|
|
|
config_dir => '/tmp', |
108
|
|
|
|
|
|
|
file => 'foo.pl', |
109
|
|
|
|
|
|
|
auto_create => 1, |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
) ; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
my $inst = $model->instance(root_class_name => 'MyClass' ); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $root = $inst->config_root ; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $steps = 'foo=yada bar="bla bla" baz:en=hello |
118
|
|
|
|
|
|
|
baz:fr=bonjour baz:hr="dobar dan"'; |
119
|
|
|
|
|
|
|
$root->load( steps => $steps ) ; |
120
|
|
|
|
|
|
|
$inst->write_back ; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Now, C</tmp/foo.pl> contains: |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
{ |
125
|
|
|
|
|
|
|
bar => 'bla bla', |
126
|
|
|
|
|
|
|
baz => { |
127
|
|
|
|
|
|
|
en => 'hello', |
128
|
|
|
|
|
|
|
fr => 'bonjour', |
129
|
|
|
|
|
|
|
hr => 'dobar dan' |
130
|
|
|
|
|
|
|
}, |
131
|
|
|
|
|
|
|
foo => 'yada' |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 DESCRIPTION |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This module is used directly by L<Config::Model> to read or write the |
137
|
|
|
|
|
|
|
content of a configuration tree written with Cds syntax in |
138
|
|
|
|
|
|
|
C<Config::Model> configuration tree. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Note: |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=over 4 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Undefined values are skipped for list element. I.e. if a |
147
|
|
|
|
|
|
|
list element contains C<('a',undef,'b')>, the data structure |
148
|
|
|
|
|
|
|
contains C<'a','b'>. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Cds file is not created (and may be deleted) when no data is to be |
153
|
|
|
|
|
|
|
written. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 backend parameter |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 config_dir |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Mandoatory parameter to specify where is the Cds configuration file. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 new |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Inherited from L<Config::Model::Backend::Any>. The constructor is |
168
|
|
|
|
|
|
|
called by L<Config::Model::BackendMgr>. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 read |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Of all parameters passed to this read call-back, only C<file_path> is |
173
|
|
|
|
|
|
|
used. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
It can also be undef. In which case C<read> returns 0. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
When a file is read, C<read> returns 1. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 write |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Of all parameters passed to this write call-back, only C<file_path> is |
182
|
|
|
|
|
|
|
used. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
C<write> returns 1. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Dominique Dumont, (ddumont at cpan dot org) |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 SEE ALSO |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
L<Config::Model>, |
193
|
|
|
|
|
|
|
L<Config::Model::BackendMgr>, |
194
|
|
|
|
|
|
|
L<Config::Model::Backend::Any>, |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 AUTHOR |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Dominique Dumont |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
This software is Copyright (c) 2005-2022 by Dominique Dumont. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
This is free software, licensed under: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |