PYTHON-LIBECONF(3)
π python-libeconf π
okay so bestie, welcome to python-libeconf(3) β that's the python-libeconf manual, section 3, if you're keeping track π
python-libeconf is a Python Library which offers Python bindings for libeconf. β¨
And libeconf? She's a highly flexible and configurable library to parse and manage key=value configuration files. The whole serve is that she reads configuration file snippets from different directories and builds the final configuration file for the application from it. It's giving assembly line, but make it config π
π CONTENTS
- Usage
- install
- usage example
- API
π 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! No cap, check first π
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
like, literally just vibe with it:
>>> 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")
>>>
ate, honestly π
API
π§ Functions to interact with config files
econf.read_file(file_name: str | bytes, delim: str | bytes, comment: str | bytes) -> EconfFileβ 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.
'#'
Returns: Key-Value storage object
econf.read_file_with_callback(file_name: str | bytes, delim: str | bytes, comment: str | bytes, callback: Callable[[any], bool], callback_data: any) -> EconfFileβ 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
Returns: Key-Value storage object
econf.new_key_file(delim: str | bytes, comment: str | bytes) -> EconfFileβ Create a new empty keyfile π§- delim β delimiter of a key/value e.g.
'=' - comment β string that defines the start of a comment e.g.
'#'
- delim β delimiter of a key/value e.g.
Returns: created EconfFile object
econf.new_ini_file() -> EconfFileβ Create a new empty keyfile with delimiter'='and comment'#'β¨
Returns: created EconfFile object
econf.merge_files(usr_file: EconfFile, etc_file: EconfFile) -> EconfFileβ Merge the content of 2 keyfile objects π«Ά- usr_file β first
EconfFileobject - etc_file β second
EconfFileobject
- usr_file β first
Returns: merged EconfFile object
econf.read_config(project: str |bytes, usr_conf_dir: str | bytes, config_name: str | bytes, config_suffix: str | bytes, delim: str | bytes, comment: str | bytes) -> EconfFileβ 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". Like, certified iconic π
- 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.
'#'
Returns: merged EconfFile object
econf.read_config_with_callback(project: str | bytes, usr_conf_dir: str | bytes, config_name: str | bytes, config_suffix: str | bytes, delim: str | bytes, comment: str | bytes, callback: Callable[[any], bool], callback_data: any) -> EconfFileβ 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
Returns: merged EconfFile object
econf.comment_tag(ef: EconfFile) -> strβ Get the comment tag of the specifiedEconfFileπ- ef β Key-Value storage object
Returns: The comment tag of the EconfFile
econf.set_comment_tag(ef: EconfFile, comment: str | bytes) -> Noneβ Set the comment tag of the specifiedEconfFileβοΈ- ef β Key-Value storage object
- comment β The desired comment tag character
Returns: Nothing
econf.delimiter_tag(ef: EconfFile) -> strβ Get the delimiter tag of the specifiedEconfFileπ- ef β Key-Value storage object
Returns: the delimiter tag of the EconfFile
econf.set_delimiter_tag(ef: EconfFile, delimiter: str | bytes) -> Noneβ Set the delimiter tag of the specifiedEconfFileβοΈ- ef β Key-Value storage object
- delimiter β The desired delimiter character
Returns: Nothing
econf.write_file(ef: EconfFile, save_to_dir: str, file_name: str) -> Noneβ 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
Returns: Nothing
econf.get_path(ef: EconfFile) -> strβ Get the path of the source of the given key file πΊοΈ- ef β Key-Value storage object
Returns: path of the config file as string
π Functions for getting values
econf.get_groups(ef: EconfFile) -> list[str]β List all the groups of given keyfile πΈ- ef β Key-Value storage object
Returns: list of groups in the keyfile
econf.get_keys(ef: EconfFile, group: str) -> list[str]β 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
Nonefor keys without a group
Returns: list of keys in the given group
econf.get_int_value(ef: EconfFile, group: str, key: str) -> intβ Return an integer value for given group/key π’- ef β Key-Value storage object
- group β desired group
- key β key of the value that is requested
Returns: value of the key
econf.get_uint_value(ef: EconfFile, group: str, key: str) -> intβ 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
Returns: value of the key
econf.get_float_value(ef: EconfFile, group: str, key: str) -> floatβ Return a float value for given group/key π- ef β Key-Value storage object
- group β desired group
- key β key of the value that is requested
Returns: value of the key
econf.get_string_value(ef: EconfFile, group: str, key: str) -> strβ Return a string value for given group/key π- ef β Key-Value storage object
- group β desired group
- key β key of the value that is requested
Returns: value of the key
econf.get_bool_value(ef: EconfFile, group: str, key: str) -> boolβ Return a boolean value for given group/key β- ef β Key-Value storage object
- group β desired group
- key β key of the value that is requested
Returns: value of the key
π Functions for getting values with defaults
Okay so this little family of five is literally my favorite thing ever β every single one of them does the exact same bit: try to get a value from a group/key, and if that key is not there, hand you back a default instead of crying. No exceptions, no drama, we love a safety net π«Ά
β¨ econf.get_int_value_def
econf.get_int_value_def(ef: EconfFile, group: str, key: str, default: int) -> int
Returns an integer value for the given group/key, or the default value if the key is not found.
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingdefaultβ the value that comes back if no key is found
Returns β the value of the key π
β¨ econf.get_uint_value_def
econf.get_uint_value_def(ef: EconfFile, group: str, key: str, default: int) -> int
Returns an unsigned integer value for the given group/key, or the default value if the key is not found. Same energy, no negatives allowed bestie π«
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingdefaultβ the value that comes back if no key is found
Returns β the value of the key πΈ
β¨ econf.get_float_value_def
econf.get_float_value_def(ef: EconfFile, group: str, key: str, default: float) -> float
Returns a float value for the given group/key, or the default value if the key is not found. Decimals girlies rise β¨
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingdefaultβ the value that comes back if no key is found
Returns β the value of the key π¦
β¨ econf.get_string_value_def
econf.get_string_value_def(ef: EconfFile, group: str, key: str, default: str) -> str
Returns a string value for the given group/key, or the default value if the key is not found. Text? iconic, obsessed π
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingdefaultβ the value that comes back if no key is found
Returns β the value of the key π
β¨ econf.get_bool_value_def
econf.get_bool_value_def(ef: EconfFile, group: str, key: str, default: bool) -> bool
Returns a boolean value for the given group/key, or the default value if the key is not found. Yes/no, true/false, slay/not slay π
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingdefaultβ the value that comes back if no key is found
Returns β the value of the key β
π Functions for setting values
Now we flip it β instead of reading, we're writing into the keyfile. Main character energy fr fr π
π
econf.set_value
econf.set_value(ef: EconfFile, group: str | bytes, key: str | bytes, value: int | float | str | bool) -> None
Dynamically sets a value in a keyfile and returns a status code.
Parameters
efβ theEconfFileobject to set the value ingroupβ the group of the key to be changedkeyβ the key to be changedvalueβ the value you want
Returns β nothing
ngl the description says it returns a status code but the Returns line says Nothing β that's just how the page is written, so I'm passing it to you exactly as-is π
π
econf.set_int_value
econf.set_int_value(ef: EconfFile, group: str, key: str, value: int) -> None
Sets an integer value for the given group/key.
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingvalueβ the value to be set for the given key
Returns β nothing π
π
econf.set_uint_value
econf.set_uint_value(ef: EconfFile, group: str, key: str, value: int) -> None
Sets an unsigned integer value for the given group/key.
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingvalueβ the value to be set for the given key
Returns β nothing π§
π
econf.set_float_value
econf.set_float_value(ef: EconfFile, group: str, key: str, value: float) -> None
Sets a float value for the given group/key.
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingvalueβ the value to be set for the given key
Returns β nothing π
π
econf.set_string_value
econf.set_string_value(ef: EconfFile, group: str, key: str, value: str | bytes) -> None
Sets a string value for the given group/key. Note the str | bytes on value β both are welcome here π
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingvalueβ the value to be set for the given key
Returns β nothing π¦
π
econf.set_bool_value
econf.set_bool_value(ef: EconfFile, group: str, key: str, value: bool) -> None
Sets a boolean value for the given group/key.
Parameters
efβ the Key-Value storage objectgroupβ the group you wantkeyβ the key of the value you're requestingvalueβ the value to be set for the given key
Returns β nothing β¨
π§ Functions for memory management
Short section, but honestly the most important warning on this whole page. Pay attention bestie π
econf.free_file
econf.free_file(ef: EconfFile)
Frees the memory of a given keyfile.
β οΈ This function is called automatically at the end of every object's lifetime and should not be used otherwise. Like, don't touch it. It's already handling itself. Using it manually is giving "double free" and we do not want that π
Parameters
efβ theEconfFileto be freed
Returns β None
π₯ Functions for handling error codes
When things go sideways (they won't, but like, if), these two are your group chat when something breaks π
econf.err_string
econf.err_string(error: int) -> str
Converts an error code into an error message.
Parameters
errorβ the error code, as an integer
Returns β the error string πΈ
econf.err_location
econf.err_location() -> Tuple[str, int]
Info about the line where an error happened.
Returns β the path to the last handled file and the number of the last handled line π«Ά
π AUTHOR
Nico Krapp
π COPYRIGHT
2023, Nico Krapp
okay that's the whole page, you're officially a libeconf girlie now π
β¨ mwah, go parse something pretty β this page was from Nov 13, 2023 and it's the PYTHON-LIBECONF(3) manual, so go forth and set some values ππ¦
π¦ where this page comes from
- opensuse-leap 16.0: python-libeconf-doc 0.7.9-160000.2.2 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms