| 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::BackendMgr 2.169; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 59 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 59 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 59 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 59 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 59 | ▶ | use v5.20; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 59 | ▶ | use Config::Model::Exception; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 59 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 59 | ▶ | use Storable qw/dclone/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 59 | ▶ | use Scalar::Util qw/weaken reftype/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 59 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 59 | ▶ | use Path::Tiny 0.070; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 59 | ▶ | use feature qw/postderef signatures/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 59 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 59 | my $logger = get_logger('BackendMgr'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 59 | my $user_logger = get_logger('User'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # one BackendMgr per file | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 59 | has 'node' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Config::Model::Node', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | weak_ref => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 98 | ▶ | has 'file_backup' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 59 | has 'rw_config' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 59 | has 'backend_obj' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | isa => 'Config::Model::Backend::Any', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | lazy => 1 , | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | builder => '_build_backend_obj', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 98 | ▶ | sub _build_backend_obj { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_backend_obj: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 98 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 98 | my $backend = $self->rw_config->{backend}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $logger->warn("function parameter for a backend is deprecated. Please implement 'read' method in backend $backend") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 98 | ▶ | if $self->rw_config->{function}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # try to load a specific Backend class | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 98 | ▶ | my $f = $self->rw_config->{function} || 'read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->rw_config->{'function'} || 'read'
MC/DC0%0/1
$self->rw_config->{'function'} || 'read'
$self->rw_config->{'function'}
Truth table50%1/2
A
A= $self->rw_config->{'function'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 98 | my $c = $self->load_backend_class( $backend, $f ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 59 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | return $c->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | node => $self->node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | name => $backend, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | auto_create => $self->rw_config->{auto_create}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | auto_delete => $self->rw_config->{auto_delete}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 98 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 59 | has support_annotation => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 59 | with "Config::Model::Role::ComputeFunction"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 59 | with "Config::Model::Role::FileHandler"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # check if dir is present. May create it in auto_create write mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 130 | ▶ | sub get_cfg_dir_path ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cfg_dir_path: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 130 | ▶ | my $w = $args{write} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'write'} || 0
MC/DC100%1/1
$args{'write'} || 0
$args{'write'}
Truth table100%2/2
A
A= $args{'write'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 130 | my $dir = $self->get_tuned_config_dir(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 130 | ▶ | if ( not $dir->is_dir and $w and $args{auto_create} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $dir->is_dir and $w and $args{"auto_create"}
not $dir->is_dir and $w
MC/DC66%2/3
not $dir->is_dir and $w and $args{"auto_create"}
not $dir->is_dir
$w
$args{"auto_create"}
Truth table75%3/4
not(A) and B and C
A= not $dir->is_dirB= $wC= $args{"auto_create"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $logger->info("creating directory $dir"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | $dir->mkpath; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 130 | ▶ | unless ( $dir->is_dir ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | my $mode = $w ? 'write' : 'read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | $logger->info( "$args{backend}: missing directory $dir ($mode mode)" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | return ( 0, $dir ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 126 | ▶ | $logger->trace( "dir: " . $dir // '<undef>' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'dir: ' . $dir // "<undef>"
MC/DC0%0/1
'dir: ' . $dir // "<undef>"
'dir: ' . $dir
Truth table50%1/2
A
A= 'dir: ' . $dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 126 | return ( 1, $dir ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # return (1, config file path) constructed from arguments or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # (0). May create directory in auto_create write mode. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 140 | ▶ | sub get_cfg_file_path ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cfg_file_path: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 140 | ▶ | my $w = $args{write} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'write'} || 0
MC/DC100%1/1
$args{'write'} || 0
$args{'write'}
Truth table100%2/2
A
A= $args{'write'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # config file override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 140 | my $cfo = $args{config_file}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 140 | ▶ | if ( defined $cfo) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | my $override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | ▶ | = $args{root} ? $args{root}->child($cfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | : $cfo =~ m!^/! ? path($cfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | : path('.')->child($cfo); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 10 | ▶ | my $mode = $w ? 'write' : 'read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | $logger->trace("$args{backend} override target file is $override ($mode mode)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | return ( 1, $override ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 130 | my ( $dir_ok, $dir ) = $self->get_cfg_dir_path(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 130 | ▶ | if ( defined $args{file} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 111 | ▶ | my $file = $args{skip_compute} ? $args{file} : $self->node->compute_string($args{file}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 111 | my $res = $dir->child($file); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 111 | $logger->trace("get_cfg_file_path: returns $res"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 111 | return ( $dir_ok, $res ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 19 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 58 | ▶ | sub open_read_file ($self, $file_path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_read_file: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 58 | ▶ | if ( $file_path->is_file ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 56 | $logger->debug("open_read_file: open $file_path for read"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # store a backup in memory in case there's a problem | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 56 | $self->file_backup( [ $file_path->lines_utf8 ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 56 | return $file_path->filehandle("<", ":utf8"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # called at configuration node creation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 196 | ▶ | sub load_backend_class ($self, $backend, $function) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_backend_class: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 196 | $logger->trace("load_backend_class: called with backend $backend, function $function"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 196 | my %c; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 196 | my $k = "Config::Model::Backend::" . ucfirst($backend); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 196 | my $f = $k . '.pm'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 196 | $f =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 196 | $c{$k} = $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # try another class | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 196 | $k =~ s/_(\w)/uc($1)/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 196 | $f =~ s/_(\w)/uc($1)/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 196 | $c{$k} = $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 196 | foreach my $c ( sort keys %c ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 202 | ▶ | if ( $c->can($function) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # no need to load class | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 179 | $logger->debug("load_backend_class: $c is already loaded (can $function)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 179 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # look for file to load | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 17 | my $class_to_load; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 17 | foreach my $c ( sort keys %c ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 23 | $logger->trace("load_backend_class: looking to load class $c"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 23 | foreach my $prefix (@INC) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 197 | my $realfilename = "$prefix/$c{$c}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 197 | ▶ | $class_to_load = $c if -f $realfilename; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 17 | ▶ | if (not defined $class_to_load) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | Config::Model::Exception::Model->throw( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | object => $self->node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | error => "backend error: cannot find Perl class for backend: '$backend'", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 17 | my $file_to_load = $c{$class_to_load}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 17 | $logger->trace("load_backend_class: loading class $class_to_load, $file_to_load"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 17 | eval { require $file_to_load; }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 17 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | die "Error with backend $backend: could not parse $file_to_load: $@\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 17 | return $class_to_load; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 98 | ▶ | sub read_config_data ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_config_data: called CC=7 SCAR=20.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 98 | $logger->trace( "called for node ", $self->node->location ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 98 | my $check = delete $args{check}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 98 | my $config_file_override = delete $args{config_file}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 98 | my $auto_create_override = delete $args{auto_create}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 98 | ▶ | croak "unexpected args " . join( ' ', keys %args ) . "\n" if %args; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 98 | my $rw_config = dclone $self->rw_config ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 98 | my $instance = $self->node->instance(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # root override is passed by the instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 98 | my $root_dir = $instance->root_dir ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 98 | my $auto_create = $rw_config->{auto_create}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 98 | my $backend = $rw_config->{backend}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 98 | ▶ | if ( $rw_config->{default_layer} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 2 | $self->read_config_sub_layer( $rw_config, $root_dir, $config_file_override, $check, | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | $backend ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 98 | my ( $res, $file ) = | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | $self->try_read_backend( $rw_config, $root_dir, $config_file_override, $check, $backend ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 98 | ▶ | Config::Model::Exception::ConfigFile::Missing->throw ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
$res or $auto_create_override or $auto_create
$res or $auto_create_override
$file || '<unknown>'
MC/DC0%0/3
$res or $auto_create_override or $auto_create
$res
$auto_create_override
$auto_create
MC/DC0%0/1
$file || '<unknown>'
$file
Truth table50%2/4
A or B or C
A= $resB= $auto_create_overrideC= $auto_create
Truth table0%0/2
A
A= $file
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | file => $file || "<unknown>", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | object => $self->node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | ) unless $res or $auto_create_override or $auto_create; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 98 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | ▶ | sub read_config_sub_layer ($self, $rw_config, $root_dir, $config_file_override, $check, $backend) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_config_sub_layer: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | my $layered_config = delete $rw_config->{default_layer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | my $layered_read = dclone $rw_config ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | foreach my $item ( qw/file config_dir os_config_dir/ ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 6 | my $lc = delete $layered_config->{$item}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 6 | ▶ | $layered_read->{$item} = $lc if $lc; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | Config::Model::Exception::Model->throw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | error => "backend error: unexpected default_layer parameters: " | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | . join( ' ', sort keys %$layered_config ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | object => $self->node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ) if %$layered_config; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | my $i = $self->node->instance; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | my $already_in_layered = $i->layered; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # layered stuff here | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | ▶ | if ( not $already_in_layered ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | $i->layered_clear; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | $i->layered_start; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | $self->try_read_backend( $layered_read, $root_dir, $config_file_override, $check, $backend ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | if ( not $already_in_layered ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | $i->layered_stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # called at configuration node creation, NOT when writing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 100 | ▶ | sub try_read_backend ($self, $rw_config, $root_dir, $config_file_override, $check, $backend) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
try_read_backend: called CC=15 SCAR=34.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 100 | my $read_dir = $self->get_tuned_config_dir(%$rw_config); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 100 | my @read_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | %$rw_config, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | root => $root_dir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | config_dir => $read_dir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | backend => $backend, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | check => $check, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | config_file => $config_file_override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 100 | my $backend_obj = $self->backend_obj(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 100 | ▶ | if ($backend_obj->can('suffix')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | $logger->warn("suffix method is deprecated. you can remove it from backend $backend"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 100 | my ( $file_ok, $file_path ) = $self->get_cfg_file_path( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | @read_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | skip_compute => $backend_obj->skip_open, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 100 | my $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 100 | ▶ | if (not $backend_obj->skip_open and $file_ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $backend_obj->skip_open and $file_ok
MC/DC100%2/2
not $backend_obj->skip_open and $file_ok
not $backend_obj->skip_open
$file_ok
Truth table100%3/3
not(A) and B
A= not $backend_obj->skip_openB= $file_ok
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 58 | $fh = $self->open_read_file($file_path) ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 100 | ▶ | my $f = $self->rw_config->{function} || 'read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->rw_config->{'function'} || 'read'
MC/DC0%0/1
$self->rw_config->{'function'} || 'read'
$self->rw_config->{'function'}
Truth table50%1/2
A
A= $self->rw_config->{'function'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 100 | ▶ | if ($logger->is_info) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | my $fp = defined $file_path ? " on $file_path":'' ; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $logger->info( "Read with $backend " . reftype($backend_obj) . "::$f".$fp); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 100 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 100 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 100 | $res = $backend_obj->$f( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | @read_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | file_path => $file_path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | object => $self->node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 100 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # catch eval error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 100 | ▶ | if ( ref($error) and $error->isa('Config::Model::Exception::Syntax') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition33%1/3
ref $error and $error->isa("Config::Model::Exception::Syntax")
MC/DC0%0/2
ref $error and $error->isa("Config::Model::Exception::Syntax")
ref $error
$error->isa("Config::Model::Exception::Syntax")
Truth table33%1/3
A and B
A= ref $errorB= $error->isa("Config::Model::Exception::Syntax")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | $error->parsed_file( $file_path) unless $error->parsed_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | $error->rethrow; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | ▶ | elsif ( ref $error and $error->isa('Config::Model::Exception') ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $error and $error->isa("Config::Model::Exception")
MC/DC0%0/2
ref $error and $error->isa("Config::Model::Exception")
ref $error
$error->isa("Config::Model::Exception")
Truth table33%1/3
A and B
A= ref $errorB= $error->isa("Config::Model::Exception")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $error->rethrow ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | elsif ( ref $error ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | die $error ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | elsif ( $error ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | die "Backend $backend failed to read $file_path: $error"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # only backend based on C::M::Backend::Any can support annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 100 | ▶ | if ($backend_obj->can('annotation')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 100 | $self->{support_annotation} = $backend_obj->annotation ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 100 | return ( $res, $file_path ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 98 | ▶ | sub auto_write_init ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auto_write_init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 98 | my $rw_config = dclone $self->rw_config ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 98 | my $instance = $self->node->instance(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # root override is passed by the instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 98 | my $root_dir = $instance->root_dir; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 98 | my $backend = $rw_config->{backend}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 98 | my $write_dir = $self->get_tuned_config_dir(%$rw_config); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 98 | $logger->trace( "auto_write_init creating write cb ($backend) for ", $self->node->name ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 98 | my @wr_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | %$rw_config, # model data | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | config_dir => $write_dir, # override from instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | write => 1, # for get_cfg_file_path | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | root => $root_dir, # override from instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # used bby C::M::Dumper and C::M::DumpAsData | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 98 | $self->{auto_write}{$backend} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 98 | my $wb; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 98 | ▶ | my $f = $rw_config->{function} || 'write'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rw_config->{'function'} || 'write'
MC/DC0%0/1
$rw_config->{'function'} || 'write'
$rw_config->{'function'}
Truth table50%1/2
A
A= $rw_config->{'function'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 98 | my $backend_class = $self->load_backend_class( $backend, $f ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 98 | my $location = $self->node->name; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 98 | my $node = $self->node; # closure | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | # provide a proper write back function | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 56 | ▶ | $wb = sub (%cb_args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 56 | my $force_delete = delete $cb_args{force_delete} ; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 56 | ▶ | $logger->debug( "write cb ($backend) called for $location ", $force_delete ? '' : ' (deleted)' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 56 | my $backend_obj = $self->backend_obj(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 56 | my ($fh, $file_ok, $file_path ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 56 | ▶ | if (not $backend_class->skip_open) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 40 | ( $file_ok, $file_path ) = $self->get_cfg_file_path( @wr_args, %cb_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 56 | ▶ | if ($file_ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 35 | $fh = $self->open_file_to_write( $backend, $file_path, delete $cb_args{backup} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # override needed for "save as" button | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 56 | my %backend_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | @wr_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | file_path => $file_path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | object => $node, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | %cb_args # override from user | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 56 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 56 | ▶ | if ($force_delete) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | $backend_obj->delete(%backend_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 53 | $res = eval { $backend_obj->$f( %backend_args ); }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 53 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 53 | ▶ | $logger->error( "write backend $backend $backend_class" . '::' . "$f failed: $error" ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | if $error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 53 | $self->close_file_to_write( $error, $file_path, $rw_config->{file_mode} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | $self->auto_delete($file_path, \%backend_args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 53 | ▶ | if $rw_config->{auto_delete} and not $backend_class->skip_open ; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rw_config->{"auto_delete"} and not $backend_class->skip_open
MC/DC100%2/2
$rw_config->{"auto_delete"} and not $backend_class->skip_open
$rw_config->{"auto_delete"}
not $backend_class->skip_open
Truth table100%3/3
A and not(B)
A= $rw_config->{"auto_delete"}B= not $backend_class->skip_open
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 56 | ▶ | return defined $res ? $res : $@ ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 98 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 98 | $logger->trace( "registering write $backend in node " . $self->node->name ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 98 | $instance->register_write_back( $self->node->location, $backend, $wb ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 98 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 8 | ▶ | sub auto_delete ($self, $file_path, $args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auto_delete: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 8 | ▶ | return unless $file_path; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 3 | my $perl_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 3 | ▶ | $perl_data = $self->node->dump_as_data( full_dump => $args->{full_dump} // 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$args->{"full_dump"} // 0
MC/DC0%0/1
$args->{"full_dump"} // 0
$args->{"full_dump"}
Truth table50%1/2
A
A= $args->{"full_dump"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | if defined $self->node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 3 | ▶ | my $size = ref($perl_data) eq 'HASH' ? scalar keys %$perl_data | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | : ref($perl_data) eq 'ARRAY' ? scalar @$perl_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | : $perl_data ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 3 | ▶ | if (not $size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 2 | $logger->info( "Removing $file_path (no data to store)" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 2 | unlink($file_path); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 35 | ▶ | sub open_file_to_write ($self, $backend, $file_path, $backup) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_file_to_write: called CC=6 SCAR=21.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 35 | my $do_backup = defined $backup; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 35 | ▶ | $backup ||= 'old'; # use old only if defined | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$backup ||= "old"
MC/DC0%0/1
$backup ||= "old"
$backup
Truth table50%1/2
A
A= $backup
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 35 | ▶ | $backup = '.' . $backup unless $backup =~ /^\./; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # make sure that parent dir exists before creating file | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 35 | $file_path->parent->mkpath; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 35 | ▶ | if ( $do_backup and $file_path->is_file ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$do_backup and $file_path->is_file
MC/DC0%0/2
$do_backup and $file_path->is_file
$do_backup
$file_path->is_file
Truth table33%1/3
A and B
A= $do_backupB= $file_path->is_file
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | ▶ | $file_path->copy( $file_path.$backup ) or die "Backup copy failed: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 35 | $logger->debug("$backend backend opened file $file_path to write"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 35 | return $file_path->filehandle(">",":utf8"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 53 | ▶ | sub close_file_to_write ($self, $error, $file_path, $file_mode) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close_file_to_write: called CC=8 SCAR=28.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 53 | ▶ | return unless defined $file_path; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 35 | ▶ | if ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | # restore backup and display error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | $logger->warn("Error during write, restoring backup data in $file_path" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $file_path->append_utf8({ truncate => 1 }, $self->file_backup ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | $error->rethrow if ref($error) and $error->can('rethrow'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $error and $error->can("rethrow")
MC/DC0%0/2
ref $error and $error->can("rethrow")
ref $error
$error->can("rethrow")
Truth table0%0/3
A and B
A= ref $errorB= $error->can("rethrow")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | die $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # TODO: move chmod in a backend role | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 35 | ▶ | $file_path->chmod($file_mode) if $file_mode; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | # TODO: move in a backend role | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | # check file size and remove empty files | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 35 | ▶ | $file_path->remove if -z $file_path and not -l $file_path; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-z $file_path and not -l $file_path
MC/DC0%0/2
-z $file_path and not -l $file_path
-z $file_path
not -l $file_path
Truth table33%1/3
A and not(B)
A= -z $file_pathB= not -l $file_path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 35 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 1 | ▶ | sub is_auto_write_for_type ($self, $type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_auto_write_for_type: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 1 | ▶ | return $self->{auto_write}{$type} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'auto_write'}{$type} || 0
MC/DC0%0/1
$self->{'auto_write'}{$type} || 0
$self->{'auto_write'}{$type}
Truth table50%1/2
A
A= $self->{'auto_write'}{$type}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 59 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 59 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | # ABSTRACT: Load configuration node on demand | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||