konfigleser


Namekonfigleser JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/konfigleser
Summaryparses config files and converts the data types
upload_time2023-08-20 01:12:29
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords config parser
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# parses config files and converts the data types 

## pip install konfigleser


```python

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

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/konfigleser",
    "name": "konfigleser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "config,parser",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f3/b7/554fbc190aca1041e5e7cd2dabba89bebe126245e3d218a310d08d7a7fe9/konfigleser-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# parses config files and converts the data types \r\n\r\n## pip install konfigleser\r\n\r\n\r\n```python\r\n\r\n# parse_data_from_config_file\r\n\tparse_data_from_config_file converts configuration data from the configparser format into a more convenient structure (MultiKeyDict)\r\n\tthat allows for efficient data manipulation using multi-level keys. This transformation can make it\r\n\tsimpler to access and update configuration values. Besides that, it handles type conversions\r\n\r\n\tParses data from a configuration file and returns values, keys, and a MultiKeyDict object.\r\n\r\n\r\n\tArgs:\r\n\t\tcfgfile (str): Path to the configuration file.\r\n\t\tencoding (str, optional): Encoding of the file. Defaults to \"utf-8\".\r\n\t\tonezeroasboolean (bool, optional): Treat \"1\" and \"0\" as boolean values. Defaults to False.\r\n\r\n\tReturns:\r\n\t\ttuple: A tuple containing:\r\n\t\t\t- list[tuple]: List of tuples containing keys and values extracted from the configuration.\r\n\t\t\t- MultiKeyDict: A MultiKeyDict object containing the extracted data.\r\n\r\n# write_config_file\r\n    Writes the data from a dictionary into a configuration file.\r\n\r\n    Args:\r\n        d (dict): The dictionary containing the data to be written.\r\n        filepath (str): Path to the output configuration file.\r\n        encoding (str): encoding - defaults to \"utf-8\"\r\n\r\nfrom konfigleser import write_config_file,parse_data_from_config_file\r\nexample = '''\r\n[DEFAULT]\r\nServerAliveInterval = 45\r\nCompression = yes\r\nCompressionLevel = 9\r\nForwardX11 = yes\r\n\r\n[forge.example]\r\nUser = hg\r\n\r\n[topsecret.server.example]\r\nPort = 50022\r\nForwardX11 = no\r\n'''\r\n\r\ncfgtestfile = 'c:\\\\testestest.ini'\r\ncfgtestfileoutput = 'c:\\\\testestestout.ini'\r\n\r\n# Create example configuration file\r\nwith open(cfgtestfile, mode='w', encoding='utf-8') as f:\r\n\tf.write(example)\r\n\r\n# Parse, modify, and write configuration data\r\nvaluesandkeys, configdict = parse_data_from_config_file(cfgfile=cfgtestfile)\r\n\r\nvaluesandkeys, configdict = parse_data_from_config_file(cfgfile=example) # also possible\r\nprint(f'{valuesandkeys=}')\r\nprint(f'{configdict=}')\r\nfor ra in range(4):\r\n\tconfigdict[[f\"cat1{ra}\", \"value1\"]] = ra * 2\r\n\tconfigdict[[f\"othercat1{ra}\", \"value2\"]] = ra * ra\r\n\tconfigdict[[f\"cat1{ra}\", \"value3\", 'value31']] = ra * 2\r\n\tconfigdict[[f\"othercat1{ra}\", \"value4\",'value31']] = ra * ra\r\n\tconfigdict[[f\"cat1{ra}\", \"value5\", 'value31','value21']] = ra * 2\r\n\tconfigdict[[f\"othercat1{ra}\", \"value6\",'value31','value21']] = ra * ra\r\nwrite_config_file(d=configdict, filepath=cfgtestfileoutput)\r\nprint('----------------------------------------------------')\r\n\r\nvaluesandkeys, configdict = parse_data_from_config_file(cfgfile=cfgtestfileoutput)\r\nprint(f'{valuesandkeys=}')\r\nprint(f'{configdict=}')\r\n\r\nprint('----------------------------------------------------')\r\n\r\n#output:\r\n\r\nvaluesandkeys=[('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'))]\r\nconfigdict={'DEFAULT': {'compression': 'yes',\r\n 'compressionlevel': '9',\r\n 'forwardx11': 'yes',\r\n 'serveraliveinterval': '45'},\r\n 'forge.example': {'user': 'hg'},\r\n 'topsecret.server.example': {'forwardx11': False,\r\n 'port': 50022}}\r\n----------------------------------------------------\r\nvaluesandkeys=[('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'))]\r\nconfigdict={'DEFAULT': {'compression': 'yes',\r\n 'compressionlevel': '9',\r\n 'forwardx11': 'yes',\r\n 'serveraliveinterval': '45'},\r\n 'cat10': {'value1': 0,\r\n 'value3': {'value31': 0},\r\n 'value5': {'value31': {'value21': 0}}},\r\n 'cat11': {'value1': 2,\r\n 'value3': {'value31': 2},\r\n 'value5': {'value31': {'value21': 2}}},\r\n 'cat12': {'value1': 4,\r\n 'value3': {'value31': 4},\r\n 'value5': {'value31': {'value21': 4}}},\r\n 'cat13': {'value1': 6,\r\n 'value3': {'value31': 6},\r\n 'value5': {'value31': {'value21': 6}}},\r\n 'forge.example': {'user': 'hg'},\r\n 'othercat10': {'value2': 0,\r\n 'value4': {'value31': 0},\r\n 'value6': {'value31': {'value21': 0}}},\r\n 'othercat11': {'value2': 1,\r\n 'value4': {'value31': 1},\r\n 'value6': {'value31': {'value21': 1}}},\r\n 'othercat12': {'value2': 4,\r\n 'value4': {'value31': 4},\r\n 'value6': {'value31': {'value21': 4}}},\r\n 'othercat13': {'value2': 9,\r\n 'value4': {'value31': 9},\r\n 'value6': {'value31': {'value21': 9}}},\r\n 'topsecret.server.example': {'forwardx11': False,\r\n 'port': 50022}}\r\n----------------------------------------------------\r\n# file:\r\n[DEFAULT]\r\nserveraliveinterval = 45\r\ncompression = yes\r\ncompressionlevel = 9\r\nforwardx11 = yes\r\n\r\n[forge.example]\r\nuser = hg\r\n\r\n[topsecret.server.example]\r\nport = 50022\r\nforwardx11 = False\r\n\r\n[cat10]\r\nvalue1 = 0\r\nvalue3 = {'value31': 0}\r\nvalue5 = {'value31': {'value21': 0}}\r\n\r\n[othercat10]\r\nvalue2 = 0\r\nvalue4 = {'value31': 0}\r\nvalue6 = {'value31': {'value21': 0}}\r\n....\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "parses config files and converts the data types",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/konfigleser"
    },
    "split_keywords": [
        "config",
        "parser"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "62d1da9d67e5cff18308a12e7f418f90d562eb15c6ba23a723843af651c7face",
                "md5": "38f1941c6ccfa6f9903695195ae14c18",
                "sha256": "6ef093e3d5a9405c1bcce21701b5c54f93f804b4f5658a55df921e9f99b197cb"
            },
            "downloads": -1,
            "filename": "konfigleser-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "38f1941c6ccfa6f9903695195ae14c18",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8966,
            "upload_time": "2023-08-20T01:12:27",
            "upload_time_iso_8601": "2023-08-20T01:12:27.876769Z",
            "url": "https://files.pythonhosted.org/packages/62/d1/da9d67e5cff18308a12e7f418f90d562eb15c6ba23a723843af651c7face/konfigleser-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f3b7554fbc190aca1041e5e7cd2dabba89bebe126245e3d218a310d08d7a7fe9",
                "md5": "e052adae8c086830abb22166b674fc93",
                "sha256": "0d690a59c701ac1c1805ab64ca81778f43786aaf82b026008a66fb355a3de9e5"
            },
            "downloads": -1,
            "filename": "konfigleser-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "e052adae8c086830abb22166b674fc93",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6637,
            "upload_time": "2023-08-20T01:12:29",
            "upload_time_iso_8601": "2023-08-20T01:12:29.699614Z",
            "url": "https://files.pythonhosted.org/packages/f3/b7/554fbc190aca1041e5e7cd2dabba89bebe126245e3d218a310d08d7a7fe9/konfigleser-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-20 01:12:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "konfigleser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "konfigleser"
}
        
Elapsed time: 0.14213s