| 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::Backend::Any 2.169; | ||||||||||
| 11 | |||||||||||
| 12 | 9 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 9 | ▶ | use Config::Model::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 9 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 9 | ▶ | use File::Path; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 9 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 9 | ▶ | use feature qw/postderef signatures/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 9 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 9 | my $logger = get_logger("Backend"); | |||||||||
| 25 | |||||||||||
| 26 | 9 | has 'name' => ( is => 'ro', default => 'unknown', ); | |||||||||
| 27 | 9 | has [qw/annotation auto_create auto_delete/] => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||
| 28 | 9 | has 'node' => ( | |||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | isa => 'Config::Model::Node', | ||||||||||
| 31 | weak_ref => 1, | ||||||||||
| 32 | required => 1, | ||||||||||
| 33 | handles => [ qw/show_message instance get_element_names/], | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 190 | ▶ | sub skip_open { return 0; } | ||||||||
|
Subroutine
skip_open: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | |||||||||||
| 38 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||
| 39 | 0 | ▶ | sub read { | ||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 0 | my $self = shift; | |||||||||
| 41 | 0 | my $err = "Internal error: read not defined in backend $self->{name}."; | |||||||||
| 42 | 0 | $logger->error($err); | |||||||||
| 43 | 0 | croak $err; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub write { | ||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 47 | 0 | my $self = shift; | |||||||||
| 48 | 0 | my $err = "Internal error: write not defined in backend $self->{name}."; | |||||||||
| 49 | 0 | $logger->error($err); | |||||||||
| 50 | 0 | croak $err; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 44 | ▶ | sub read_global_comments { | ||||||||
|
Subroutine
read_global_comments: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||
| 54 | 44 | my $self = shift; | |||||||||
| 55 | 44 | my $lines = shift; | |||||||||
| 56 | 44 | my $cc = shift; # comment character(s) | |||||||||
| 57 | |||||||||||
| 58 | 44 | ▶ | my $cc_re = length $cc > 1 ? "[$cc]" : $cc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 44 | my @global_comments; | |||||||||
| 60 | my @global_comment_lines; | ||||||||||
| 61 | |||||||||||
| 62 | 44 | ▶ | while ( defined( my $l = shift @$lines ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 102 | ▶ | next if $l =~ /^$cc_re{2}/; # remove comments added by Config::Model | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 44 | unshift @$lines, $l; | |||||||||
| 65 | 44 | last; | |||||||||
| 66 | } | ||||||||||
| 67 | 44 | ▶ | while ( defined( my $l = shift @$lines ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 63 | ▶ | next if $l =~ /^\s*$/; # remove empty lines | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 44 | unshift @$lines, $l; | |||||||||
| 70 | 44 | last; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 44 | ▶ | while ( defined( my $l = shift @$lines ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 83 | chomp $l; | |||||||||
| 75 | |||||||||||
| 76 | 83 | my ( $data, $comment ) = split /\s*$cc_re\s?/, $l, 2; | |||||||||
| 77 | |||||||||||
| 78 | 83 | ▶ | if (defined $comment) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 40 | push @global_comment_lines, $l; | |||||||||
| 80 | 40 | push @global_comments, $comment; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 83 | ▶ | if ( $l =~ /^\s*$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | # we indeed had global comments which are now finished by | ||||||||||
| 85 | # a blank line. Store them and bail out | ||||||||||
| 86 | 17 | ▶ | if (@global_comments) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 17 | $self->node->annotation(@global_comments); | |||||||||
| 88 | 17 | $logger->debug("Setting global comment with @global_comments on ", $self->node->name); | |||||||||
| 89 | } | ||||||||||
| 90 | # stop global comment at first blank line | ||||||||||
| 91 | 17 | last; | |||||||||
| 92 | } | ||||||||||
| 93 | 66 | ▶ | if ( $data ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | # The comment found is not global, put back line and any captured comment | ||||||||||
| 95 | 27 | unshift @$lines, @global_comment_lines, $l; | |||||||||
| 96 | |||||||||||
| 97 | # stop global comment | ||||||||||
| 98 | 27 | last; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | 44 | return; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 43 | ▶ | sub associates_comments_with_data { | ||||||||
|
Subroutine
associates_comments_with_data: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||
| 105 | 43 | my $self = shift; | |||||||||
| 106 | 43 | my $lines = shift; | |||||||||
| 107 | 43 | my $cc = shift; # comment character(s) | |||||||||
| 108 | |||||||||||
| 109 | 43 | ▶ | my $cc_re = length $cc > 1 ? "[$cc]" : $cc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 43 | my @result; | |||||||||
| 111 | my @comments; | ||||||||||
| 112 | 43 | foreach my $l (@$lines) { | |||||||||
| 113 | 460 | ▶ | next if $l =~ /^$cc_re{2}/; # remove comments added by Config::Model | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 457 | chomp $l; | |||||||||
| 115 | |||||||||||
| 116 | 457 | my ( $data, $comment ) = split /\s*$cc_re\s?/, $l, 2; | |||||||||
| 117 | 457 | ▶ | push @comments, $comment if defined $comment; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 457 | ▶ | next unless defined $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 355 | $data =~ s/^\s+//g; | |||||||||
| 121 | 355 | $data =~ s/\s+$//g; | |||||||||
| 122 | |||||||||||
| 123 | 355 | ▶ | if ($data) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 248 | my $note = ''; | |||||||||
| 125 | 248 | ▶ | $note = join( "\n", @comments ) if @comments; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 248 | $logger->trace("associates_comments_with_data: '$note' with '$data'"); | |||||||||
| 127 | 248 | push @result, [ $data, $note ]; | |||||||||
| 128 | 248 | @comments = (); | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 43 | ▶ | return wantarray ? @result : \@result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 26 | ▶ | sub write_global_comment { | ||||||||
|
Subroutine
write_global_comment: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 137 | 26 | goto &write_global_comments; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 26 | ▶ | sub write_global_comments ($self, $cc) { | ||||||||
|
Subroutine
write_global_comments: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 141 | 26 | ▶ | croak "write_global_comments: no comment char specified" unless $cc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | # no need to mention 'cme list' if current application is found | ||||||||||
| 144 | 26 | my $app = $self->node->instance->application ; | |||||||||
| 145 | 26 | my $extra = '' ; | |||||||||
| 146 | 26 | ▶ | if (not $app) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 14 | $extra = "$cc$cc Run 'cme list' to get the list of applications" | |||||||||
| 148 | . " available on your system\n"; | ||||||||||
| 149 | 14 | $app = '<application>'; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 26 | my $res = "$cc$cc This file was written by cme command.\n" | |||||||||
| 153 | . "$cc$cc You can run 'cme edit $app' to modify this file.\n" | ||||||||||
| 154 | . $extra | ||||||||||
| 155 | . "$cc$cc You may also modify the content of this file with your favorite editor.\n\n"; | ||||||||||
| 156 | |||||||||||
| 157 | # write global comment | ||||||||||
| 158 | 26 | my $global_note = $self->node->annotation; | |||||||||
| 159 | 26 | ▶ | if ($global_note) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 11 | for ( split /\n/, $global_note ) { $res .= "$cc $_\n" } | |||||||||
| 161 | 11 | $res .= "\n"; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 26 | return $res; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | # $cc can be undef when writing a list on a single line | ||||||||||
| 168 | 137 | ▶ | sub write_data_and_comments ( $self, $cc, @data_and_comments ) { | ||||||||
|
Subroutine
write_data_and_comments: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 169 | 137 | my $res = ''; | |||||||||
| 170 | 137 | ▶ | while (@data_and_comments) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 137 | my ( $d, $c ) = splice @data_and_comments, 0, 2; | |||||||||
| 172 | 137 | ▶ | if ($c) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 63 | for (split /\n/, $c ) { $res .= "$cc $_\n" } | |||||||||
| 174 | } | ||||||||||
| 175 | 137 | ▶ | $res .= "$d\n" if defined $d; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | } | ||||||||||
| 177 | 137 | return $res; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||
| 181 | |||||||||||
| 182 | 9 | 1; | |||||||||
| 183 | |||||||||||
| 184 | # ABSTRACT: Virtual class for other backends | ||||||||||
| 185 | |||||||||||
| 186 | __END__ | ||||||||||