A powerful and flexible configuration management library that supports both INI and JSON formats with automatic type conversion, list/dictionary parsing, and class-based interfaces.
Project description
๐ ConfigSet - Enhanced Configuration Management Library
๐ A powerful and flexible configuration management library that supports both INI, JSON and YAML formats with automatic type conversion, list/dictionary parsing, and class-based interfaces.
๐ Table of Contents
- โจ Features
- ๐ฆ Installation
- ๐ Quick Start
- ๐ Documentation
- ๐ฏ Usage Examples
- ๐ฅ๏ธ Command Line Interface
- ๐๏ธ Advanced Usage
- ๐ง Configuration
- ๐ค Contributing
- ๐ License
โจ Features
| Feature | Description | Icon |
|---|---|---|
| INI Support | Full INI file configuration management | ๐ |
| JSON Support | JSON configuration with attribute-based access | ๐๏ธ |
| Auto Type Conversion | Automatic string to bool/int/float conversion | ๐ |
| List Parsing | Parse comma-separated, newline-separated, and JSON arrays | ๐ |
| Dictionary Parsing | Parse key:value pairs and JSON objects | ๐๏ธ |
| Class-based Interface | Metaclass-powered configuration classes | ๐๏ธ |
| CLI Interface | Command-line tool for configuration management | ๐ป |
| Search Functionality | Find sections and options with case sensitivity control | ๐ |
| Pretty Printing | Enhanced output with optional color support | ๐จ |
| Python 2/3 Compatible | Works with both Python 2.7+ and Python 3.6+ | ๐ |
๐ฆ Installation
๐ Basic Installation
# Install from PyPI
pip install configset
# Or clone the repository
git clone https://github.com/cumulus13/configset.git
cd configset
# Install the package in editable/development mode
pip install -e .
# Or install the latest development version directly from GitHub
pip install git+https://github.com/cumulus13/configset.git
๐จ With Optional Dependencies (Recommended)
# Install with enhanced output support
pip install -e .[full]
# Or install dependencies manually
pip install rich jsoncolor make-colors
๐ Requirements
- Python 2.7+ or Python 3.6+
- Optional: rich, jsoncolor, make-colors for enhanced output
๐ Quick Start
๐ Basic INI Configuration
from configset import ConfigSet
# or from configset import configset
# Create configuration instance
config = ConfigSet('myapp.ini')
# or config = configset('myapp.ini')
# Write configuration
config.write_config('database', 'host', 'localhost')
config.write_config('database', 'port', 5432)
config.write_config('database', 'ssl', True)
# Read configuration (with automatic type conversion)
host = config.get_config('database', 'host') # Returns: 'localhost'
port = config.get_config('database', 'port') # Returns: 5432 (int)
ssl = config.get_config('database', 'ssl') # Returns: True (bool)
๐๏ธ Class-based Configuration
from configset import CONFIG
class AppConfig(CONFIG):
CONFIGFILE = 'myapp.ini'
# Use as class methods
AppConfig.write_config('api', 'endpoint', 'https://api.example.com')
endpoint = AppConfig.get_config('api', 'endpoint')
# JSON-style attribute access
config = AppConfig()
config.api_key = 'secret123'
config.debug_mode = True
print(f"API Key: {config.api_key}") # Output: API Key: secret123
๐ Documentation
๐ง ConfigSet Class
The main class for INI file configuration management.
๐ Constructor
ConfigSet(config_file='', auto_write=True, **kwargs)
| Parameter | Type | Default | Description |
|---|---|---|---|
| config_file | str | '' | Path to configuration file |
| auto_write | bool | True | Auto-create missing files/sections |
| **kwargs | dict | {} | Additional ConfigParser arguments |
๐ Key Methods
| Method | Description | Return Type |
|---|---|---|
| get_config(section, option, default=None) | Get config value with type conversion | Any |
| write_config(section, option, value) | Write config value to file | Any |
| remove_config(section, option=None) | Remove section or specific option | bool |
| get_config_as_list(section, option, default=None) | Parse value as list | List[Any] |
| get_config_as_dict(section, option, default=None) | Parse value as dictionary | Dict[str, Any] |
| find(query, case_sensitive=True, verbose=False) | Search sections/options | bool |
| print_all_config(sections=None) | Print all configuration | List[Tuple] |
๐ฏ Usage Examples
๐ List Configuration
config = ConfigSet('servers.ini')
# Write list data (multiple formats supported)
config.write_config('cluster', 'servers', 'server1.com, server2.com, server3.com')
config.write_config('cluster', 'ports', '[8080, 8081, 8082]')
config.write_config('cluster', 'features', '''
load_balancer
ssl_support
monitoring
''')
# Read as lists (automatic parsing)
servers = config.get_config_as_list('cluster', 'servers')
# Returns: ['server1.com', 'server2.com', 'server3.com']
ports = config.get_config_as_list('cluster', 'ports')
# Returns: [8080, 8081, 8082]
features = config.get_config_as_list('cluster', 'features')
# Returns: ['load_balancer', 'ssl_support', 'monitoring']
๐๏ธ Dictionary Configuration
config = ConfigSet('settings.ini')
# Write dictionary data
config.write_config('limits', 'quotas', 'users:1000, files:5000, bandwidth:100')
config.write_config('features', 'enabled', '{"auth": true, "cache": false, "debug": true}')
# Read as dictionaries
quotas = config.get_config_as_dict('limits', 'quotas')
# Returns: {'users': 1000, 'files': 5000, 'bandwidth': 100}
features = config.get_config_as_dict('features', 'enabled')
# Returns: {'auth': True, 'cache': False, 'debug': True}
๐ Search and Remove
config = ConfigSet('myapp.ini')
# Search for sections/options
found = config.find('database') # Returns: True if found
config.find('host', verbose=True) # Print found items
# Remove operations
config.remove_config('old_section') # Remove entire section
config.remove_config('database', 'old_option') # Remove specific option
๐ฅ๏ธ Command Line Interface
The ConfigSet library includes a powerful CLI for configuration management.
๐ Basic Commands
# Show help
python -m configset --help
# Read all configuration
python -m configset myapp.ini --all
# Read specific value
python -m configset myapp.ini --read --section database --option host
# Write configuration
python -m configset myapp.ini --write --section database --option host --value localhost
# Remove section
python -m configset myapp.ini --delete --section old_section
# Remove specific option
python -m configset myapp.ini --delete --section database --option password
๐๏ธ Advanced CLI Usage
# Parse as list
python -m configset myapp.ini --read --section cluster --option servers --list
# Parse as dictionary
python -m configset myapp.ini --read --section limits --option quotas --dict
# Enable debug mode
DEBUG=1 python -m configset myapp.ini --all
๐๏ธ Advanced Usage
๐จ Custom Configuration Classes
from configset import CONFIG, ConfigSet
class DatabaseConfig(CONFIG):
CONFIGFILE = 'database.ini'
@classmethod
def get_connection_string(cls):
host = cls.get_config('database', 'host')
port = cls.get_config('database', 'port')
db = cls.get_config('database', 'name')
return f"postgresql://{host}:{port}/{db}"
class APIConfig(CONFIG):
CONFIGFILE = 'api.ini'
def __init__(self):
super().__init__()
# Set default values
if not hasattr(self, 'timeout'):
self.timeout = 30
if not hasattr(self, 'retries'):
self.retries = 3
# Usage
db_config = DatabaseConfig()
connection = db_config.get_connection_string()
api_config = APIConfig()
api_config.endpoint = 'https://api.example.com'
print(f"Timeout: {api_config.timeout}") # Output: Timeout: 30
๐ Configuration Migration
def migrate_config_v1_to_v2(old_config_file, new_config_file):
"""Migrate configuration from v1 to v2 format."""
old_config = ConfigSet(old_config_file)
new_config = ConfigSet(new_config_file)
# Copy all sections and options
for section_name, section_data in old_config.get_all_config():
for option, value in section_data.items():
# Apply any transformations needed
if section_name == 'database' and option == 'ssl':
value = 'enabled' if value else 'disabled'
new_config.write_config(section_name, option, value)
print(f"โ
Migration completed: {old_config_file} โ {new_config_file}")
# Usage
migrate_config_v1_to_v2('old_app.ini', 'new_app.ini')
๐งช Configuration Validation
from configset import ConfigSet
class ValidatedConfig(ConfigSet):
"""Configuration with validation rules."""
REQUIRED_SECTIONS = ['database', 'api', 'logging']
REQUIRED_OPTIONS = {
'database': ['host', 'port', 'name'],
'api': ['endpoint', 'key'],
'logging': ['level', 'file']
}
def validate(self):
"""Validate configuration against rules."""
errors = []
# Check required sections
for section in self.REQUIRED_SECTIONS:
if not self.has_section(section):
errors.append(f"โ Missing required section: [{section}]")
continue
# Check required options
required_opts = self.REQUIRED_OPTIONS.get(section, [])
for option in required_opts:
if not self.has_option(section, option):
errors.append(f"โ Missing required option: [{section}] {option}")
if errors:
print("\n".join(errors))
return False
print("โ
Configuration validation passed!")
return True
# Usage
config = ValidatedConfig('validated_app.ini')
if config.validate():
# Proceed with application
pass
๐ง Configuration
๐ Environment Variables
| Variable | Description | Values |
|---|---|---|
| DEBUG | Enable debug output | 1, true, yes |
| DEBUG_SERVER | Enable server debug mode | 1, true, yes |
| SHOW_CONFIGNAME | Show config file path | 1, true, yes |
๐จ Optional Dependencies
# Enhanced output with colors and formatting
pip install rich # Rich text and tables
pip install jsoncolor # JSON syntax highlighting
pip install make-colors # Terminal color support
๐ Migration Guide
๐ From v1.x to v2.x
Breaking Changes:
- Removed redundant methods (read_config2, read_config3, etc.)
- Simplified method signatures
- Enhanced type conversion
Migration Steps:
# Old v1.x code
config.read_config2('section', 'option')
# New v2.x code
config.get_config_as_list('section', 'option')
๐ง Configuration File Format
INI Format Example:
[database]
host = localhost
port = 5432
ssl = true
features = load_balancer, ssl_support, monitoring
[api]
endpoint = https://api.example.com
timeout = 30
headers = {"Content-Type": "application/json", "Accept": "application/json"}
JSON Format Example:
{
"database_host": "localhost",
"database_port": 5432,
"api_key": "secret123",
"debug_mode": true,
"feature_flags": ["auth", "cache", "monitoring"]
}
๐งช Testing
๐ Run Tests
# Install test dependencies
pip install pytest pytest-cov
# Run all tests
pytest tests/
# Run with coverage
pytest --cov=configset tests/
# Run specific test file
pytest tests/test_configset.py -v
๐ Test Example
import pytest
from configset import ConfigSet
import tempfile
import os
def test_config_basic_operations():
"""Test basic configuration operations."""
with tempfile.NamedTemporaryFile(suffix='.ini', delete=False) as f:
config_file = f.name
try:
config = ConfigSet(config_file)
# Test write and read
config.write_config('test', 'key', 'value')
assert config.get_config('test', 'key') == 'value'
# Test type conversion
config.write_config('test', 'number', '42')
assert config.get_config('test', 'number') == 42
assert isinstance(config.get_config('test', 'number'), int)
# Test boolean
config.write_config('test', 'flag', 'true')
assert config.get_config('test', 'flag') is True
finally:
os.unlink(config_file)
๐ค Contributing
We welcome contributions! ๐
๐ Development Setup
# Clone repository
git clone https://github.com/cumulus13/configset.git
cd configset
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install development dependencies
pip install -e .[dev]
pip install pytest pytest-cov black isort mypy
๐ง Code Standards
# Format code
black configset/
isort configset/
# Type checking
mypy configset/
# Run tests
pytest tests/ --cov=configset
๐ Contributing Guidelines
- ๐ด Fork the repository
- ๐ Create a feature branch (
git checkout -b feature/amazing-feature) - โ Add tests for your changes
- ๐ Update documentation if needed
- ๐ฏ Commit your changes (
git commit -m 'Add amazing feature') - ๐ค Push to the branch (
git push origin feature/amazing-feature) - ๐ Open a Pull Request
๐ Changelog
๐ v1.56 (Latest)
- โจ New Features:
- Enhanced type conversion system
- List and dictionary parsing
- Class-based configuration interface
- Improved CLI with delete operations
- Search functionality
- Pretty printing with colors
- ๐ง Improvements:
- Better error handling
- Python 2/3 compatibility
- Comprehensive documentation
- Unit tests coverage
- Type hints support
- ๐๏ธ Removed:
- Deprecated methods (read_config2, read_config3, etc.)
- Redundant functionality
๐ v1.x (Legacy)
- Basic INI file support
- Simple read/write operations
- Limited type conversion
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 Hadi Cahyadi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
๐ Acknowledgments
- ๐ Python ConfigParser - Foundation for INI file handling
- ๐จ Rich Library - Enhanced terminal output
- ๐ Make Colors - Terminal color support
- ๐ฏ JSONColor - JSON syntax highlighting
- ๐ฅ Contributors - Thank you to all contributors!
๐ Support & Contact
- ๐ง Email: cumulus13@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Wiki
โญ If you find ConfigSet useful, please consider giving it a star! โญ
Made with โค๏ธ by developers, for developers.
Support
- Python 2.7+, 3.x+
- Windows, Linux, Mac
Author
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file configset-1.86.tar.gz.
File metadata
- Download URL: configset-1.86.tar.gz
- Upload date:
- Size: 63.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bad7eb730932851ccd24cf2c8ffadf1d451ea47f06f203b36eb5d22633b82e2a
|
|
| MD5 |
6fa8df75e0be0d961ab748746f9a2ac9
|
|
| BLAKE2b-256 |
630fe11b312df34441eeff31d85e69b59251eb7b075d285a1fc50c5e44b92dfd
|
File details
Details for the file configset-1.86-py2.py3-none-any.whl.
File metadata
- Download URL: configset-1.86-py2.py3-none-any.whl
- Upload date:
- Size: 55.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cd96ae2cac5d2c3590354c77df4771e664b2ce4ff7abd8920b646ea07d54b64
|
|
| MD5 |
7e4899dcfa6e75713065a77afbb8d885
|
|
| BLAKE2b-256 |
595bce29fb36d405647bd4b256d880b7c79c1610870e2d7e1b1c25066d93637b
|