parses config files and converts the data types
pip install konfigleser
# parse_data_from_config_file
parse_data_from_config_file converts configuration data from the configparser format into a more convenient structure (MultiKeyDict)
that allows for efficient data manipulation using multi-level keys. This transformation can make it
simpler to access and update configuration values. Besides that, it handles type conversions
Parses data from a configuration file and returns values, keys, and a MultiKeyDict object.
Args:
cfgfile (str): Path to the configuration file.
encoding (str, optional): Encoding of the file. Defaults to "utf-8".
onezeroasboolean (bool, optional): Treat "1" and "0" as boolean values. Defaults to False.
Returns:
tuple: A tuple containing:
- list[tuple]: List of tuples containing keys and values extracted from the configuration.
- MultiKeyDict: A MultiKeyDict object containing the extracted data.
# write_config_file
Writes the data from a dictionary into a configuration file.
Args:
d (dict): The dictionary containing the data to be written.
filepath (str): Path to the output configuration file.
encoding (str): encoding - defaults to "utf-8"
from konfigleser import write_config_file,parse_data_from_config_file
example = '''
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[forge.example]
User = hg
[topsecret.server.example]
Port = 50022
ForwardX11 = no
'''
cfgtestfile = 'c:\\testestest.ini'
cfgtestfileoutput = 'c:\\testestestout.ini'
# Create example configuration file
with open(cfgtestfile, mode='w', encoding='utf-8') as f:
f.write(example)
# Parse, modify, and write configuration data
valuesandkeys, configdict = parse_data_from_config_file(cfgfile=cfgtestfile)
valuesandkeys, configdict = parse_data_from_config_file(cfgfile=example) # also possible
print(f'{valuesandkeys=}')
print(f'{configdict=}')
for ra in range(4):
configdict[[f"cat1{ra}", "value1"]] = ra * 2
configdict[[f"othercat1{ra}", "value2"]] = ra * ra
configdict[[f"cat1{ra}", "value3", 'value31']] = ra * 2
configdict[[f"othercat1{ra}", "value4",'value31']] = ra * ra
configdict[[f"cat1{ra}", "value5", 'value31','value21']] = ra * 2
configdict[[f"othercat1{ra}", "value6",'value31','value21']] = ra * ra
write_config_file(d=configdict, filepath=cfgtestfileoutput)
print('----------------------------------------------------')
valuesandkeys, configdict = parse_data_from_config_file(cfgfile=cfgtestfileoutput)
print(f'{valuesandkeys=}')
print(f'{configdict=}')
print('----------------------------------------------------')
#output:
valuesandkeys=[('hg', ('forge.example', 'user')), (50022, ('topsecret.server.example', 'port')), (False, ('topsecret.server.example', 'forwardx11')), ('45', ('DEFAULT', 'serveraliveinterval')), ('yes', ('DEFAULT', 'compression')), ('9', ('DEFAULT', 'compressionlevel')), ('yes', ('DEFAULT', 'forwardx11'))]
configdict={'DEFAULT': {'compression': 'yes',
'compressionlevel': '9',
'forwardx11': 'yes',
'serveraliveinterval': '45'},
'forge.example': {'user': 'hg'},
'topsecret.server.example': {'forwardx11': False,
'port': 50022}}
----------------------------------------------------
valuesandkeys=[('hg', ('forge.example', 'user')), (50022, ('topsecret.server.example', 'port')), (False, ('topsecret.server.example', 'forwardx11')), (0, ('cat10', 'value1')), (0, ('cat10', 'value3', 'value31')), (0, ('cat10', 'value5', 'value31', 'value21')), (0, ('othercat10', 'value2')), (0, ('othercat10', 'value4', 'value31')), (0, ('othercat10', 'value6', 'value31', 'value21')), (2, ('cat11', 'value1')), (2, ('cat11', 'value3', 'value31')), (2, ('cat11', 'value5', 'value31', 'value21')), (1, ('othercat11', 'value2')), (1, ('othercat11', 'value4', 'value31')), (1, ('othercat11', 'value6', 'value31', 'value21')), (4, ('cat12', 'value1')), (4, ('cat12', 'value3', 'value31')), (4, ('cat12', 'value5', 'value31', 'value21')), (4, ('othercat12', 'value2')), (4, ('othercat12', 'value4', 'value31')), (4, ('othercat12', 'value6', 'value31', 'value21')), (6, ('cat13', 'value1')), (6, ('cat13', 'value3', 'value31')), (6, ('cat13', 'value5', 'value31', 'value21')), (9, ('othercat13', 'value2')), (9, ('othercat13', 'value4', 'value31')), (9, ('othercat13', 'value6', 'value31', 'value21')), ('45', ('DEFAULT', 'serveraliveinterval')), ('yes', ('DEFAULT', 'compression')), ('9', ('DEFAULT', 'compressionlevel')), ('yes', ('DEFAULT', 'forwardx11'))]
configdict={'DEFAULT': {'compression': 'yes',
'compressionlevel': '9',
'forwardx11': 'yes',
'serveraliveinterval': '45'},
'cat10': {'value1': 0,
'value3': {'value31': 0},
'value5': {'value31': {'value21': 0}}},
'cat11': {'value1': 2,
'value3': {'value31': 2},
'value5': {'value31': {'value21': 2}}},
'cat12': {'value1': 4,
'value3': {'value31': 4},
'value5': {'value31': {'value21': 4}}},
'cat13': {'value1': 6,
'value3': {'value31': 6},
'value5': {'value31': {'value21': 6}}},
'forge.example': {'user': 'hg'},
'othercat10': {'value2': 0,
'value4': {'value31': 0},
'value6': {'value31': {'value21': 0}}},
'othercat11': {'value2': 1,
'value4': {'value31': 1},
'value6': {'value31': {'value21': 1}}},
'othercat12': {'value2': 4,
'value4': {'value31': 4},
'value6': {'value31': {'value21': 4}}},
'othercat13': {'value2': 9,
'value4': {'value31': 9},
'value6': {'value31': {'value21': 9}}},
'topsecret.server.example': {'forwardx11': False,
'port': 50022}}
----------------------------------------------------
# file:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[forge.example]
user = hg
[topsecret.server.example]
port = 50022
forwardx11 = False
[cat10]
value1 = 0
value3 = {'value31': 0}
value5 = {'value31': {'value21': 0}}
[othercat10]
value2 = 0
value4 = {'value31': 0}
value6 = {'value31': {'value21': 0}}
....
Metadata
Release files for konfigleser 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| konfigleser-0.10.tar.gz | 6.6 kB | Without provenance | Details for konfigleser-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| konfigleser-0.10-py3-none-any.whl | Python 3 | none | any | Details for konfigleser-0.10-py3-none-any.whl |
Total release size: 15.6 kB
Release files / konfigleser-0.10.tar.gz
| Download URL | konfigleser-0.10.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d690a59c701ac1c1805ab64ca81778f43786aaf82b026008a66fb355a3de9e5
|
|
BLAKE2b-256 checksum How to use checksums |
f3b7554fbc190aca1041e5e7cd2dabba89bebe126245e3d218a310d08d7a7fe9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Release files / konfigleser-0.10-py3-none-any.whl
| Download URL | konfigleser-0.10-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ef093e3d5a9405c1bcce21701b5c54f93f804b4f5658a55df921e9f99b197cb
|
|
BLAKE2b-256 checksum How to use checksums |
62d1da9d67e5cff18308a12e7f418f90d562eb15c6ba23a723843af651c7face
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Security for konfigleser 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.