Scroll to navigation

PYTHON-LIBECONF(3) python-libeconf PYTHON-LIBECONF(3)

python-libeconf is a Python Library which offers Python bindings for libeconf.

libeconf is a highly flexible and configurable library to parse and manage key=value configuration files. It reads configuration file snippets from different directories and builds the final configuration file for the application from it.

CONTENTS

Usage

install

WARNING:

The recommended way to install this project is to get it via your distro package manager. If you install this project from pypi libeconf will not be automatically installed. To use this project please make sure you have libeconf installed on your system!


You can install this project from pypi with

pip install python-libeconf


and then import it into your python project with

import econf


For information about the functions provided by this library, have a look at the API section

usage example

>>> import econf
>>> file = econf.read_file("examples/etc/example/example.conf", "=", "#")
>>> econf.get_groups(file)
['Another Group', 'First Group', 'Group']
>>> econf.get_keys(file, "Group")
['Bla', 'Welcome[la]', 'Welcome']
>>> econf.get_int_value(file, "Group", "Bla")
12311
>>> econf.set_int_value(file, "Group", "Bla", 5)
>>> econf.get_int_value(file, "Group", "Bla")
5
>>> econf.write_file(file, "examples/etc/example", "example_new.conf")
>>>


API

Functions to interact with config files

Read a config file and write the key-value pairs into a keyfile object
  • file_name – absolute path of file to be parsed
  • delim – delimiter of a key/value e.g. ‘=’
  • comment – string that defines the start of a comment e.g. ‘#’

Key-Value storage object


Read a config file and write the key-value pairs into a keyfile object

A user defined function will be called in order e.g. to check the correct file permissions. If the function returns False the parsing will be aborted and an Exception will be raised

  • file_name – absolute path of file to be parsed
  • delim – delimiter of a key/value e.g. ‘=’
  • comment – string that defines the start of a comment e.g. ‘#’
  • callback – User defined function which will be called and returns a boolean
  • callback_data – argument to be give to the callback function

Key-Value storage object


Create a new empty keyfile
  • delim – delimiter of a key/value e.g. ‘=’
  • comment – string that defines the start of a comment e.g. ‘#’

created EconfFile object


Create a new empty keyfile with delimiter ‘=’ and comment ‘#’
created EconfFile object


Merge the content of 2 keyfile objects
  • usr_file – first EconfFile object
  • etc_file – second EconfFile object

merged EconfFile object


Evaluating key/values of a given configuration by reading and merging all needed/available files from different directories.

This call fulfills all requirements, defined by the Linux Userspace API (UAPI) Group chapter "Configuration Files Specification".

  • project – name of the project used as subdirectory
  • usr_conf_dir – absolute path of the first directory to be searched
  • config_name – basename of the configuration file
  • config_suffix – suffix of the configuration file
  • delim – delimiter of a key/value e.g. ‘=’
  • comment – string that defines the start of a comment e.g. ‘#’

merged EconfFile object


Evaluating key/values of a given configuration by reading and merging all needed/available files from different directories.

For every file a user defined function will be called in order e.g. to check the correct file permissions. If the function returns False the parsing will be aborted and an Exception will be raised

This call fulfills all requirements, defined by the Linux Userspace API (UAPI) Group chapter "Configuration Files Specification".

  • project – name of the project used as subdirectory
  • usr_conf_dir – absolute path of the first directory to be searched
  • config_name – basename of the configuration file
  • config_suffix – suffix of the configuration file
  • delim – delimiter of a key/value e.g. ‘=’
  • comment – string that defines the start of a comment e.g. ‘#’
  • callback – User defined function which will be called for each file and returns a boolean
  • callback_data – argument to be give to the callback function

merged EconfFile object


Get the comment tag of the specified EconfFile
ef – Key-Value storage object
The comment tag of the EconfFile


Set the comment tag of the specified EconfFile
  • ef – Key-Value storage object
  • comment – The desired comment tag character

Nothing


Get the delimiter tag of the specified EconfFile
ef – Key-Value storage object
the delimiter tag of the EconfFile


Set the delimiter tag of the specified EconfFile
  • ef – Key-Value storage object
  • delimiter – The desired delimiter character

Nothing


Write content of a keyfile to specified location
  • ef – Key-Value storage object
  • save_to_dir – directory into which the file has to be written
  • file_name – filename with suffix of the to be written file

Nothing


Get the path of the source of the given key file
ef – Key-Value storage object
path of the config file as string


Functions for getting values

List all the groups of given keyfile
ef – Key-Value storage object
list of groups in the keyfile


List all the keys of a given group or all keys in a keyfile
  • ef – Key-Value storage object
  • group – group of the keys to be returned or None for keys without a group

list of keys in the given group


Return an integer value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested

value of the key


Return an unsigned integer value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested

value of the key


Return a float value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested

value of the key


Return a string value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested

value of the key


Return a boolean value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested

value of the key


Functions for getting values with defaults

Return an integer value for given group/key or return a default value if key is not found
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • default – value to be returned if no key is found

value of the key


Return an unsigned integer value for given group/key or return a default value if key is not found
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • default – value to be returned if no key is found

value of the key


Return a float value for given group/key or return a default value if key is not found
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • default – value to be returned if no key is found

value of the key


Return a string value for given group/key or return a default value if key is not found
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • default – value to be returned if no key is found

value of the key


Return a boolean value for given group/key or return a default value if key is not found
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • default – value to be returned if no key is found

value of the key


Functions for setting values

Dynamically set a value in a keyfile and returns a status code
  • ef – EconfFile object to set value in
  • group – group of the key to be changed
  • key – key to be changed
  • value – desired value

Nothing


Setting an integer value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • value – value to be set for given key

Nothing


Setting an unsigned integer value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • value – value to be set for given key

Nothing


Setting a float value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • value – value to be set for given key

Nothing


Setting a string value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • value – value to be set for given key

Nothing


Setting a boolean value for given group/key
  • ef – Key-Value storage object
  • group – desired group
  • key – key of the value that is requested
  • value – value to be set for given key

Nothing


Functions for memory management

Free the memory of a given keyfile

This function is called automatically at the end of every objects lifetime and should not be used otherwise

ef – EconfFile to be freed
None


Functions for handling error codes

Convert an error code into error message
error – error code as integer
error string


Info about the line where an error happened
path to the last handled file and number of last handled line


AUTHOR

Nico Krapp

COPYRIGHT

2023, Nico Krapp

November 13, 2023