line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model-Backend-Yaml |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2018 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::Yaml; |
11
|
|
|
|
|
|
|
$Config::Model::Backend::Yaml::VERSION = '2.133'; |
12
|
2
|
|
|
2
|
|
71840
|
use 5.10.1; |
|
2
|
|
|
|
|
7
|
|
13
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
153
|
|
14
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
15
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
70
|
|
16
|
2
|
|
|
2
|
|
10
|
use Config::Model 2.131; |
|
2
|
|
|
|
|
39
|
|
|
2
|
|
|
|
|
76
|
|
17
|
2
|
|
|
2
|
|
12
|
use Config::Model::Exception; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
18
|
2
|
|
|
2
|
|
12
|
use File::Path; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
128
|
|
19
|
2
|
|
|
2
|
|
16
|
use Log::Log4perl qw(get_logger :levels); |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
21
|
|
20
|
2
|
|
|
2
|
|
1200
|
use boolean; |
|
2
|
|
|
|
|
2244
|
|
|
2
|
|
|
|
|
11
|
|
21
|
2
|
|
|
2
|
|
949
|
use YAML::XS 0.69; |
|
2
|
|
|
|
|
5447
|
|
|
2
|
|
|
|
|
122
|
|
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
16
|
use base qw/Config::Model::Backend::Any/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1025
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $logger = get_logger("Backend::Yaml"); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub single_element { |
28
|
17
|
|
|
17
|
0
|
37
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
17
|
|
|
|
|
162
|
my @elts = $self->node->children; |
31
|
17
|
100
|
|
|
|
47867
|
return if @elts != 1; |
32
|
|
|
|
|
|
|
|
33
|
9
|
|
|
|
|
55
|
my $obj = $self->node->fetch_element($elts[0]); |
34
|
9
|
|
|
|
|
611
|
my $type = $obj->get_type; |
35
|
9
|
50
|
|
|
|
123
|
return $type =~ /^(list|hash)$/ ? $obj : undef ; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub read { |
39
|
11
|
|
|
11
|
1
|
98480
|
my $self = shift; |
40
|
11
|
|
|
|
|
110
|
my %args = @_; |
41
|
|
|
|
|
|
|
|
42
|
11
|
|
|
|
|
38
|
local $YAML::XS::LoadBlessed = 0; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# args is: |
45
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
46
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
47
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
48
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
49
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
50
|
|
|
|
|
|
|
# check => yes|no|skip |
51
|
|
|
|
|
|
|
|
52
|
11
|
100
|
|
|
|
52
|
return 0 unless $args{file_path}->exists; # no file to read |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# load yaml file |
55
|
8
|
|
|
|
|
194
|
my $yaml = $args{file_path}->slurp_raw; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# convert to perl data |
58
|
8
|
|
|
|
|
1803
|
my $perl_data = Load($yaml) ; |
59
|
8
|
50
|
|
|
|
47
|
if ( not defined $perl_data ) { |
60
|
0
|
|
|
|
|
0
|
my $msg = "No data found in YAML file $args{file_path}"; |
61
|
0
|
0
|
|
|
|
0
|
if ($args{auto_create}) { |
62
|
0
|
|
|
|
|
0
|
$logger->info($msg); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else { |
65
|
0
|
|
|
|
|
0
|
$logger->warn($msg); |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
0
|
return 1; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
8
|
|
66
|
|
|
34
|
my $target = $self->single_element // $self->node ; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# load perl data in tree |
73
|
8
|
|
50
|
|
|
66
|
$target->load_data( data => $perl_data, check => $args{check} || 'yes' ); |
74
|
8
|
|
|
|
|
32787
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub write { |
78
|
9
|
|
|
9
|
1
|
264826
|
my $self = shift; |
79
|
9
|
|
|
|
|
53
|
my %args = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# args is: |
82
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
83
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
84
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
85
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
86
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
87
|
|
|
|
|
|
|
# check => yes|no|skip |
88
|
|
|
|
|
|
|
|
89
|
9
|
|
|
|
|
34
|
local $YAML::XS::Boolean = "boolean"; |
90
|
|
|
|
|
|
|
|
91
|
9
|
|
66
|
|
|
51
|
my $target = $self->single_element // $self->node ; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $perl_data = $target->dump_as_data( |
94
|
|
|
|
|
|
|
full_dump => $args{full_dump} // 0, |
95
|
6
|
|
|
6
|
|
3384
|
to_boolean => sub { return boolean($_[0]) } |
96
|
9
|
|
100
|
|
|
140
|
); |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
2
|
|
21
|
my $yaml = Dump( $perl_data ); |
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|
2
|
|
142
|
|
|
2
|
|
|
1
|
|
24
|
|
|
2
|
|
|
1
|
|
6
|
|
|
2
|
|
|
1
|
|
116
|
|
|
2
|
|
|
|
|
23
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
125
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
65
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
34614
|
|
99
|
|
|
|
|
|
|
|
100
|
9
|
|
|
|
|
153
|
$args{file_path}->spew_raw($yaml); |
101
|
|
|
|
|
|
|
|
102
|
9
|
|
|
|
|
4722
|
return 1; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# ABSTRACT: Read and write config as a YAML data structure |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |