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