Config::Model::Backend::Any - Virtual class for other backends
version 2.083
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; }
Some application have configuration files with a syntax which is not supported by existing Config::Model::Backend::* classes.
Config::Model::Backend::*
In this case a new backend must be written. Config::Model::Backend::Any was created to facilitate this task.
Config::Model::Backend::Any
The new backend class must use Mouse and must extends (inherit) Config::Model::Backend::Any.
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:
Config::Model::Backend::Foo
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))
ucfirst($backend_name)
read_config can also have custom parameters that will passed verbatim to Config::Model::Backend::Foo methods:
read_config
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() is called with the following parameters:
read()
%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' ) , so read() will also be called with root, config_dir, file_path, io_handle and my_param => 'my_value'.
my_param
%custom_parameters
( 'my_param' , 'my_value' )
root
config_dir
file_path
io_handle
my_param => 'my_value'
write() is called with the following parameters:
write()
%$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 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. If io_handle is not defined, there's not enough information in the model to read the configuration file and create the backup. Your write() method will have to do the backup requested by user.
When both config_dir and file are specified, the backend manager will write-open the configuration file (and thus clobber it) before calling the write call-back and pass the file handle with io_handle parameter. write should use this handle to write data in the target configuration file.
file
write
If this behavior causes problem, the solution is either to override skip_open method in your backend that returns 1.
skip_open
1
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.
*-test-conf.pl
The constructor should be used only by Config::Model::Node.
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 of the configuration file. This method returns undef by default.
undef
Read the configuration file. This method must be overridden.
Write the configuration file. This method must be overridden.
Return the node (a Config::Model::Node) holding this backend.
Return the instance (a Config::Model::Instance) holding this configuration.
Show a message to STDOUT (unless overridden). Delegated to "show_message( string )" in Config::Model::Instance.
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. The first parameter (lines) is an array ref containing file lines.
lines
This method will extract 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:
# Foo comments foo= 1 Baz = 0 # Baz comments
will return
( [ 'foo= 1', 'Foo comments' ] , [ 'Baz = 0' , 'Baz comments' ] )
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 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 will be written on its own line.
Dominique Dumont, (ddumont at cpan dot org)
Config::Model, Config::Model::BackendMgr, Config::Model::Node, Config::Model::Backend::Yaml,
Dominique Dumont
This software is Copyright (c) 2005-2016 by Dominique Dumont.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999
To install Config::Model, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Model
CPAN shell
perl -MCPAN -e shell install Config::Model
For more information on module installation, please visit the detailed CPAN module installation guide.