NAME
Config::Model::Backend::Any - Virtual class for other backends
VERSION
version 2.102
SYNOPSIS
package Config::Model::Backend::Foo ;
use Mouse ;
extends 'Config::Model::Backend::Any';
# optional
sub suffix {
return '.conf';
}
# mandatory
sub read {
my $self = shift ;
my %args = @_ ;
# args are:
# root => './my_test', # fake root directory, used for tests
# config_dir => /etc/foo', # absolute path
# file => 'foo.conf', # file name
# file_path => './my_test/etc/foo/foo.conf'
# io_handle => $io # IO::File object opened for read
# check => yes|no|skip
return 0 unless defined $args{io_handle} ; # or die, your choice
# read the file line by line
# we assume the file contain lines like 'key=value'
foreach ($args{io_handle}->getlines) {
chomp ; # remove trailing \n
s/#.*// ; # remove any comment
next unless /\S/; # skip blank line
# $data is 'foo=bar' which is compatible with load
$self->node->load(steps => $_, check => $args{check} ) ;
}
return 1 ;
}
# mandatory
sub write {
my $self = shift ;
my %args = @_ ;
# args are:
# root => './my_test', # fake root directory, used for tests
# config_dir => /etc/foo', # absolute path
# file => 'foo.conf', # file name
# file_path => './my_test/etc/foo/foo.conf'
# io_handle => $io # IO::File object opened for write
# check => yes|no|skip
my $ioh = $args{io_handle} ;
# read the content of the configuration tree
foreach my $elt ($self->node->children) {
# read the value from element $elt
my $v = $self->node->grab_value($elt) ;
# write value in file
$ioh->print(qq!$elt="$v"\n!) if defined $v ;
}
return 1;
}
DESCRIPTION
Some application have configuration files with a syntax which is not supported by existing Config::Model::Backend::*
classes.
In this case a new backend must be written. Config::Model::Backend::Any
was created to facilitate this task.
The new backend class must use Mouse and must extends (inherit) Config::Model::Backend::Any
.
How to write your own backend
Declare the new backend in a node of the model
As explained in "Backend specification" in Config::Model::BackendMgr, the new backend must be declared as an attribute of a Config::Model::Node specification.
Let's say your new backend is Config::Model::Backend::Foo
. This new backend can be specified with:
read_config => [ {
backend => 'Foo' , # can also be 'foo'
config_dir => '/etc/cfg_dir'
file => 'foo.conf', # optional
}]
(The backend class name is constructed with ucfirst($backend_name)
)
read_config
can also have custom parameters that are passed verbatim to Config::Model::Backend::Foo
methods:
read_config => [ {
backend => 'Foo' , # can also be 'foo'
config_dir => '/etc/cfg_dir'
file => 'foo.conf', # optional
my_param => 'my_value',
} ]
Config::Model::Backend::Foo
class must inherit (extend) Config::Model::Backend::Any and is expected to provide the following methods:
- read
-
read()
is called with the following parameters:%custom_parameters, # e.g. my_param => 'my_value' in the example above object => $obj, # Config::Model::Node object root => $root_dir, # fake root directory, used for tests backend => $backend, # backend name config_dir => $read_dir, # path below root file => 'foo.conf', # file name file_path => $full_name, # full file name (root+path+file) io_handle => $io_file # IO::File object opened for read check => [yes|no|skip]
The IO::File object is undef if the file cannot be read.
This method must return 1 if the read was successful, 0 otherwise.
Following the
my_param
example above,%custom_parameters
contains( 'my_param' , 'my_value' )
, soread()
is called withroot
,config_dir
,file_path
,io_handle
andmy_param => 'my_value'
. - write
-
write()
is called with the following parameters:%$custom_parameters, # e.g. my_param => 'my_value' in the example above object => $obj, # Config::Model::Node object root => $root_dir, # fake root directory, used for tests auto_create => $auto_create, # boolean specified in backend declaration auto_delete => $auto_delete, # boolean specified in backend declaration backend => $backend, # backend name config_dir => $write_dir, # override from instance file => 'foo.conf', # file name file_path => $full_name, # full file name (root+path+file) io_handle => $fh, # IO::File object write => 1, # always check => [ yes|no|skip] , backup => [ undef || '' || suffix ] # backup strategy required by user
The IO::File object is undef if the file cannot be written to.
This method must return 1 if the write was successful, 0 otherwise
If
io_handle
is defined, the backup has already been done before opening the config file. Ifio_handle
is not defined, there's not enough information in the model to read the configuration file and create the backup. Yourwrite()
method will have to do the backup requested by user.When both
config_dir
andfile
are specified, the backend manager opens the configuration file for write (and thus clobbers it) before calling thewrite
call-back with the file handle withio_handle
parameter.write
should use this handle to write data in the target configuration file.If this behavior causes problem, the solution is to override
skip_open
method in your backend to return1
.
How to test your new backend
Using Config::Model::Tester, you can test your model with your backend following the instructions given in Config::Model::Tester.
You can also test your backend with a minimal model (and Config::Model::Tester). In this case, you need to specify a small model to test in a *-test-conf.pl
file. See the IniFile backend test for an example and its examples files.
CONSTRUCTOR
new ( node => $node_obj, name => backend_name )
The constructor should be used only by Config::Model::Node.
Methods to override
annotation
Whether the backend supports reading and writing annotation (a.k.a comments). Default is 0. Override this method to return 1 if your backend supports annotations.
suffix
Suffix of the configuration file. This method returns undef
by default.
read
Read the configuration file. This method must be overridden.
write
Write the configuration file. This method must be overridden.
Methods
node
Return the node (a Config::Model::Node) holding this backend.
instance
Return the instance (a Config::Model::Instance) holding this configuration.
show_message( string )
Show a message to STDOUT (unless overridden). Delegated to "show_message( string )" in Config::Model::Instance.
read_global_comments
Parameters:
array ref of string containing the lines to be parsed
A string to specify how a comment is started. Each character is recognized as a comment starter (e.g '
#;
' allow a comment to begin with '#
' or ';
')
Read the global comments (i.e. the first block of comments until the first blank or non comment line) and store them as root node annotation.
Example:
$self->read_global_comments( \@lines, ';');
$self->read_global_comments( \@lines, '#;');
associates_comments_with_data
Parameters:
array ref of string containing the lines to be parsed
A string to specify how a comment is started. Each character is recognized as a comment starter (e.g '
#;
' allow a comment to begin with '#
' or ';
')
This method extracts comments from the passed lines and associate them with actual data found in the file lines. Data is associated with comments preceding or on the same line as the data. Returns a list of [ data, comment ].
Example:
my @lines = (
'# Foo comments',
'foo= 1',
'Baz = 0 # Baz comments'
);
my @res = $self->associates_comments_with_data( \@lines, '#')
# @res is:
# ( [ 'foo= 1', 'Foo comments' ] , [ 'Baz = 0' , 'Baz comments' ] )
write_global_comments( io_handle , comment_char)
Write global comments from configuration root annotation into the io_handle (if defined). Returns the string written to the io_handle.
write_data_and_comments( io_handle , comment_char , data1, comment1, data2, comment2 ...)
Write data and comments in the io_handle
(if defined). Comments are written before the data. Returns the string written to the io_handle. If a data is undef, the comment is written on its own line.
AUTHOR
Dominique Dumont, (ddumont at cpan dot org)
SEE ALSO
Config::Model, Config::Model::BackendMgr, Config::Model::Node, Config::Model::Backend::Yaml,
AUTHOR
Dominique Dumont
COPYRIGHT AND LICENSE
This software is Copyright (c) 2005-2017 by Dominique Dumont.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999