2.4. vacumm.config
– Configuration of modules¶
2.5. Overview¶
Functions: | ||||
---|---|---|---|---|
Classes: |
2.6. Content¶
Utilities to manage VACUMM basic configuration of modules
-
class
URLLibProgressBar
(width=80)[source]¶ Progress bar compatible with
urllib.urlretrieve()
It looks like:
[=======> 22% ]
Example: >>> import urllib >>> mybar = URLProgress(width=50) >>> urllib.urlretrieve("http://web.net/file", "myfile", reporthook=mybar)
Inspired from: http://code.activestate.com/recipes/168639-progress-bar-class/
-
check_data_file
(section, option, parent_section=None, parent_option=None, quiet=None, suffix=None, avail=False, check_license=True)[source]¶ Check the existence of a file whose path is stored in a configuration file
Two cases are treated:
- Path value is accessible from the vacumm configuration (
get_config()
), usingsection
(module name) andoption
. - Path value is accessible from a secondary configuration file using
parent_section
(module name) andparent_option
, and the path value of this config file is accessible from the vacumm configuration usingsection
andname
.
If the file is not found, it may download it using an url whose value is accessible at the same place as the path and with the same option name + ‘_url’.
Tasks: - If
suffix
is a list, call itself in a loop withsuffix
set to each element of the list. - Get the
path
,url
andlicense
of the data file. - If
avail is True
, only check the existence and return the status. - If the file is not found (and
quiet is False
), ask the user for it. If the path specified is empty, simply go further. If is not empty, update the configuration and return it, else raise an error. - If a license info is present, tell the user that he must have the authorization to download it. If the user does not agree, raise an error.
- Ask the user where to download the file using
get_dl_dir()
. - Download the file using
download_file()
. - Update the configuration if needed.
- Return the path.
Params: - section: Section where to find the path to data.
- option: Option to read the path.
- parent_section, optional: Section of the vacumm configuration where to find the path to the secondary configuration file that has the path stored within.
- parent_option, optional: Option of the vacumm configuration to read the path to the secondary configuration.
- quiet, optional: Don’t ask question about where to download the file, don’t display any info, don’t ask for authorization.
- suffix, optional: A suffix or a list of suffixes to append to the path and url before using it.
- check_license, optional: If a license info is found, show ot and ask the user if he has autorization for downloading it.
- avail, optional: Check availability only (see below).
Return: A single path or a list of paths.
None
or a list of them if nothing found.If
avail=True
:0
: Data file not on disk and not downloadable (however, user can specify the path manually).1
: File is not on disk, but downloadable.2
: File is on disk.
Examples: >>> check_data_file('vacumm.bathy.shorelines', 'shapefile_histolitt', suffix=['.shp', '.dbf']) >>> check_data_file('etopo2', 'file', parent_section='vacumm.bathy.bathy', ... parent_option='cfgfile_gridded')
- Path value is accessible from the vacumm configuration (
-
data_sample
(data_file, mode='all')[source]¶ Transform the relative path of a sample file to an absolute path
This can be either in the VACUMM data directory (
get_data_dir()
), or in the UVCDAT data directory.Params: data_file: File basename.
mode, optional:
"all"
orNone
: Look first into VACUMM dir then and into UVCDAT dir if not found. If finally not found, return the VACUMM sample path."vacumm"
: Look only into the VACUMM data dir only."uvcdat"
: Look only into the UVCDAT data dir only.
-
download_file
(url, locfile, quiet=False)[source]¶ Download a file with a progress bar using
urllib.urlretrieve()
-
edit_file
(fname, editor=None, verbose=True)[source]¶ Edit a file
Params: - fname: File name.
- editor, optional: Editor to use (default to
VISUAL
orEDITOR
.
Source: Mercurial code
-
edit_user_conf_file
(fname='vacumm.cfg', editor=None, verbose=True)[source]¶ Edit the user configuration file
Params: - fname, optional: Relative file name, which defaults to
vacumm.cfg
. - editor, optional: Editor to use (default to
VISUAL
orEDITOR
.
Examples:
>>> edit_user_conf_file() >>> edit_user_conf_file('my_other_config.cfg', editor='gedit')
See also: edit_file()
andget_user_conf_file()
- fname, optional: Relative file name, which defaults to
-
get_com_conf_dir
()[source]¶ Get directory of common alternate configuration files
These files are not included in the distribution. They have a similar role to user config files but they are accessible to all users. This directory can be at two different places, depending on if the library is an installed version or developers version.
- If installed : in
vacumm-config
subdirectory in the installed package directory (seeget_lib_dir()
). - Else in the
config
subdirectory of the main distribution tree (seeget_dist_dir()
).
- If installed : in
-
get_com_conf_file
()[source]¶ Get common user configuration file
Shortcut for:
os.path.join(get_com_conf_dir(), 'vacumm.cfg')
See also: get_com_conf_dir()
,get_user_conf_file()
-
get_config
(section=None, user=True, asstring=False, cfg=None, parent_section=None, parent_option=None, split=False)[source]¶ Get a
ConfigParser.SafeConfigParser
instance of current configuration or as a stringParams: - section, optional: A module or its name, or
"__all__"
to get a full config (config files in module drectories are also scanned). - user, optional: Also scan available user configuration files.
- asstring, optional: Get it as string.
- parent_section/option, optional: Section (module name) and option of the main vacumm configuration that give the path to the target configuration file. If not provided, it uses cfg the main vacumm configuration.
See also: - section, optional: A module or its name, or
-
get_config_files
(section=None, check=False, user=1)[source]¶ Get the list of configuration files that can be loaded
It lists all configuration files that should be loaded to search for a configuration value. It first list default configuration files, then add user specific files.
Params: - section, optional: Target section of the configuration files.
It is generally a module name.
In this case, user and default configuration files
located in the where the module is are inserted
into the list.
It can also be a list of module name.
Eventually, if the special value
"__all__"
is passed, it searches for all configuration files namedvacumm.cfg
in the whole library tree. - user, optional: If
False
or0
, load only default config. If1
orTrue
, append user configuration files to the list. If2
, list only user config files.
Example: Suppose you are in directory
/home/me/myproject
,get_config_files()
is called from modulevacumm.bathy.shorelines
, and your use VACUMM as a developper, i.e. the library is not installed but used directly in the installation tree whose path is/home/me/python/vacumm
:>>> get_config_files('vacumm.bathy.shorelines')
The following list is returned :
- /home/me/python/vacumm/lib/python/vacumm/bathy/shorelines/vacumm.cfg (defaults)
- /home/me/python/vacumm/lib/python/vacumm/vacumm-config/vacumm.cfg (defaults)
- /home/me/.config/vacumm/vacumm.cfg
Content of last files overides content of first files.
Other usages:
>>> get_config_files('__all__', user=False) >>> get_config_files(['toto.tutu', 'tutu.tata'])
- section, optional: Target section of the configuration files.
It is generally a module name.
In this case, user and default configuration files
located in the where the module is are inserted
into the list.
It can also be a list of module name.
Eventually, if the special value
-
get_config_sections
(cfg=None, parent_section=None, parent_option=None, getcfg=False)[source]¶ Get sections names
Params: - parent_section/option, optional: Section (module name) and option of the main vacumm configuration that give the path to the target configuration file. If not provided, it uses cfg the main vacumm configuration.
- getcfg, optional: Also return the configurations object.
-
get_config_value
(section, option, regexp=False, ispath=False, user=True, expand=True, vars=None, raw=False, umode='auto', cfg=None, getcfg=False, parent_section=None, parent_option=None)[source]¶ Get the a config value in available VACUMM configuration files
The list of possible configuration files is provided by
get_config_files()
.Configuration values can use expanded variables in two ways:
Internal option name are automatically expanded to their value if found in the form
%(option_name)s
. In addition to internal options, you can provide your own (var_name,value) pairs by passing a dictionary to thevars
keyword. Some external options pointing to directory names are provided automatically (but can be overidden):lib_dir
(seeget_lib_dir()
)data_dir
(seeget_data_dir()
)dist_dir
(seeget_dist_dir()
)tut_dir
(seeget_tut_dir()
)user_conf_dir
orconf_dir
(seeget_user_conf_dir()
)scripts_dir
(seeget_scripts_dir()
)mod_dir
(directory of the module whensection
is a module name)
User home
~
and environement variable (in the form${HOME}
are expanded if keywordexpand
is set toTrue
.
Params: section: A section in the config, typically a full module name (see
get_config_files()
)option: Option to read in the section.
regexp, optional:
option
is interpreted a regular expression. IfTrue
, it returns a dictionary of (option,value) items found.ispath, optional: if
True
, value is converted usingnumpy.distutils.misc_util.allpath()
.vars, optional: Dictionary used to expand variables in the form
%(var_name)s
.user, optional: Also scan user files. If
raw
is True, value withconf_dir
expanded usinguser_conf_dir
, and if differents values are found, they are treated depending onmode
.umode, optional: If
user
is True, and two possible values are found (one withconf_dir
, one withuser_conf_dir
), they are treated depending onumode
:"merge"
: Value found usinguser_conf_dir
has priority over value found usingconf_dir
."all"
: A two-element list is returned if elements are different.- Else, if value seems to be a config file (ending with “.ini”
or “.cfg”),
mode="all"
elsemode="merge"
.
cfg, optional: External
ConfigParser.ConfigParser
instance or an external config file. If not provided, configuration is given byget_config()
(internal configuration).
Returns: If
regexp
is `` False``, return the value or None if not found, else return a dictionary.Example: >>> path = get_config_value('vacumm.bathy.shorelines', 'shapefile_histolitt') >>> paths = get_config_value('vacumm.bathy.shorelines', 'shapefile_.*', regexp=True) >>> paths = get_config_value('my.module', vars=dict(mydir='/home/me/dir'))
-
get_data_dir
(raiseerr=False)[source]¶ Get the data directory absolute path
This directory contains data samples and other needed files. It can be at two different places, depending on if the library is an installed version or a developers version.
- If installed : in
vacumm-data
subdirectory in the installed package directory (seeget_lib_dir()
). - Else in the
data
subdirectory of the main distribution tree (seeget_dist_dir()
).
Warning
It raises an
VACUMMError
error if not found and ifraiseerr
is True, else return ‘’.- If installed : in
-
get_default_config
(section='__all__')[source]¶ Shortcut to:
get_config(section='__all__', user=False)
-
get_dir_dict
(modname=None)[source]¶ Get the following directory names as a dictionary
lib_dir
(seeget_lib_dir()
)data_dir
(seeget_data_dir()
)dist_dir
(seeget_dist_dir()
)tut_dir
(seeget_tut_dir()
)com_conf_dir
(seeget_com_conf_dir()
)user_conf_dir
(seeget_user_conf_dir()
)scripts_dir
(seeget_scripts_dir()
)mod_dir
(directory ofmodname
)
Params: - modname, optional: Module name.
-
get_dist_dir
()[source]¶ Upper directory of VACUMM distribution tree or
None
for an installed package
-
get_home_conf_dir
()[source]¶ Get the directory which contains the user config dir
It defaults to
~/.config
and may be set with theXDG_CONFIG_HOME
environmement variable.
-
get_scripts_dir
(subdir=None, raiseerr=False)[source]¶ Get the scripts directory absolute path
This directory contains examples of script. It can be at two different places, depending on if the library is an installed version or a developers version.
- If installed : in
vacumm-scripts
subdirectory in the installed package directory (seeget_lib_dir()
). - Else in the
scripts
subdirectory of the main distribution tree (seeget_dist_dir()
).
Warning
It raises an
VACUMMError
error if not found and ifraiseerr
is True.- If installed : in
-
get_tut_dir
(raiseerr=False)[source]¶ Get the tutorials directory absolute path
This directory contains all tutorials used by the documentation. It can be at two different places, depending on if the library is an installed version or developers version.
- If installed : in
vacumm-scripts/tutorials
subdirectory in the installed package directory (seeget_lib_dir()
). - Else in the
scripts/tutorials
subdirectory of the main distribution tree (seeget_dist_dir()
).
Warning
It raises an
VACUMMError
error if not found and ifraiseerr
is True.- If installed : in
-
get_user_conf_dir
()[source]¶ Get directory that contains user configuration files
This directory contains main and secondary configuration files. These files are usually referenced in the general configuration file. On linux, it should be
~/.config/vacumm
. It may be set with theVACUMM_CONFIG_DIR
.
-
get_user_conf_file
(fname=None)[source]¶ Get main user configuration file
Params: - fname, optional: Relative or absolute file name.
It may be set with the
VACUMM_CONFIG_FILE
. The config directory is prepended if the file name is relative.
Shortcut for:
os.path.join(get_user_conf_dir(), fname)
See also: get_user_conf_dir()
,edit_user_conf_file()
- fname, optional: Relative or absolute file name.
It may be set with the
-
load_configs
(cfgfiles)[source]¶ Read configuration files in merged and separate configuations
Params: cfgfiles: Configuration files.
- If a file, simply return the associated
SafeConfigParser
instance. - If a list of files, does the same merging all the files.
- If a tuple, create a configuration for each of its elements.
- If a file, simply return the associated
Example: >>> cfg = load_configs('myfile.cfg') >>> cfg = load_configs(['myfile1.cfg', 'myfile2.cfg']) >>> cfg1, cfg2 = load_configs(('vacumm.cfg', ['myfile1.cfg', 'myfile2.cfg']))
-
merge_configs
(cfgs, forprint=True, raw=True)[source]¶ Merge configurations
Options of the last config overrides options of the other ones, except for config file names (values then end with .ini or .cfg) that are store as a tuple.
Warning
This makes output config incompatible with
getint()
, etc.
-
print_config
(section='__all__', system=True, direc=True, config=True, packages=True, extended=False, user=True, headers=True)[source]¶ Print current configuration
Params: - system: print the base of the config.
- direc: print key directories of vacumm.
- config: print the vacumm config.
- packages: print the version of some important packages.
- extended: force printing everything.
This is equivalent to (if
extended=False
):>>> print get_config(section, user, asstring=True)
-
set_config_value
(section, option, value=None, quiet=False, cfgfile=None)[source]¶ Create or update user configuration file
The directory of this file is given by
get_user_conf_dir()
and its name isvacumm.cfg
. Therefore it should be:~/.config/vacumm/vacumm.cfg
.Params: - section: A module or its name.
- option: Option name.
- value: Value of the option. If
None
, option is removed. - cfgfile, optional: Configuration file. If not provided,
internal user configuration file is used
(
vacumm.cfg
in user config directory – given byget_user_conf_dir()
).
Example: >>> set_config_value('vacumm.bathy.bathy', 'cfgfile_gridded', '%(mod_dir)s/bathy.gridded.cfg') # set >>> import vacumm.bathy.bathy as B >>> set_config_value(B, 'cfgfile_gridded') # remove
Output: Path of the configuration file
-
write_default_config
(cfgfile)[source]¶ Write the defaults config (see
get_default_config()
) to a file