/usr/share/perl5/Config/Model/Instance.pm is in libconfig-model-perl 2.005-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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | #
# This file is part of Config-Model
#
# This software is Copyright (c) 2012 by Dominique Dumont, Krzysztof Tyszecki.
#
# This is free software, licensed under:
#
# The GNU Lesser General Public License, Version 2.1, February 1999
#
package Config::Model::Instance;
{
$Config::Model::Instance::VERSION = '2.005';
}
#use Scalar::Util qw(weaken) ;
use Any::Moose ;
use namespace::autoclean;
use Any::Moose '::Util::TypeConstraints';
use File::Path;
use Log::Log4perl qw(get_logger :levels);
use Config::Model::Annotation;
use Config::Model::Exception ;
use Config::Model::Node ;
use Config::Model::Loader;
use Config::Model::SearchElement;
use Config::Model::Iterator;
use Config::Model::ObjTreeScanner;
use warnings FATAL => qw(all);
use warnings::register ;
use Carp qw/carp croak confess cluck/;
my $logger = get_logger("Instance") ;
has [qw/root_class_name/] => (is => 'ro', isa => 'Str', required => 1) ;
has config_model => (
is => 'ro',
isa => 'Config::Model',
weak_ref => 1 ,
required => 1
) ;
has check => (
is => 'ro',
isa => 'Str',
default => 'yes',
reader => 'read_check' ,
) ;
# a unique (instance wise) placeholder for various tree objects
# to store information
has _safe => (
is => 'rw',
isa => 'HashRef',
traits => ['Hash'] ,
default => sub { {} } ,
handles => {
data => 'accessor' ,
},
) ;
# preset mode: to load values found by HW scan or other automatic scheme
# layered mode: to load values found in included files (e.g. a la multistrap)
has [qw/preset layered/] => (
is => 'ro',
isa => 'Bool' ,
default => 0,
);
has needs_save => (
is => 'rw',
isa => 'Bool' ,
default => 0,
);
# initial_load mode: when data is loaded the first time
has initial_load => (
is => 'ro',
isa => 'Bool' ,
default => 1,
) ;
# This array holds a set of sub ref that will be invoked when
# the users requires to write all configuration tree in their
# backend storage.
has _write_back => (
is => 'ro',
isa => 'ArrayRef',
traits => ['Array'],
handles => {
register_write_back => 'push' ,
count_write_back => 'count' , # mostly for tests
},
default => sub { [] } ,
);
# used for auto_read auto_write feature
has [qw/name root_dir config_file backend/] => (
is => 'ro',
isa => 'Maybe[Str]' ,
);
has skip_read => (is => 'ro', isa => 'Bool', default => 0) ;
sub BUILD {
my $self = shift ;
#carp "instance new: force_load is deprecated" if defined $args{force_load} ;
#$read_check = 'no' if delete $args{force_load} ;
# cleanup paths
map { $self->{$_} .= '/' if defined $self->{$_} and $self->{$_} !~ m!/$!}
qw/root_dir/;
}
has tree => (
is => 'ro',
isa => 'Config::Model::Node',
builder => 'reset_config' ,
reader => 'config_root' ,
);
sub reset_config {
my $self =shift;
return Config::Model::Node->new(
config_class_name => $self->{root_class_name},
instance => $self,
container => $self,
skip_read => $self->{skip_read},
config_file => $self->{config_file} ,
);
}
sub preset_start {
my $self = shift ;
$logger->info("Starting preset mode");
carp "Cannot start preset mode during layered mode"
if $self->{layered} ;
$self->{preset} = 1;
}
sub preset_stop {
my $self = shift ;
$logger->info("Stopping preset mode");
$self->{preset} = 0;
}
sub preset_clear {
my $self = shift ;
my $leaf_cb = sub {
my ($scanner, $data_ref,$node,$element_name,$index, $leaf_object) = @_ ;
$leaf_object->clear_preset ;
} ;
$self->_stuff_clear($leaf_cb) ;
}
sub layered_start {
my $self = shift ;
$logger->info("Starting layered mode");
carp "Cannot start layered mode during preset mode"
if $self->{preset} ;
$self->{layered} = 1;
}
sub layered_stop {
my $self = shift ;
$logger->info("Stopping layered mode");
$self->{layered} = 0;
}
sub layered_clear {
my $self = shift ;
my $leaf_cb = sub {
my ($scanner, $data_ref,$node,$element_name,$index, $leaf_object) = @_ ;
$$data_ref ||= $leaf_object->clear_layered ;
};
$self->_stuff_clear($leaf_cb);
}
sub get_data_mode {
my $self = shift;
return
$self->{layered} ? 'layered'
: $self->{preset} ? 'preset'
: 'normal';
}
sub _stuff_clear {
my ($self,$leaf_cb) = @_ ;
# this sub may remove hash keys that were entered by user if the
# corresponding hash value has no data.
# it also clear auto_created ids if there's no data in there
my $h_cb = sub {
my ($scanner, $data_ref,$node,$element_name,@keys) = @_ ;
my $obj = $node->fetch_element($element_name) ;
foreach my $k (@keys) {
my $has_data = 0;
$scanner->scan_hash(\$has_data,$node,$element_name,$k);
$obj->remove($k) unless $has_data ;
$$data_ref ||= $has_data ;
}
};
my $wiper = Config::Model::ObjTreeScanner->new (
fallback => 'all',
auto_vivify => 0,
check => 'skip' ,
leaf_cb => $leaf_cb ,
hash_element_cb => $h_cb,
list_element_cb => $h_cb,
);
$wiper->scan_node(undef,$self->config_root) ;
}
sub initial_load_start {
my $self = shift ;
$logger->info("Start initial_load mode");
$self->{initial_load} = 1;
}
sub initial_load_stop {
my $self = shift ;
$logger->info("Stopping initial_load mode");
$self->{initial_load} = 0;
}
sub load {
my $self = shift ;
my $loader = Config::Model::Loader->new ;
my %args = @_ eq 1 ? (step => $_[0]) : @_ ;
$loader->load(node => $self->{tree}, %args) ;
}
sub search_element {
my $self = shift ;
$self->{tree}->search_element(@_) ;
}
sub wizard_helper {
carp __PACKAGE__,"::wizard_helper helped is deprecated. Call iterator instead" ;
goto &iterator ;
}
sub iterator {
my $self = shift ;
my @args = @_ ;
my $tree_root = $self->config_root ;
return Config::Model::Iterator->new ( root => $tree_root, @args) ;
}
sub read_directory {
carp "read_directory is deprecated";
return shift -> {root_dir} ;
}
sub read_root_dir {
return shift -> {root_dir} ;
}
sub write_directory {
my $self = shift ;
carp "write_directory is deprecated";
return $self -> {root_dir} ;
}
sub write_root_dir {
my $self = shift ;
return $self -> {root_dir} ;
}
# FIXME: record changes to implement undo/redo ?
sub notify_change {
my $self = shift ;
$self->{needs_save} = 1;
}
sub write_back {
my $self = shift ;
my %args = scalar @_ > 1 ? @_
: scalar @_ == 1 ? (config_dir => $_[0])
: () ;
my $force_backend = delete $args{backend} || $self->{backend} ;
my $force_write = delete $args{force} || 0;
foreach (keys %args) {
if (/^(root|config_dir)$/) {
$args{$_} ||= '' ;
$args{$_} .= '/' if $args{$_} and $args{$_} !~ m(/$) ;
}
elsif (not /^config_file$/) {
croak "write_back: wrong parameters $_" ;
}
}
croak "write_back: no subs registered in instance $self->{name}. cannot save data\n"
unless @{$self->{_write_back}} ;
foreach my $path (@{$self->{_write_back}}) {
$logger->info("write_back called on node $path");
my $node = $self->config_root->grab(step => $path, type => 'node');
$node->write_back(
%args,
config_file => $self->{config_file} ,
backend => $force_backend,
force => $force_write,
);
}
$self->needs_save(0) ;
}
sub apply_fixes {
my $self = shift ;
# define leaf call back
my $fix_leaf = sub {
my ($scanner, $data_ref, $node,$element_name,$index, $leaf_object) = @_ ;
$leaf_object->apply_fixes ;
} ;
my $fix_hash = sub {
my ( $scanner, $data_r, $node, $element, @keys ) = @_;
return unless @keys;
# leaves must be fixed before the hash, hence the
# calls to scan_hash before apply_fixes
map {$scanner->scan_hash($data_r,$node,$element,$_)} @keys ;
$node->fetch_element($element)->apply_fixes ;
} ;
my $fix_list = sub {
my ( $scanner, $data_r, $node, $element, @keys ) = @_;
return unless @keys;
map {$scanner->scan_list($data_r,$node,$element,$_)} @keys ;
$node->fetch_element($element)->apply_fixes ;
} ;
my $scan = Config::Model::ObjTreeScanner-> new (
hash_element_cb => $fix_hash ,
list_element_cb => $fix_list ,
leaf_cb => $fix_leaf ,
check => 'no',
) ;
$scan->scan_node(undef, $self->config_root) ;
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=head1 NAME
Config::Model::Instance - Instance of configuration tree
=head1 VERSION
version 2.005
=head1 SYNOPSIS
use Config::Model;
use Log::Log4perl qw(:easy);
use File::Path ;
Log::Log4perl->easy_init($WARN);
# setup a dummy popcon conf file
my $wr_dir = '/tmp/etc/';
my $conf_file = "$wr_dir/popularity-contest.conf" ;
unless (-d $wr_dir) {
mkpath($wr_dir, { mode => 0755 })
|| die "can't mkpath $wr_dir: $!";
}
open(my $conf,"> $conf_file" ) || die "can't open $conf_file: $!";
$conf->print( qq!MY_HOSTID="aaaaaaaaaaaaaaaaaaaa"\n!,
qq!PARTICIPATE="yes"\n!,
qq!USEHTTP="yes" # always http\n!,
qq!DAY="6"\n!);
$conf->close ;
my $model = Config::Model->new;
# PopCon model is provided. Create a new Config::Model::Instance object
my $inst = $model->instance (root_class_name => 'PopCon',
root_dir => '/tmp',
);
my $root = $inst -> config_root ;
print $root->describe;
=head1 DESCRIPTION
This module provides an object that holds a configuration tree.
=head1 CONSTRUCTOR
An instance object is created by calling
L<instance method|Config::Model/"Configuration instance"> on
an existing model:
my $inst = $model->instance (root_class_name => 'SomeRootClass',
instance_name => 'test1');
The directory (or directories) holding configuration files is
specified within the configuration model. For test purpose you can
change the "root" directory with C<root_dir> parameter:
=over
=item root_dir
Pseudo root directory where to read I<and> write configuration files
=item backend
Specify which backend to use. See L</write_back ( ... )> for details
=item skip_read
When set, configuration files will not be read when creating
configuration tree.
=item check
'yes', 'skip' or 'no'
=back
Note that the root directory specified within the configuration model
will be overridden by C<root_dir> parameter.
If you need to load configuration data that are not correct, you can
use C<< force_load => 1 >>. Then, wrong data will be discarded (equivalent to
C<check => 'no'> ).
=head1 METHODS
=head2 name()
Returns the instance name.
=head2 config_root()
Returns the root object of the configuration tree.
=head2 read_check()
Returns how to check read files.
=head2 reset_config
Destroy current configuration tree (with data) and returns a new tree with
data (and annotations) loaded from disk.
=head2 config_model()
Returns the model (L<Config::Model> object) of the configuration tree.
=head2 annotation_saver()
Returns the object loading and saving annotations. See
L<Config::Model::Annotation> for details.
=head2 preset_start ()
All values stored in preset mode are shown to the user as default
values. This feature is useful to enter configuration data entered by
an automatic process (like hardware scan)
=head2 preset_stop ()
Stop preset mode
=head2 preset ()
Get preset mode
=head2 preset_clear()
Clear all preset values stored.
=head2 layered_start ()
All values stored in layered mode are shown to the user as default
values. This feature is useful to enter configuration data entered by
an automatic process (like hardware scan)
=head2 layered_stop ()
Stop layered mode
=head2 layered ()
Get layered mode
=head2 layered_clear()
Clear all layered values stored.
=head2 get_data_mode
Returns 'normal' or 'preset' or 'layered'. Does not take into account initial_load.
=head2 initial_load_stop ()
Stop initial_load mode. Instance is built with initial_load as 1. Read backend
will clear this value once the first read is done.
=head2 initial_load ()
Get initial_load mode
=head2 data( kind, [data] )
The data method provide a way to store some arbitrary data in the
instance object.
=head2 load( "..." )
Load configuration tree with configuration data. See
L<Config::Model::Loader> for more details
=head2 searcher ( )
Returns an object dedicated to search an element in the configuration
model (respecting privilege level).
This method returns a L<Config::Model::Searcher> object. See
L<Config::Model::Searcher> for details on how to handle a search.
=head2 wizard_helper ( ... )
Deprecated. Call L</iterator> instead.
=head2 iterator
This method returns a L<Config::Model::Iterator> object. See
L<Config::Model::Iterator> for details.
Arguments are explained in L<Config::Model::Iterator>
L<constructor arguments|Config::Model::Iterator/"Creating an iterator">.
=head2
=head1 Auto read and write feature
Usually, a program based on config model must first create the
configuration model, then load all configuration data.
This feature enables you to declare with the model a way to load
configuration data (and to write it back). See
L<Config::Model::AutoRead> for details.
=head2 read_root_dir()
Returns root directory where configuration data is read from.
=head2 backend()
Get the preferred backend method for this instance (as passed to the
constructor).
=head2 write_root_dir()
Returns root directory where configuration data is written to.
=head2 register_write_back ( node_location )
Register a node path that will be called back with
C<write_back> method.
=head2 notify_change
Notify that some data has changed in the tree.
=head2 write_back ( ... )
Try to run all subroutines registered with C<register_write_back> to
write the configuration information until one succeeds (returns
true). (See L<Config::Model::AutoRead> for details).
You can specify here a pseudo root directory or another config
directory to write configuration data back with C<root> and
C<config_dir> parameters. This will override the model specifications.
You can force to use a backend by specifying C<< backend => xxx >>.
For instance, C<< backend => 'augeas' >> or C<< backend => 'custom' >>.
You can force to use all backend to write the files by specifying
C<< backend => 'all' >>.
C<write_back> will croak if no write call-back are known.
=head2 apply_fixes
Scan the tree and apply fixes that are attached to warning specifications.
See C<warn_if_match> or C<warn_unless_match> in L<Config::Model::Value/>.
=head1 AUTHOR
Dominique Dumont, (ddumont at cpan dot org)
=head1 SEE ALSO
L<Config::Model>,
L<Config::Model::Node>,
L<Config::Model::Loader>,
L<Config::Model::Searcher>,
L<Config::Model::Value>,
=cut
|