Description | Perl tools for X-ray Absorption Spectroscopy |
Demeter::Config - Demeter's configuration system
This documentation refers to Demeter version 0.9.26.
This subclass of Demeter provides a general way of storing and manipulating parameters of all sorts, including configuration parameters for controlling the behavior of Demeter's various subsystems, parameters using by Demeter-base applications, and parameters that get passed to the templating subsystem. The $C
special templating variable accesses this object.
Demeter uses a two-tiered configuration system. The system-wide tier reads from a configuration file demeter.config which lives in the lib/ directory beneath the Demeter.pm module. This configuration file contains complete information about each configuration parameter, including description text and various attributes. The second tier is the user's initialization file. This uses the standard INI format and the Demeter::IniReader module. The user's file lives in $ENV{HOME} on unix and Mac and in $ENV{USERPROFILE} on Windows.
Defining new parameters to be handled by Config is as simple as entering them into the INI file using normal INI syntax. Those user-created parameters will not possess descriptions or other attributes, however.
The normal idiom for accessing method of the Config class is to chain method calls starting with any other Demeter object. Although you can certainly store a reference to the Config object as a scalar, it usually is not necessary to do so.
The co
method, inhereted by all other objects from the Demeter base class, returns a reference to the Config object. All of the examples below use the chain idiom and self
can be any kind of object.
default
Return the default value for a parameter.
print "Default fft kmin is ", $object->co->default("fft", "kmin"), $/;
set_default
This method is called repeatedly by read_ini
to move the information from the user's ini file into the Config object.
$object -> co -> set_default("bkg", "rbkg", 1.2);
describe
Return a text description of a parameter.
print $object -> co -> describe("bkg", "kw");
== prints ==>
bkg -> kw
"The default value for the k-weighting used to fit the background spline."
type : positive integer
default : 2
demeter : 2
minint : 0
maxint : 3
There is a third argument that controls the width of the description text, the default being 90 (as set by the operations-->config_text_width
parameter).
print $object -> co -> describe("bkg", "kw", 45);
== prints ==>
bkg -> kw
"The default value for the k-weighting
used to fit the background spline."
type : positive integer
default : 2
demeter : 2
minint : 0
maxint : 3
groups
Return a list of known configuration groups.
@groups = $object -> co -> groups;
parameters
Return a list of parameters associated with a specified configuration group.
@params = $object -> co -> parameters('bkg');
read_ini
Read default values from a user's ini file, overwriting the values read from the system-wide configuration file.
$object -> co -> read_ini($filename);
write_ini
Write an ini file for a user.
$object -> co -> write_ini($filename);
If the filename is not given, the user's ini file will be written.
is_configured
This method returns true if a Config object has been created and initialized by the read_config
method.
read_config
This method reads the system-wide configuration file, demeter.config, from it location in the Demeter/lib/ folder just below the main Demeter.pm module. This loads the entire contents, including default values, parameter descriptions, and parameter attributes, into the Config object.
set_this_param
Use this method to update the default value for a parameter. For example, the read_ini
method calls this repeatedly. Note that the original value of each parameter from the demeter.config file always stays in the object as the "demeter" parameter attribute.
$object->set_this_param($group, $param, %hash);
where %hash contains the various attributes decribed below.
All parameters have these attributes. Each attribute has an associated convenience method of the same name.
This is one of string, regex, real, "positive integer", list, boolean, color, or font.
print $object -> co -> Type("fft", "kwindow")
==prints==>
list
This is the value associated with the parameter. This is overridden by an imported INI file or by the set_default
method.
print $object -> co -> default("fft", "kwindow")
==prints==>
hanning
This overrides the default, but only on Windows computers.
Strings that take a fully resolved path to an executable are treated specially. If that executable resides beneath the perl installation location, then the installation location should be denoted as __PERL_BASE__
. For example, Strawberry perl is typically instaled into C:\strawberry
. Rather than denoting the location of the Feff executable as C:\strawberry\c\bin\feff.exe
, it should be specified as __PERL_BASE__\c\bin\feff.exe
. The __PERL_BASE__
tag will be replaced by the correct installation location. Then, if Strawberry is installed into some other location, the Feff executable will be found at runtime.
This is the default value from the configuration file shipped with demeter. This is untouched even when a default is overridden by an imported INI file.
print $object -> co -> demeter("fft", "kwindow")
==prints==>
hanning
This is the previous value of the parameter from before the last time it was changed using the set_default
method. If the parameter has not been changed since it was initialized, this method returns 0. The primary use of this method is to aid in post-processing a change in parameter value in a GUI or other application.
print $object -> co -> demeter("fft", "kwindow")
==prints==>
welch
This is a text string explaining the purpose of the parameter.
print $object -> co -> description("fft", "kwindow")
==prints==>
The default window type to use for the forward Fourier transform.
This can also return the description text of a parameter group.
print $object -> co -> description("fft")
==prints==>
These parameters determine how forward Fourier transforms are done by Demeter.
Some attributes are specific to a parameter type. Each attribute has an associated convenience method of the same name.
This specifies the units of the parameter, if appropriate.
print $object -> co -> units("bkg", "pre1")
==prints==>
eV (relative to e0 or to the beginning of the data)
This is true or false depending on whether changing the parameter takes effect immediately or if a restart of the application is required.
These are the values associated with the true and false states of a boolean parameter. If unspecified in the configuration file, they default to 1 and 0.
print $object -> co -> onvalue("bkg", "flatten")
==prints==>
1
These are the minimum and maximum allowed values of a positive integer parameter. If unspecified in the configuration file, they default to 0 and 1e9.
print $object -> co -> maxint("bkg", "kw")
==prints==>
3
This are the possible options for a list parameter. They are stored as a space-separated string. This string will have to be split on the spaces to be used as a list.
print $object -> co -> options("fft", "kwindow")
==prints==>
hanning kaiser-bessel welch parzen sine
The set
method creates a user-defined parameter and stores it in the Config object for later use.
$merged->set(ndata=>$ndata);
These user-defined parameter are the accessed via get
(rather than default
)
print "Number of sets in merge = ", $data->co->get("ndata");
The reason to use the Config object to store parameters is that they are easily used by the templating system that Demeter uses to do its work. Also, the user-defined parameters will be serialized along with the Config object.
This system is used extensively in the merge
method from Demeter::Data::Process. See also the merge_*.tmpl files in the "process" group of templates.
You cannot have more than one Config object
One Config object is created when Demeter loads. It is forbidden to create a second one. Config paramaters are global to an instance of Demeter.
The Demeter configuration file ($config_file) does not exist
The demeter.config file should be installed in the lib/ subdirectory beneath the location of the Demeter.pm module. This diagnostic says that configuration file is absent.
The Demeter configuration file ($config_file) cannot be opened
The demeter.config file should be installed in the lib/ subdirectory beneath the location of the Demeter.pm module. This diagnostic says that configuration file is unreadable, perhaps because of a permissions setting.
$key is not a valid configuration parameter
This diagnostic says that you have attempted to retrieve an attribute for a parameter that does not exist.
The configuration files are broken into a simple hierarchy. Parameter groups tend to be contained in their own files which are included into the master configuration file config.demeter_conf.
The included configuration files are fairly structured. Beginning-of-line whitespace (2 spaces, no more, no less) is important in the parameter descriptions, as are the empty lines and the lines that begin with a dot. The empty lines denote separations between entries. The dots are used to build lists in the descriptions.
The parser for this file is fairly stupid, so if you make mistakes in the use of whitespace, bad things will happen. An "EMACS MAJOR MODE" is provided, the main purpose of which is to color text as an indication of correct formatting. Also, order matters. It is very important that "variable=" comes first, then "type=", then "default=", then the rest.
The contents of the configuration files are used to populate the Config object and to generate the files system-wide demeter.ini. It can also be used to populate the preferences dialog in a GUI. Here is a list of suggested widgets to use with each configuration type
variable types suggested widget
string TextCtrl
regex TextCtrl
real TextCtrl -- validates to accept only numbers
positive integer SpinCtrl -- restricted to be >= 0
list Choice or some other multiple selection widget
boolean Checkbutton
keypress TextCtrl -- rigged to display one character at a time
color ColorPicker -- launches color browser
font FontPicker -- does nothing at this time
A major mode for emacs is provided with the Demeter package. It provides some functionality for editing Demeter configuration files. The most significant functionality is syntax colorization that enforces overly strict rules for the layout of the file. Using this major mode helps avoid introducing formatting errors into a configuration file which has an admittedly fragile syntax.
See config-mode.el in the tools/ subdirectory of the Demeter distribution. To use config-mode, simply place the file somewhere in the Emacs load path and put this line in your .emacs file or some other emacs initialization file.
(autoload 'config-mode "config-mode")
The mode is tiny. You can byte-compile or not -- as you wish.
See the Build.PL file for a list of dependencies.
This object needs to be a singleton, but it also needs to extend Demeter. How is that done in Moose? I have no idea....
Nothing prevents overwriting Config entries -- this is a potential problem for user-defined parameetrs.
There is no introspection method for returning all (sets of) user defined parameters.
The configuration file syntax is somewhat fragile.
read_config_file
only handles files relative to the master config file. If it could understand fully resolved filenames, it could, for instance, look for additional configuration files in userspace or application space. That way apps and one-off tools could use the configuration system.
When (how often) should ini files be written out?
Please report problems to the Ifeffit Mailing List (http://cars9.uchicago.edu/mailman/listinfo/ifeffit/)
Patches are welcome.
Bruce Ravel, http://bruceravel.github.io/home
http://bruceravel.github.io/demeter/
Copyright (c) 2006-2018 Bruce Ravel (http://bruceravel.github.io/home). All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlgpl.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.