Skip to main content

DevelopersToolbox logo
Github Build Status License Created
Release Released Commits since release

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:

pip install wolfsoftware.profiles-config

Usage

Here is an example of how to use the ConfigHandler class from the profiles-config package:

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.

Release files for wolfsoftware.profiles-config 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wolfsoftware.profiles-config 0.1.1
File Size Uploaded
wolfsoftware_profiles_config-0.1.1.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wolfsoftware.profiles-config 0.1.1
File Interpreter ABI Platform
wolfsoftware.profiles_config-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / wolfsoftware_profiles_config-0.1.1.tar.gz

Download URL wolfsoftware_profiles_config-0.1.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1c564fea2ab626b922109e20cfdce1aa6f53b3424183934699305f1a8c5a736e
BLAKE2b-256 checksum
How to use checksums
59862fed1c94b40b047fe0f0616131e9e3fc5decf653be42c043bebee271cf84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / wolfsoftware.profiles_config-0.1.1-py3-none-any.whl

Download URL wolfsoftware.profiles_config-0.1.1-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ef172555de5fbf70dd9b4422b716016539fc501afb5def93cc9e378c2abf326
BLAKE2b-256 checksum
How to use checksums
f40776a906a1718c70b90518d06effcf99f09b4d30b8b7af863411f2e1b250f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page