/usr/share/perl5/Config/Model/DumpAsData.pm is in libconfig-model-perl 2.117-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | #
# This file is part of Config-Model
#
# This software is Copyright (c) 2005-2018 by Dominique Dumont.
#
# This is free software, licensed under:
#
# The GNU Lesser General Public License, Version 2.1, February 1999
#
package Config::Model::DumpAsData;
$Config::Model::DumpAsData::VERSION = '2.117';
use Carp;
use strict;
use warnings;
use 5.10.1;
use Config::Model::Exception;
use Config::Model::ObjTreeScanner;
sub new {
bless {}, shift;
}
sub dump_as_data {
my $self = shift;
my %args = @_;
my $dump_node = delete $args{node}
|| croak "dump_as_data: missing 'node' parameter";
my $fetch_mode = delete $args{mode} ;
my $skip_aw = delete $args{skip_auto_write} || '';
my $auto_v = delete $args{auto_vivify} || 0;
my $ordered_hash_as_list = delete $args{ordered_hash_as_list};
$ordered_hash_as_list = 1 unless defined $ordered_hash_as_list;
# mode and full_dump params are both accepted
my $full = delete $args{full_dump} // 1;
$fetch_mode //= 'non_upstream_default' if $full;
$fetch_mode //= 'custom';
my $std_cb = sub {
my ( $scanner, $data_r, $obj, $element, $index, $value_obj ) = @_;
$$data_r = $value_obj->fetch(mode => $fetch_mode);
};
my $check_list_element_cb = sub {
my ( $scanner, $data_r, $node, $element_name, @check_items ) = @_;
my $a_ref = $node->fetch_element($element_name)->get_checked_list;
# don't store empty checklist
$$data_r = $a_ref if @$a_ref;
};
my $hash_element_cb = sub {
my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_;
my $force_write = $node->fetch_element($element_name)->write_empty_value;
# resume exploration but pass a ref on $data_ref hash element
# instead of data_ref
my %h;
my @res;
foreach my $k (@keys) {
my $v;
$scanner->scan_hash( \$v, $node, $element_name, $k );
# don't create the key if $v is undef
if (defined $v or $force_write) {
$h{$k} = $v;
push @res , $k, $v;
}
} ;
my $ordered_hash = $node->fetch_element($element_name)->ordered;
if ( $ordered_hash and $ordered_hash_as_list ) {
$$data_ref = \@res if @res;
}
else {
$h{'__'.$element_name.'_order'} = \@keys if $ordered_hash and @keys;
$$data_ref = \%h if scalar %h;
}
};
my $list_element_cb = sub {
my ( $scanner, $data_ref, $node, $element_name, @idx ) = @_;
# resume exploration but pass a ref on $data_ref hash element
# instead of data_ref
my @a;
foreach my $i (@idx) {
my $v;
$scanner->scan_hash( \$v, $node, $element_name, $i );
push @a, $v if defined $v;
}
$$data_ref = \@a if scalar @a;
};
my $node_content_cb = sub {
my ( $scanner, $data_ref, $node, @element ) = @_;
my %h;
foreach my $e (@element) {
my $v;
$scanner->scan_element( \$v, $node, $e );
$h{$e} = $v if defined $v;
}
$$data_ref = \%h if scalar %h;
};
my $node_element_cb = sub {
my ( $scanner, $data_ref, $node, $element_name, $key, $next ) = @_;
return if $skip_aw and $next->is_auto_write_for_type($skip_aw);
$scanner->scan_node( $data_ref, $next );
};
my @scan_args = (
check => delete $args{check} || 'yes',
fallback => 'all',
auto_vivify => $auto_v,
list_element_cb => $list_element_cb,
check_list_element_cb => $check_list_element_cb,
hash_element_cb => $hash_element_cb,
leaf_cb => $std_cb,
node_element_cb => $node_element_cb,
node_content_cb => $node_content_cb,
);
my @left = keys %args;
croak "DumpAsData: unknown parameter:@left" if @left;
# perform the scan
my $view_scanner = Config::Model::ObjTreeScanner->new(@scan_args);
my $obj_type = $dump_node->get_type;
my $result;
my $p = $dump_node->parent;
my $e = $dump_node->element_name;
my $i = $dump_node->index_value; # defined only for hash and list
if ( $obj_type =~ /node/ ) {
$view_scanner->scan_node( \$result, $dump_node );
}
elsif ( defined $i ) {
$view_scanner->scan_hash( \$result, $p, $e, $i );
}
elsif ($obj_type eq 'list'
or $obj_type eq 'hash'
or $obj_type eq 'leaf'
or $obj_type eq 'check_list' ) {
$view_scanner->scan_element( \$result, $p, $e );
}
else {
croak "dump_as_data: unexpected type: $obj_type";
}
return $result;
}
sub dump_annotations_as_pod {
my $self = shift;
my %args = @_;
my $dump_node = delete $args{node}
|| croak "dump_annotations_as_pod: missing 'node' parameter";
my $annotation_to_pod = sub {
my $obj = shift;
my $path = shift || $obj->location;
my $a = $obj->annotation;
if ($a) {
chomp $a;
return "=item $path\n\n$a\n\n";
}
else {
return '';
}
};
my $std_cb = sub {
my ( $scanner, $data_r, $obj, $element, $index, $value_obj ) = @_;
$$data_r .= $annotation_to_pod->($value_obj);
};
my $hash_element_cb = sub {
my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_;
my $h = $node->fetch_element($element_name);
my $h_path = $h->location . ':';
foreach (@keys) {
$$data_ref .= $annotation_to_pod->( $h->fetch_with_id($_), $h_path . $_ );
$scanner->scan_hash( $data_ref, $node, $element_name, $_ );
}
};
my $node_content_cb = sub {
my ( $scanner, $data_ref, $node, @element ) = @_;
my $node_path = $node->location;
$node_path .= ' ' if $node_path;
foreach (@element) {
$$data_ref .= $annotation_to_pod->(
$node->fetch_element( name => $_, check => 'no' ),
$node_path . $_
);
$scanner->scan_element( $data_ref, $node, $_ );
}
};
my @scan_args = (
check => delete $args{check} || 'yes',
fallback => 'all',
leaf_cb => $std_cb,
node_content_cb => $node_content_cb,
hash_element_cb => $hash_element_cb,
list_element_cb => $hash_element_cb,
);
my @left = keys %args;
croak "dump_annotations_as_pod: unknown parameter:@left" if @left;
# perform the scan
my $view_scanner = Config::Model::ObjTreeScanner->new(@scan_args);
my $obj_type = $dump_node->get_type;
my $result = '';
my $a = $dump_node->annotation;
my $l = $dump_node->location;
$result .= "=item $l\n\n$a\n\n" if $a;
if ( $obj_type =~ /node/ ) {
$view_scanner->scan_node( \$result, $dump_node );
}
else {
croak "dump_annotations_as_pod: unexpected type: $obj_type";
}
return '' unless $result;
return "=head1 Annotations\n\n=over\n\n" . $result . "=back\n\n";
}
1;
# ABSTRACT: Dump configuration content as a perl data structure
__END__
=pod
=encoding UTF-8
=head1 NAME
Config::Model::DumpAsData - Dump configuration content as a perl data structure
=head1 VERSION
version 2.117
=head1 SYNOPSIS
use Config::Model ;
use Data::Dumper ;
# define configuration tree object
my $model = Config::Model->new ;
$model ->create_config_class (
name => "MyClass",
element => [
[qw/foo bar/] => {
type => 'leaf',
value_type => 'string'
},
baz => {
type => 'hash',
index_type => 'string' ,
cargo => {
type => 'leaf',
value_type => 'string',
},
},
],
) ;
my $inst = $model->instance(root_class_name => 'MyClass' );
my $root = $inst->config_root ;
# put some data in config tree the hard way
$root->fetch_element('foo')->store('yada') ;
$root->fetch_element('bar')->store('bla bla') ;
$root->fetch_element('baz')->fetch_with_id('en')->store('hello') ;
# put more data the easy way
my $steps = 'baz:fr=bonjour baz:hr="dobar dan"';
$root->load( steps => $steps ) ;
print Dumper($root->dump_as_data);
# $VAR1 = {
# 'bar' => 'bla bla',
# 'baz' => {
# 'en' => 'hello',
# 'fr' => 'bonjour',
# 'hr' => 'dobar dan'
# },
# 'foo' => 'yada'
# };
=head1 DESCRIPTION
This module is used directly by L<Config::Model::Node> to dump the content
of a configuration tree in perl data structure.
The perl data structure is a hash of hash. Only
L<CheckList|Config::Model::CheckList> content is stored in an array ref.
Note that undefined values are skipped for list element. I.e. if a
list element contains C<('a',undef,'b')>, the data structure then
contains C<'a','b'>.
=head1 CONSTRUCTOR
=head2 new ( )
No parameter. The constructor should be used only by
L<Config::Model::Node>.
=head1 Methods
=head2 dump_as_data(...)
Return a perl data structure
Parameters are:
=over
=item node
Reference to a L<Config::Model::Node> object. Mandatory
=item full_dump
Also dump default values in the data structure. Useful if the dumped
configuration data is used by the application. (default is yes)
Note that C<mode> parameter is also accepted and overrides
C<full_dump> parameter. See L<Config::Model::Value/fetch(...)> for
details on C<mode>.
=item skip_auto_write
Skip node that have a C<perl write> capability in their model. See
L<Config::Model::BackendMgr>.
This option must be used when using DumpAsData: to write back
configuration data. When a configuration model contains several
backends (one at the tree root and others in tree nodes), setting this
option ensure that the "root" configuration file does not contain data
duplicated in configuration file of others tree nodes.
=item auto_vivify
Scan and create data for nodes elements even if no actual data was
stored in them. This may be useful to trap missing mandatory values.
=item ordered_hash_as_list
By default, ordered hash (i.e. the order of the keys are important)
are dumped as Perl list. This is the faster way to dump such hashed
while keeping the key order. But it's the less readable way.
When this parameter is 1 (default), the ordered hash is dumped as a
list:
my_hash => [ A => 'foo', B => 'bar', C => 'baz' ]
When this parameter is set as 0, the ordered hash is dumped with a
special key that specifies the order of keys. E.g.:
my_hash => {
__my_hash_order => [ 'A', 'B', 'C' ] ,
B => 'bar', A => 'foo', C => 'baz'
}
=back
=head1 Methods
=head2 dump_annotations_as_pod(...)
Return a string formatted in pod (See L<perlpod>) with the annotations.
Parameters are:
=over
=item node
Reference to a L<Config::Model::Node> object. Mandatory
=item check_list
Yes, no or skip
=back
=head1 AUTHOR
Dominique Dumont, (ddumont at cpan dot org)
=head1 SEE ALSO
L<Config::Model>,L<Config::Model::Node>,L<Config::Model::ObjTreeScanner>
=head1 AUTHOR
Dominique Dumont
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2005-2018 by Dominique Dumont.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999
=cut
|