Skip to main content

parses config files and converts the data types

Project description

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}}
....

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

konfigleser-0.10.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

konfigleser-0.10-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file konfigleser-0.10.tar.gz.

File metadata

  • Download URL: konfigleser-0.10.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for konfigleser-0.10.tar.gz
Algorithm Hash digest
SHA256 0d690a59c701ac1c1805ab64ca81778f43786aaf82b026008a66fb355a3de9e5
MD5 e052adae8c086830abb22166b674fc93
BLAKE2b-256 f3b7554fbc190aca1041e5e7cd2dabba89bebe126245e3d218a310d08d7a7fe9

See more details on using hashes here.

File details

Details for the file konfigleser-0.10-py3-none-any.whl.

File metadata

  • Download URL: konfigleser-0.10-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for konfigleser-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 6ef093e3d5a9405c1bcce21701b5c54f93f804b4f5658a55df921e9f99b197cb
MD5 38f1941c6ccfa6f9903695195ae14c18
BLAKE2b-256 62d1da9d67e5cff18308a12e7f418f90d562eb15c6ba23a723843af651c7face

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page