| 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-2021 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.012'; | 
| 12 | 2 |  |  | 2 |  | 11009 | use Mouse ; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 16 |  | 
| 13 | 2 |  |  | 2 |  | 1116 | use Log::Log4perl qw(get_logger :levels); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 16 |  | 
| 14 | 2 |  |  | 2 |  | 264 | use Carp ; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 129 |  | 
| 15 | 2 |  |  | 2 |  | 563 | use File::Copy ; | 
|  | 2 |  |  |  |  | 2543 |  | 
|  | 2 |  |  |  |  | 115 |  | 
| 16 | 2 |  |  | 2 |  | 12 | use File::Path ; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 98 |  | 
| 17 | 2 |  |  | 2 |  | 46 | use 5.010 ; | 
|  | 2 |  |  |  |  | 9 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | extends 'Config::Model::Backend::Any'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub annotation { | 
| 22 | 3 |  |  | 3 | 1 | 202 | return 1 ; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | my $logger = Log::Log4perl::get_logger('Backend::Approx'); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub read { | 
| 28 | 3 |  |  | 3 | 1 | 12523 | my $self = shift ; | 
| 29 | 3 |  |  |  |  | 29 | 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 |  |  |  | 32 | $logger->info("loading config file $args{file}") if defined $args{file}; | 
| 40 | 3 |  |  |  |  | 40 | my @lines = $args{file_path}->lines_utf8 ; | 
| 41 | 3 |  |  |  |  | 589 | my $global = $self->read_global_comments(\@lines, '#') ; | 
| 42 | 3 |  |  |  |  | 317 | $self->node->annotation($global) ; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 3 |  |  |  |  | 105 | my @data = $self->associates_comments_with_data(\@lines, '#') ; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 3 |  |  |  |  | 728 | foreach my $item (@data) { | 
| 47 | 15 |  |  |  |  | 287 | my ($line,$note) = @$item ; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 15 |  |  |  |  | 98 | my ($k,$v) = split /\s+/,$line,2 ; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 15 | 100 |  |  |  | 111 | my $step = ($k =~ s/^\$//) ? $k | 
|  |  | 100 |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | : ($v =~ m!://!)  ? "distributions:".$k | 
| 53 |  |  |  |  |  |  | :                 $k ; # old style parameter | 
| 54 | 15 |  |  |  |  | 91 | my $leaf = $self->node->grab(step => $step) ; | 
| 55 | 15 |  |  |  |  | 87747 | $leaf->store($v) ; | 
| 56 | 15 |  |  |  |  | 7603 | $leaf->annotation($note) ; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 3 |  |  |  |  | 115 | return 1; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub write { | 
| 63 | 2 |  |  | 2 | 1 | 52727 | my $self = shift ; | 
| 64 | 2 |  |  |  |  | 28 | my %args = @_ ; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 |  |  |  |  | 20 | $logger->info("writing config file $args{file}"); | 
| 67 | 2 |  |  |  |  | 25 | my $node = $args{object} ; | 
| 68 | 2 |  |  |  |  | 29 | my $res = $self->write_global_comment('#'); | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # Using Config::Model::ObjTreeScanner would be overkill | 
| 71 | 2 |  |  |  |  | 115 | foreach my $elt ($node->get_element_name) { | 
| 72 | 26 | 100 |  |  |  | 565 | next if $elt eq 'distributions'; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # write value | 
| 75 | 24 |  |  |  |  | 85 | my $obj = $node->grab($elt) ; | 
| 76 | 24 |  |  |  |  | 7594 | my $v = $obj->fetch ; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 24 | 100 |  |  |  | 18346 | if (defined $v) { | 
| 79 | 4 | 100 |  |  |  | 17 | $res .= sprintf("# %s\n", $obj->annotation) if $obj->annotation; | 
| 80 | 4 |  |  |  |  | 108 | $res .= sprintf("\$%-10s %s\n\n",$elt,$v) ; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 2 |  |  |  |  | 16 | my $h = $node->fetch_element('distributions') ; | 
| 85 | 2 |  |  |  |  | 134 | foreach my $dname ($h->fetch_all_indexes) { | 
| 86 | 6 |  |  |  |  | 1179 | my $d = $node->grab("distributions:$dname") ; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 6 |  |  |  |  | 2378 | my $note = $d->annotation; | 
| 89 | 6 | 100 |  |  |  | 69 | $res .= "# $note\n" if $note; | 
| 90 | 6 |  |  |  |  | 22 | $res .= sprintf("%-10s %s\n",$dname,$d->fetch) ; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 2 |  |  |  |  | 533 | $args{file_path}->spew_utf8($res); | 
| 94 | 2 |  |  |  |  | 1325 | return 1; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | 1; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # ABSTRACT: Read and write Approx configuration file | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | __END__ | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =pod | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =encoding UTF-8 | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 NAME | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | Config::Model::Backend::Approx - Read and write Approx configuration file | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head1 VERSION | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | version 1.012 | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # This backend is loaded by Config::Model::Node | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | This module provides a backend to read and write configuration files for Approx. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head1 Methods | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =head2 read | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | Read F<approx.conf> and load the data in the C<approx_root> | 
| 128 |  |  |  |  |  |  | configuration tree. | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head2 write | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Write data from the C<approx_root> configuration tree into | 
| 133 |  |  |  |  |  |  | F<approx.conf>. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | L<cme>, L<Config::Model::Backend::Any>, | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head1 AUTHOR | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | Dominique Dumont | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | This software is Copyright (c) 2015-2021 by Dominique Dumont. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | This is free software, licensed under: | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | The GNU Lesser General Public License, Version 2.1, February 1999 | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =cut |