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.132'; |
12
|
2
|
|
|
2
|
|
60035
|
use 5.10.1; |
|
2
|
|
|
|
|
9
|
|
13
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
134
|
|
14
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
40
|
|
15
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
16
|
2
|
|
|
2
|
|
8
|
use Config::Model 2.131; |
|
2
|
|
|
|
|
36
|
|
|
2
|
|
|
|
|
67
|
|
17
|
2
|
|
|
2
|
|
12
|
use Config::Model::Exception; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
60
|
|
18
|
2
|
|
|
2
|
|
17
|
use File::Path; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
105
|
|
19
|
2
|
|
|
2
|
|
12
|
use Log::Log4perl qw(get_logger :levels); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
27
|
|
20
|
2
|
|
|
2
|
|
998
|
use boolean; |
|
2
|
|
|
|
|
1825
|
|
|
2
|
|
|
|
|
8
|
|
21
|
2
|
|
|
2
|
|
792
|
use YAML::XS 0.69; |
|
2
|
|
|
|
|
4412
|
|
|
2
|
|
|
|
|
117
|
|
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
13
|
use base qw/Config::Model::Backend::Any/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
925
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $logger = get_logger("Backend::Yaml"); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub single_element { |
28
|
14
|
|
|
14
|
0
|
32
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
14
|
|
|
|
|
104
|
my @elts = $self->node->children; |
31
|
14
|
100
|
|
|
|
34122
|
return if @elts != 1; |
32
|
|
|
|
|
|
|
|
33
|
9
|
|
|
|
|
49
|
my $obj = $self->node->fetch_element($elts[0]); |
34
|
9
|
|
|
|
|
511
|
my $type = $obj->get_type; |
35
|
9
|
50
|
|
|
|
111
|
return $type =~ /^(list|hash)$/ ? $obj : undef ; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub read { |
39
|
9
|
|
|
9
|
1
|
66813
|
my $self = shift; |
40
|
9
|
|
|
|
|
88
|
my %args = @_; |
41
|
|
|
|
|
|
|
|
42
|
9
|
|
|
|
|
23
|
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
|
9
|
100
|
|
|
|
42
|
return 0 unless $args{file_path}->exists; # no file to read |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# load yaml file |
55
|
6
|
|
|
|
|
123
|
my $yaml = $args{file_path}->slurp_utf8; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# convert to perl data |
58
|
6
|
|
|
|
|
1169
|
my $perl_data = Load($yaml) ; |
59
|
6
|
50
|
|
|
|
45
|
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
|
6
|
|
66
|
|
|
26
|
my $target = $self->single_element // $self->node ; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# load perl data in tree |
73
|
6
|
|
50
|
|
|
43
|
$target->load_data( data => $perl_data, check => $args{check} || 'yes' ); |
74
|
6
|
|
|
|
|
23845
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub write { |
78
|
8
|
|
|
8
|
1
|
194973
|
my $self = shift; |
79
|
8
|
|
|
|
|
48
|
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
|
8
|
|
|
|
|
22
|
local $YAML::XS::Boolean = "boolean"; |
90
|
|
|
|
|
|
|
|
91
|
8
|
|
66
|
|
|
55
|
my $target = $self->single_element // $self->node ; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $perl_data = $target->dump_as_data( |
94
|
|
|
|
|
|
|
full_dump => $args{full_dump} // 0, |
95
|
3
|
|
|
3
|
|
1315
|
to_boolean => sub { return boolean($_[0]) } |
96
|
8
|
|
100
|
|
|
111
|
); |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
2
|
|
17
|
my $yaml = Dump( $perl_data ); |
|
2
|
|
|
2
|
|
4
|
|
|
2
|
|
|
1
|
|
134
|
|
|
2
|
|
|
1
|
|
18
|
|
|
2
|
|
|
1
|
|
4
|
|
|
2
|
|
|
1
|
|
117
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
23
|
|
|
8
|
|
|
|
|
27963
|
|
99
|
|
|
|
|
|
|
|
100
|
8
|
|
|
|
|
109
|
$args{file_path}->spew_utf8($yaml); |
101
|
|
|
|
|
|
|
|
102
|
8
|
|
|
|
|
4677
|
return 1; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# ABSTRACT: Read and write config as a YAML data structure |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |