<!-- markdownlint-disable -->
<p align="center">
<a href="https://github.com/DevelopersToolbox/">
<img src="https://cdn.wolfsoftware.com/assets/images/github/organisations/developerstoolbox/black-and-white-circle-256.png" alt="DevelopersToolbox logo" />
</a>
<br />
<a href="https://github.com/DevelopersToolbox/profiles-config-package/actions/workflows/cicd.yml">
<img src="https://img.shields.io/github/actions/workflow/status/DevelopersToolbox/profiles-config-package/cicd.yml?branch=master&label=build%20status&style=for-the-badge" alt="Github Build Status" />
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/blob/master/LICENSE.md">
<img src="https://img.shields.io/github/license/DevelopersToolbox/profiles-config-package?color=blue&label=License&style=for-the-badge" alt="License">
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package">
<img src="https://img.shields.io/github/created-at/DevelopersToolbox/profiles-config-package?color=blue&label=Created&style=for-the-badge" alt="Created">
</a>
<br />
<a href="https://github.com/DevelopersToolbox/profiles-config-package/releases/latest">
<img src="https://img.shields.io/github/v/release/DevelopersToolbox/profiles-config-package?color=blue&label=Latest%20Release&style=for-the-badge" alt="Release">
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/releases/latest">
<img src="https://img.shields.io/github/release-date/DevelopersToolbox/profiles-config-package?color=blue&label=Released&style=for-the-badge" alt="Released">
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/releases/latest">
<img src="https://img.shields.io/github/commits-since/DevelopersToolbox/profiles-config-package/latest.svg?color=blue&style=for-the-badge" alt="Commits since release">
</a>
<br />
<a href="https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/CODE_OF_CONDUCT.md">
<img src="https://img.shields.io/badge/Code%20of%20Conduct-blue?style=for-the-badge" />
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/CONTRIBUTING.md">
<img src="https://img.shields.io/badge/Contributing-blue?style=for-the-badge" />
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/SECURITY.md">
<img src="https://img.shields.io/badge/Report%20Security%20Concern-blue?style=for-the-badge" />
</a>
<a href="https://github.com/DevelopersToolbox/profiles-config-package/issues">
<img src="https://img.shields.io/badge/Get%20Support-blue?style=for-the-badge" />
</a>
</p>
## Overview
Profiles Config is a Python package designed to handle configuration files with profiles, key-value pairs, and comments. This package
provides functionalities to load and preprocess configuration files, manage comments and whitespace, retrieve profiles and key-value pairs,
display the current configuration, and convert the configuration to a dictionary format.
Like many of our packages this was developed as part of another project, but we felt others might be able to make use of it.
## Features
- **Load and preprocess configuration files**: Reads the configuration file, removes comments and extra spaces.
- **Handle comments and whitespace**: Processes configuration files to ensure clean and accurate data.
- **Retrieve profiles and key-value pairs**: Access specific profiles and their associated key-value pairs.
- **Display the current configuration**: Provides a formatted string representation of the current configuration.
- **Convert the configuration to a dictionary format**: Allows for easy manipulation and access of the configuration data.
## Installation
To install the `profiles-config` package, use pip:
```bash
pip install wolfsoftware.profiles-config
```
## Usage
Here is an example of how to use the `ConfigHandler` class from the `profiles-config` package:
```python
from wolfsoftware.profiles_config import ConfigHandler
# Example usage
if __name__ == "__main__":
config_handler = ConfigHandler("config.ini")
print("All Profiles:", config_handler.list_profiles())
print("Profile 'profile name 1':", config_handler.get_profile("profile name 1"))
print("Value of 'some key' in 'profile name 1':", config_handler.get_value("profile name 1", "some key"))
print("Current Config:\n" + config_handler.display_config())
print("Config as dict:\n", config_handler.get_config())
```
## Example Config File
```
[ profile name 1 ]
# This is a comment for some key
some key = some value
# This is a random comment
# This is a random comment also
[profile name 2] # Comment 1
keys1 = value
keys2 = another value # comment 2
[profile name 3] # Comment 1
keys1 = value
keys2 = another value # comment 2
# more comments
# comments to end the file
# more comments
```
> Note the spaces prefixing and suffixing the 'profile name 1' entry. These are stripped as part of the load and cleanse phase.
Once the config file has been loaded, parsed and cleansed the internal representation is a Python dictionary, which would look like this:
```
{
'profile name 1': {
'some key': 'some value'
},
'profile name 2': {
'keys1': 'value',
'keys2': 'another value'
},
'profile name 3': {
'keys1': 'value',
'keys2': 'another value'
}
}
```
If you use display_config() then the output would look like this:
```
[profile name 1]
some key = some value
[profile name 2]
keys1 = value
keys2 = another value
[profile name 3]
keys1 = value
keys2 = another value
```
## Class Documentation
### ConfigHandler
A class to manage and manipulate configuration files.
#### Attributes
- **filepath**: str - Path to the configuration file.
#### Methods
- **`__init__(self, filepath: str, preserve_case: Optional[bool] = False)`**: Initializes the ConfigHandler with the path to the configuration file.
- **`preprocess_file(self) -> str`**: Reads and preprocesses the configuration file to remove comments and extra spaces.
- **`load_config(self) -> None`**: Loads and processes the configuration file, checking for formatting errors and duplicates.
- **`get_config(self) -> Dict`**: Returns the entire configuration as a dictionary.
- **`get_profile(self, profile_name: str) -> Dict[str, str]`**: Returns a dictionary of key-value pairs for a given profile.
- **`get_value(self, profile_name: str, key_name: str) -> str`**: Returns the value for a specific key in a given profile.
- **`list_profiles(self) -> List[str]`**: Returns a list of all profile names in the configuration file.
- **`display_config(self) -> str`**: Returns the current configuration as a formatted string.
> If you set preserve_case=True it will preserve the case for both sections and keys, otherwise is will lowercase BOTH.
### Method Details
- **`__init__(self, filepath: str, preserve_case: Optional[bool] = False)`**:
- Initializes the ConfigHandler with the path to the configuration file.
- **Arguments**:
- `filepath` (str): Path to the configuration file.
- `preserve_case` Optional[bool]: Override the default and preserve case sensitivity for names.
- **`preprocess_file(self) -> str`**:
- Reads and preprocesses the configuration file to remove comments and extra spaces.
- **Returns**:
- str: A string of the preprocessed configuration file.
- **`load_config(self) -> None`**:
- Loads and processes the configuration file, checking for formatting errors and duplicates.
- **Raises**:
- `FileNotFoundError`: If the configuration file does not exist.
- `ValueError`: If there are duplicate keys in a section or parsing errors.
- **`get_config(self) -> Dict`**:
- Returns the entire configuration as a dictionary.
- **Returns**:
- Dict: A dictionary representation of the entire configuration.
- **`get_profile(self, profile_name: str) -> Dict[str, str]`**:
- Returns a dictionary of key-value pairs for a given profile.
- **Arguments**:
- `profile_name` (str): The name of the profile.
- **Returns**:
- Dict[str, str]: A dictionary of key-value pairs for the given profile.
- **Raises**:
- `KeyError`: If the profile is not found in the configuration.
- **`get_value(self, profile_name: str, key_name: str) -> str`**:
- Returns the value for a specific key in a given profile.
- **Arguments**:
- `profile_name` (str): The name of the profile.
- `key_name` (str): The name of the key.
- **Returns**:
- str: The value associated with the key in the given profile.
- **Raises**:
- `KeyError`: If the profile or key is not found in the configuration.
- **`list_profiles(self) -> List[str]`**:
- Returns a list of all profile names in the configuration file.
- **Returns**:
- List[str]: A list of all profile names.
- **`display_config(self) -> str`**:
- Returns the current configuration as a formatted string.
- **Returns**:
- str: The formatted string representation of the configuration.
<br />
<p align="right"><a href="https://wolfsoftware.com/"><img src="https://img.shields.io/badge/Created%20by%20Wolf%20on%20behalf%20of%20Wolf%20Software-blue?style=for-the-badge" /></a></p>
Raw data
{
"_id": null,
"home_page": "https://github.com/DevelopersToolbox/profiles-config-package",
"name": "wolfsoftware.profiles-config",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "python, profiles, configuration",
"author": "Wolf Software",
"author_email": "pypi@wolfsoftware.com",
"download_url": "https://files.pythonhosted.org/packages/7f/09/26414134689a48720e1350e459c1c907a849b0b1a7c847947e28069cc648/wolfsoftware_profiles_config-0.1.0.tar.gz",
"platform": null,
"description": "<!-- markdownlint-disable -->\n<p align=\"center\">\n <a href=\"https://github.com/DevelopersToolbox/\">\n <img src=\"https://cdn.wolfsoftware.com/assets/images/github/organisations/developerstoolbox/black-and-white-circle-256.png\" alt=\"DevelopersToolbox logo\" />\n </a>\n <br />\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/actions/workflows/cicd.yml\">\n <img src=\"https://img.shields.io/github/actions/workflow/status/DevelopersToolbox/profiles-config-package/cicd.yml?branch=master&label=build%20status&style=for-the-badge\" alt=\"Github Build Status\" />\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/blob/master/LICENSE.md\">\n <img src=\"https://img.shields.io/github/license/DevelopersToolbox/profiles-config-package?color=blue&label=License&style=for-the-badge\" alt=\"License\">\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package\">\n <img src=\"https://img.shields.io/github/created-at/DevelopersToolbox/profiles-config-package?color=blue&label=Created&style=for-the-badge\" alt=\"Created\">\n </a>\n <br />\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/releases/latest\">\n <img src=\"https://img.shields.io/github/v/release/DevelopersToolbox/profiles-config-package?color=blue&label=Latest%20Release&style=for-the-badge\" alt=\"Release\">\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/releases/latest\">\n <img src=\"https://img.shields.io/github/release-date/DevelopersToolbox/profiles-config-package?color=blue&label=Released&style=for-the-badge\" alt=\"Released\">\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/releases/latest\">\n <img src=\"https://img.shields.io/github/commits-since/DevelopersToolbox/profiles-config-package/latest.svg?color=blue&style=for-the-badge\" alt=\"Commits since release\">\n </a>\n <br />\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/CODE_OF_CONDUCT.md\">\n <img src=\"https://img.shields.io/badge/Code%20of%20Conduct-blue?style=for-the-badge\" />\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/CONTRIBUTING.md\">\n <img src=\"https://img.shields.io/badge/Contributing-blue?style=for-the-badge\" />\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/blob/master/.github/SECURITY.md\">\n <img src=\"https://img.shields.io/badge/Report%20Security%20Concern-blue?style=for-the-badge\" />\n </a>\n <a href=\"https://github.com/DevelopersToolbox/profiles-config-package/issues\">\n <img src=\"https://img.shields.io/badge/Get%20Support-blue?style=for-the-badge\" />\n </a>\n</p>\n\n## Overview\n\nProfiles Config is a Python package designed to handle configuration files with profiles, key-value pairs, and comments. This package\nprovides functionalities to load and preprocess configuration files, manage comments and whitespace, retrieve profiles and key-value pairs,\ndisplay the current configuration, and convert the configuration to a dictionary format.\n\nLike many of our packages this was developed as part of another project, but we felt others might be able to make use of it.\n\n## Features\n\n- **Load and preprocess configuration files**: Reads the configuration file, removes comments and extra spaces.\n- **Handle comments and whitespace**: Processes configuration files to ensure clean and accurate data.\n- **Retrieve profiles and key-value pairs**: Access specific profiles and their associated key-value pairs.\n- **Display the current configuration**: Provides a formatted string representation of the current configuration.\n- **Convert the configuration to a dictionary format**: Allows for easy manipulation and access of the configuration data.\n\n## Installation\n\nTo install the `profiles-config` package, use pip:\n\n```bash\npip install wolfsoftware.profiles-config\n```\n\n## Usage\n\nHere is an example of how to use the `ConfigHandler` class from the `profiles-config` package:\n\n```python\nfrom wolfsoftware.profiles_config import ConfigHandler\n\n# Example usage\nif __name__ == \"__main__\":\n config_handler = ConfigHandler(\"config.ini\")\n print(\"All Profiles:\", config_handler.list_profiles())\n print(\"Profile 'profile name 1':\", config_handler.get_profile(\"profile name 1\"))\n print(\"Value of 'some key' in 'profile name 1':\", config_handler.get_value(\"profile name 1\", \"some key\"))\n print(\"Current Config:\\n\" + config_handler.display_config())\n print(\"Config as dict:\\n\", config_handler.get_config())\n```\n\n## Example Config File\n\n```\n[ profile name 1 ]\n# This is a comment for some key\nsome key = some value\n\n# This is a random comment\n# This is a random comment also\n[profile name 2] # Comment 1\nkeys1 = value\nkeys2 = another value # comment 2\n\n[profile name 3] # Comment 1\nkeys1 = value\nkeys2 = another value # comment 2\n\n# more comments\n# comments to end the file\n# more comments\n```\n\n> Note the spaces prefixing and suffixing the 'profile name 1' entry. These are stripped as part of the load and cleanse phase.\n\nOnce the config file has been loaded, parsed and cleansed the internal representation is a Python dictionary, which would look like this:\n\n```\n{\n 'profile name 1': {\n 'some key': 'some value'\n },\n 'profile name 2': {\n 'keys1': 'value',\n 'keys2': 'another value'\n }, \n 'profile name 3': {\n 'keys1': 'value', \n 'keys2': 'another value'\n }\n}\n```\n\nIf you use display_config() then the output would look like this:\n\n```\n[profile name 1]\nsome key = some value\n\n[profile name 2]\nkeys1 = value\nkeys2 = another value\n\n[profile name 3]\nkeys1 = value\nkeys2 = another value\n```\n\n## Class Documentation\n\n### ConfigHandler\n\nA class to manage and manipulate configuration files.\n\n#### Attributes\n\n- **filepath**: str - Path to the configuration file.\n\n#### Methods\n\n- **`__init__(self, filepath: str, preserve_case: Optional[bool] = False)`**: Initializes the ConfigHandler with the path to the configuration file.\n- **`preprocess_file(self) -> str`**: Reads and preprocesses the configuration file to remove comments and extra spaces.\n- **`load_config(self) -> None`**: Loads and processes the configuration file, checking for formatting errors and duplicates.\n- **`get_config(self) -> Dict`**: Returns the entire configuration as a dictionary.\n- **`get_profile(self, profile_name: str) -> Dict[str, str]`**: Returns a dictionary of key-value pairs for a given profile.\n- **`get_value(self, profile_name: str, key_name: str) -> str`**: Returns the value for a specific key in a given profile.\n- **`list_profiles(self) -> List[str]`**: Returns a list of all profile names in the configuration file.\n- **`display_config(self) -> str`**: Returns the current configuration as a formatted string.\n\n> If you set preserve_case=True it will preserve the case for both sections and keys, otherwise is will lowercase BOTH.\n\n### Method Details\n\n- **`__init__(self, filepath: str, preserve_case: Optional[bool] = False)`**:\n - Initializes the ConfigHandler with the path to the configuration file.\n - **Arguments**:\n - `filepath` (str): Path to the configuration file.\n - `preserve_case` Optional[bool]: Override the default and preserve case sensitivity for names.\n\n- **`preprocess_file(self) -> str`**:\n - Reads and preprocesses the configuration file to remove comments and extra spaces.\n - **Returns**:\n - str: A string of the preprocessed configuration file.\n\n- **`load_config(self) -> None`**:\n - Loads and processes the configuration file, checking for formatting errors and duplicates.\n - **Raises**:\n - `FileNotFoundError`: If the configuration file does not exist.\n - `ValueError`: If there are duplicate keys in a section or parsing errors.\n\n- **`get_config(self) -> Dict`**:\n - Returns the entire configuration as a dictionary.\n - **Returns**:\n - Dict: A dictionary representation of the entire configuration.\n\n- **`get_profile(self, profile_name: str) -> Dict[str, str]`**:\n - Returns a dictionary of key-value pairs for a given profile.\n - **Arguments**:\n - `profile_name` (str): The name of the profile.\n - **Returns**:\n - Dict[str, str]: A dictionary of key-value pairs for the given profile.\n - **Raises**:\n - `KeyError`: If the profile is not found in the configuration.\n\n- **`get_value(self, profile_name: str, key_name: str) -> str`**:\n - Returns the value for a specific key in a given profile.\n - **Arguments**:\n - `profile_name` (str): The name of the profile.\n - `key_name` (str): The name of the key.\n - **Returns**:\n - str: The value associated with the key in the given profile.\n - **Raises**:\n - `KeyError`: If the profile or key is not found in the configuration.\n\n- **`list_profiles(self) -> List[str]`**:\n - Returns a list of all profile names in the configuration file.\n - **Returns**:\n - List[str]: A list of all profile names.\n\n- **`display_config(self) -> str`**:\n - Returns the current configuration as a formatted string.\n - **Returns**:\n - str: The formatted string representation of the configuration.\n\n<br />\n<p align=\"right\"><a href=\"https://wolfsoftware.com/\"><img src=\"https://img.shields.io/badge/Created%20by%20Wolf%20on%20behalf%20of%20Wolf%20Software-blue?style=for-the-badge\" /></a></p>\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A basic package for managing profile based configuration files.",
"version": "0.1.0",
"project_urls": {
"Documentation": "https://github.com/DevelopersToolbox/profiles-config-package",
"Homepage": "https://github.com/DevelopersToolbox/profiles-config-package",
"Source": "https://github.com/DevelopersToolbox/profiles-config-package",
"Sponsor": "https://github.com/sponsors/WolfSoftware",
"Tracker": "https://github.com/wDevelopersToolbox/profiles-config-package/issues/"
},
"split_keywords": [
"python",
" profiles",
" configuration"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "726ae0f0ea79b6f2d9f335efe9997757d8c2a019b86d34ccc5ce0ba4f308f14e",
"md5": "3ebb19cf6865462038e8417931e99b77",
"sha256": "e49ed7fb7c2cea3f3bc7db841b8024dfe3f8a497a7b5967da36cc10a039b8c83"
},
"downloads": -1,
"filename": "wolfsoftware.profiles_config-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3ebb19cf6865462038e8417931e99b77",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 7495,
"upload_time": "2024-05-29T17:35:06",
"upload_time_iso_8601": "2024-05-29T17:35:06.904644Z",
"url": "https://files.pythonhosted.org/packages/72/6a/e0f0ea79b6f2d9f335efe9997757d8c2a019b86d34ccc5ce0ba4f308f14e/wolfsoftware.profiles_config-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7f0926414134689a48720e1350e459c1c907a849b0b1a7c847947e28069cc648",
"md5": "74645e72137081f68df771716d324297",
"sha256": "fbf94935685580621cca6fd476ece71158427ca3dca5d826cd3ae3a81993c29f"
},
"downloads": -1,
"filename": "wolfsoftware_profiles_config-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "74645e72137081f68df771716d324297",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 9646,
"upload_time": "2024-05-29T17:35:08",
"upload_time_iso_8601": "2024-05-29T17:35:08.835844Z",
"url": "https://files.pythonhosted.org/packages/7f/09/26414134689a48720e1350e459c1c907a849b0b1a7c847947e28069cc648/wolfsoftware_profiles_config-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-29 17:35:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "DevelopersToolbox",
"github_project": "profiles-config-package",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": [
[
"==",
"8.2.1"
]
]
},
{
"name": "setuptools",
"specs": [
[
"==",
"70.0.0"
]
]
}
],
"lcname": "wolfsoftware.profiles-config"
}