line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model-Approx |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2015-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::Approx ; |
11
|
|
|
|
|
|
|
$Config::Model::Backend::Approx::VERSION = '1.011'; |
12
|
2
|
|
|
2
|
|
14311
|
use Mouse ; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
18
|
|
13
|
2
|
|
|
2
|
|
1239
|
use Log::Log4perl qw(get_logger :levels); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
21
|
|
14
|
2
|
|
|
2
|
|
288
|
use Carp ; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
152
|
|
15
|
2
|
|
|
2
|
|
501
|
use File::Copy ; |
|
2
|
|
|
|
|
2341
|
|
|
2
|
|
|
|
|
107
|
|
16
|
2
|
|
|
2
|
|
14
|
use File::Path ; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
93
|
|
17
|
2
|
|
|
2
|
|
38
|
use 5.010 ; |
|
2
|
|
|
|
|
8
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
extends 'Config::Model::Backend::Any'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub annotation { |
22
|
3
|
|
|
3
|
1
|
182
|
return 1 ; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $logger = Log::Log4perl::get_logger('Backend::Approx'); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub read { |
28
|
3
|
|
|
3
|
1
|
11649
|
my $self = shift ; |
29
|
3
|
|
|
|
|
31
|
my %args = @_ ; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# args are: |
32
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
33
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
34
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
35
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
36
|
|
|
|
|
|
|
# io_handle => $io # IO::File object |
37
|
|
|
|
|
|
|
# check => yes|no|skip |
38
|
|
|
|
|
|
|
|
39
|
3
|
50
|
|
|
|
35
|
$logger->info("loading config file $args{file}") if defined $args{file}; |
40
|
3
|
|
|
|
|
41
|
my @lines = $args{file_path}->lines_utf8 ; |
41
|
3
|
|
|
|
|
590
|
my $global = $self->read_global_comments(\@lines, '#') ; |
42
|
3
|
|
|
|
|
291
|
$self->node->annotation($global) ; |
43
|
|
|
|
|
|
|
|
44
|
3
|
|
|
|
|
85
|
my @data = $self->associates_comments_with_data(\@lines, '#') ; |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
765
|
foreach my $item (@data) { |
47
|
15
|
|
|
|
|
282
|
my ($line,$note) = @$item ; |
48
|
|
|
|
|
|
|
|
49
|
15
|
|
|
|
|
96
|
my ($k,$v) = split /\s+/,$line,2 ; |
50
|
|
|
|
|
|
|
|
51
|
15
|
100
|
|
|
|
122
|
my $step = ($k =~ s/^\$//) ? $k |
|
|
100
|
|
|
|
|
|
52
|
|
|
|
|
|
|
: ($v =~ m!://!) ? "distributions:".$k |
53
|
|
|
|
|
|
|
: $k ; # old style parameter |
54
|
15
|
|
|
|
|
90
|
my $leaf = $self->node->grab(step => $step) ; |
55
|
15
|
|
|
|
|
164044
|
$leaf->store($v) ; |
56
|
15
|
|
|
|
|
7710
|
$leaf->annotation($note) ; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
110
|
return 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub write { |
63
|
2
|
|
|
2
|
1
|
55118
|
my $self = shift ; |
64
|
2
|
|
|
|
|
15
|
my %args = @_ ; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
16
|
$logger->info("writing config file $args{file}"); |
67
|
2
|
|
|
|
|
47
|
my $node = $args{object} ; |
68
|
2
|
|
|
|
|
31
|
my $res = $self->write_global_comment('#'); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Using Config::Model::ObjTreeScanner would be overkill |
71
|
2
|
|
|
|
|
154
|
foreach my $elt ($node->get_element_name) { |
72
|
26
|
100
|
|
|
|
683
|
next if $elt eq 'distributions'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# write value |
75
|
24
|
|
|
|
|
92
|
my $obj = $node->grab($elt) ; |
76
|
24
|
|
|
|
|
9144
|
my $v = $obj->fetch ; |
77
|
|
|
|
|
|
|
|
78
|
24
|
100
|
|
|
|
6967
|
if (defined $v) { |
79
|
4
|
100
|
|
|
|
20
|
$res .= sprintf("# %s\n", $obj->annotation) if $obj->annotation; |
80
|
4
|
|
|
|
|
99
|
$res .= sprintf("\$%-10s %s\n\n",$elt,$v) ; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
13
|
my $h = $node->fetch_element('distributions') ; |
85
|
2
|
|
|
|
|
151
|
foreach my $dname ($h->fetch_all_indexes) { |
86
|
6
|
|
|
|
|
1272
|
my $d = $node->grab("distributions:$dname") ; |
87
|
|
|
|
|
|
|
|
88
|
6
|
|
|
|
|
2759
|
my $note = $d->annotation; |
89
|
6
|
100
|
|
|
|
76
|
$res .= "# $note\n" if $note; |
90
|
6
|
|
|
|
|
25
|
$res .= sprintf("%-10s %s\n",$dname,$d->fetch) ; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
589
|
$args{file_path}->spew_utf8($res); |
94
|
2
|
|
|
|
|
1420
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# ABSTRACT: Read and write Approx configuration file |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |