| 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::Annotation 2.169; | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use v5.20; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | ▶ | use Mouse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 1 | ▶ | use English; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | ▶ | use Path::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | ▶ | use Config::Model::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 1 | ▶ | use Config::Model::Exception; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 1 | ▶ | use Config::Model::Node; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 1 | ▶ | use Config::Model::ObjTreeScanner; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 1 | ▶ | use strict ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 1 | ▶ | use feature qw/postderef signatures/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 1 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | ▶ | use Carp qw/croak confess cluck/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | #my $logger = get_logger("Annotation") ; | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | has 'instance' => ( is => 'ro', isa => 'Config::Model::Instance', required => 1 ); | |||||||||||||
| 37 | 1 | has 'config_class_name' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||
| 38 | 1 | has 'file' => ( is => 'ro', isa => 'Path::Tiny', lazy => 1, builder => '_set_file' ); | |||||||||||||
| 39 | 1 | has 'dir' => ( is => 'ro', isa => 'Path::Tiny', lazy => 1, builder => '_set_dir' ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 1 | has 'root_dir' => ( | |||||||||||||
| 42 | is => 'ro', | ||||||||||||||
| 43 | isa => 'Config::Model::TypeContraints::Path', | ||||||||||||||
| 44 | coerce => 1 | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 2 | ▶ | sub _set_file ($self) { | ||||||||||||
|
Subroutine
_set_file: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | 2 | return $self->dir->child( $self->config_class_name . '-note.pl'); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | ▶ | sub _set_dir ($self) { | ||||||||||||
|
Subroutine
_set_dir: called CC=3 SCAR=16.0
| |||||||||||||||
| 52 | return | ||||||||||||||
| 53 | 2 | ▶ | $self->root_dir ? $self->root_dir->child('config-model') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 54 | : $EUID ? path("/var/lib/config-model") | ||||||||||||||
| 55 | : path("~/.config-model"); | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 1 | ▶ | sub save ($self) { | ||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 59 | 1 | my $dir = $self->dir; | |||||||||||||
| 60 | 1 | $dir->mkpath; | |||||||||||||
| 61 | 1 | my $h = $self->get_annotation_hash; | |||||||||||||
| 62 | 1 | $self->file->spew_utf8( Dumper($h) ); | |||||||||||||
| 63 | 1 | return; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 4 | ▶ | sub get_annotation_hash ($self) { | ||||||||||||
|
Subroutine
get_annotation_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 67 | 4 | my %data; | |||||||||||||
| 68 | 4 | my $scanner = Config::Model::ObjTreeScanner->new( | |||||||||||||
| 69 | leaf_cb => \&my_leaf_cb, | ||||||||||||||
| 70 | hash_element_cb => \&my_hash_element_cb, | ||||||||||||||
| 71 | list_element_cb => \&my_list_element_cb, | ||||||||||||||
| 72 | node_element_cb => \&my_node_element_cb, | ||||||||||||||
| 73 | ); | ||||||||||||||
| 74 | 4 | my $root = $self->instance->config_root; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 4 | $scanner->scan_node( \%data, $root ); | |||||||||||||
| 77 | 4 | return \%data; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | # WARNING: not a method | ||||||||||||||
| 81 | 24 | ▶ | sub my_hash_element_cb { | ||||||||||||
|
Subroutine
my_hash_element_cb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 82 | 24 | my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_; | |||||||||||||
| 83 | |||||||||||||||
| 84 | # custom code using $data_ref | ||||||||||||||
| 85 | 24 | store_note_in_data( $data_ref, $node->fetch_element($element_name) ); | |||||||||||||
| 86 | |||||||||||||||
| 87 | # resume exploration | ||||||||||||||
| 88 | 24 | map { $scanner->scan_hash( $data_ref, $node, $element_name, $_ ) } @keys; | |||||||||||||
| 89 | 24 | return; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | # WARNING: not a method | ||||||||||||||
| 93 | 54 | ▶ | sub my_node_element_cb { | ||||||||||||
|
Subroutine
my_node_element_cb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 94 | 54 | my ( $scanner, $data_ref, $node, $element_name, $key, $contained_node ) = @_; | |||||||||||||
| 95 | |||||||||||||||
| 96 | # your custom code using $data_ref | ||||||||||||||
| 97 | 54 | store_note_in_data( $data_ref, $contained_node ); | |||||||||||||
| 98 | |||||||||||||||
| 99 | # explore next node | ||||||||||||||
| 100 | 54 | $scanner->scan_node( $data_ref, $contained_node ); | |||||||||||||
| 101 | 54 | return; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # WARNING: not a method | ||||||||||||||
| 105 | 12 | ▶ | sub my_list_element_cb { | ||||||||||||
|
Subroutine
my_list_element_cb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 106 | 12 | my ( $scanner, $data_ref, $node, $element_name, @idx ) = @_; | |||||||||||||
| 107 | |||||||||||||||
| 108 | # custom code using $data_ref | ||||||||||||||
| 109 | 12 | store_note_in_data( $data_ref, $node->fetch_element($element_name) ); | |||||||||||||
| 110 | |||||||||||||||
| 111 | # resume exploration (if needed) | ||||||||||||||
| 112 | 12 | map { $scanner->scan_list( $data_ref, $node, $element_name, $_ ) } @idx; | |||||||||||||
| 113 | |||||||||||||||
| 114 | # note: scan_list and scan_hash are equivalent | ||||||||||||||
| 115 | 12 | return; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | # WARNING: not a method | ||||||||||||||
| 119 | 271 | ▶ | sub my_leaf_cb { | ||||||||||||
|
Subroutine
my_leaf_cb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 120 | 271 | my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_; | |||||||||||||
| 121 | 271 | store_note_in_data( $data_ref, $leaf_object ); | |||||||||||||
| 122 | 271 | return; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # WARNING: not a method | ||||||||||||||
| 126 | 361 | ▶ | sub store_note_in_data { | ||||||||||||
|
Subroutine
store_note_in_data: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 127 | 361 | my ( $data_ref, $obj ) = @_; | |||||||||||||
| 128 | |||||||||||||||
| 129 | 361 | my $note = $obj->annotation; | |||||||||||||
| 130 | 361 | ▶ | return unless $note; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | |||||||||||||||
| 132 | 22 | my $key = $obj->location; | |||||||||||||
| 133 | 22 | $data_ref->{$key} = $note; | |||||||||||||
| 134 | 22 | return; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 2 | ▶ | sub load ($self) { | ||||||||||||
|
Subroutine
load: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 138 | 2 | my $f = $self->file; | |||||||||||||
| 139 | 2 | ▶ | return unless $f->exists; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | 2 | ▶ | my $hash = do "./$f" || croak "can't do $f:$!"; | ||||||||||||
|
Condition50%1/2
do "./$f" || croak("can't do ${f}:$!")
MC/DC0%0/1
do "./$f" || croak("can't do ${f}:$!")
do "./$f"
Truth table50%1/2
A
A= do "./$f"
| |||||||||||||||
| 141 | 2 | my $root = $self->instance->config_root; | |||||||||||||
| 142 | |||||||||||||||
| 143 | 2 | foreach my $path ( keys %$hash ) { | |||||||||||||
| 144 | 14 | my $obj = eval { $root->grab( step => $path, autoadd => 0 ) }; | |||||||||||||
| 145 | 14 | ▶ | next if $@; # skip annotation of unknown elements | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 8 | $obj->annotation( $hash->{$path} ); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | 2 | return; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 1 | ▶ | no Mouse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 154 | |||||||||||||||
| 155 | 1 | 1; | |||||||||||||
| 156 | |||||||||||||||
| 157 | # ABSTRACT: Read and write configuration annotations | ||||||||||||||
| 158 | |||||||||||||||
| 159 | __END__ | ||||||||||||||