tbi-config-manager


Nametbi-config-manager JSON
Version 0.0.8 PyPI version JSON
download
home_pageNone
SummaryA flexible configuration management library for Python with multiple provider support (Environment, GCP Secret Manager, etc.) and easy extensibility.
upload_time2024-12-31 06:59:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords config dotenv env environment gcp secret-manager variables
VCS
bugtrack_url
requirements google-api-core google-cloud-secret-manager python-dotenv
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Config Manager

### tbi-config-manager (0.0.8)
A flexible configuration management library for Python with multiple provider support (Environment, GCP Secret Manager, etc.) and easy extensibility.


# Features

- **Multiple Provider Support**
  - Built-in Environment Variables (.env) support
  - Built-in GCP Secret Manager integration
  - Configurable provider priority
  - Easy to extend with custom providers

- **Type Safety & Flexibility**
  - Strong typing support
  - Optional value handling
  - Default value support
  - Attribute-style access

- **Developer Friendly**
  - Simple API
  - Clear error messages
  - Comprehensive logging
  - Easy to test


# Installation

```bash
$ pip install tbi-config-manager
```
### Prerequisites
- Python >=3.8
- GCP credentials (optional, GCP project Service Account, for Secret Manager support)


# How to Use

Basic usage example:
```python
from config_manager import ConfigManager, Provider

# Default to environment variables only
config = ConfigManager()
value = config.get("DATABASE_URL")

# With specific .env file
config = ConfigManager(env_file=".env.dev")

# ENV has higher priority than GCP.
config = ConfigManager(
    providers=[Provider.ENV, Provider.GCP],  
    project_id="your-project-id",
    credentials_path="path/to/credentials.json",
    secret_prefix="APP"  # Will use "APP_" as prefix
)

# GCP has higher priority than ENV.
config = ConfigManager(
    providers=[Provider.GCP, Provider.ENV]  
    ...
)

# Only use GCP Secret Manager
config = ConfigManager(
    providers=[Provider.GCP]  
    ...
)

DATABASE_URL = config.get("DATABASE_URL")
...
```

In your `config.py`:
```python
from config_manager import ConfigManager, Provider

config = ConfigManager(
    providers=[Provider.ENV, Provider.GCP],  
    project_id="config-manager",
    credentials_path="path/to/credentials.json",
    secret_prefix="PROD"  # Will use "PROD_" as prefix
)

# Access configs as attributes
DATABASE_URL = config.get("DATABASE_URL")
API_KEY = config.get("API_KEY")
ENV = config.get("ENV", default="dev")
PROVIDER = config.get("PROVIDER", "GCP")  # Optional parameter with default
```


# How to Contribute

## Setup
```bash
git clone https://github.com/TaiwanBigdata/config-manager.git
cd readgen
python -m venv env
source env/bin/activate  # Linux/Mac
pip install -e .
```

## Implementing Custom Providers

The config-manager library is designed for extensibility. Here's how to implement your own configuration provider:

### 1. Add New Provider Type
Add your provider type to the enum
`src/config_manager/typing.py`
```python
from enum import Enum

class Provider(Enum):
    """Available configuration provider types"""
    ENV = "env"      # Environment variables
    GCP = "gcp"      # Google Cloud Secret Manager
    REDIS = "redis"  # Your new provider
```

### 2. Implement Provider Class
Create your provider implementation under providers directory 
`src/config_manager/providers/redis.py`

```python
from config_manager import ConfigProvider
from typing import Optional

# Inherit from the base abstract class ConfigProvider
class RedisProvider(ConfigProvider):
    def __init__(self, redis_url: str, prefix: str = ""):
        self.redis_url = redis_url
        self.prefix = prefix
        self._client = redis.Redis.from_url(redis_url)
    
    # Required interface implementations
    def get(self, key: str) -> Optional[str]: ...
    def has(self, key: str) -> bool: ...
    def reload(self) -> None: ...
    @property
    def name(self) -> str: ...
```
### 3. Register Provider
Register your provider in factory
`src/config_manager/providers/factory.py`

```python
class ProviderFactory:
    ...

# Register providers here
ProviderFactory.register(Provider.ENV, EnvProvider)
ProviderFactory.register(Provider.GCP, GCPProvider)
```

### 4. Export Provider

Export your provider in the module level
`src/config_manager/providers/__init__.py`
```python
from .redis import RedisProvider

__all__ = [
    ...
    "RedisProvider",
]
```

Export your provider in the package level
`src/config_manager/__init__.py`
```python
from .providers import ProviderFactory, EnvProvider, GCPProvider, RedisProvider

__all__ = [
    ...
    "RedisProvider",
]
```


### Examples of Potential Custom Providers
- AWS Secrets Manager
- Azure Key Vault
- HashiCorp Vault
- Redis
- MongoDB
- Local JSON/YAML files
- Remote HTTP endpoints

### Each provider needs to implement the ConfigProvider interface and handle:

- Configuration retrieval
- Key existence checking
- Reloading mechanism
- Error handling


# Dependencies

### Core
- python-dotenv>=1.0.0
- google-cloud-secret-manager>=2.0.0
- typing-extensions>=4.0.0


# License

This project is licensed under the MIT License.


# Project Structure

```
config-manager/
├── src/
│   └── config_manager/       # Register providers at the project level
│       ├── providers/        # Register providers at the model level
│       │   ├── base.py       # Abstract base class for configuration providers
│       │   ├── composite.py  # Implements composite pattern to aggregate multiple config providers in priority order
│       │   ├── env.py        # Local environment configuration for the `.env` provider
│       │   ├── factory.py    # Register and initialize providers in this file
│       │   └── gcp.py        # GCP Secret Manager provider for accessing and managing configuration secrets in Google Cloud Platform
│       ├── utils/
│       │   ├── cache.py
│       │   └── logger.py
│       ├── config.py         # Configuration manager that orchestrates multiple providers to retrieve config values
│       ├── exceptions.py
│       └── typing.py         # Type definitions and enums
├── LICENSE
├── pyproject.toml
├── readgen.toml              # ReadGen - Readme Generator CLI tool configuration file
├── README.md
└── requirements.txt
```


---
> This document was automatically generated by [ReadGen](https://github.com/TaiwanBigdata/readgen).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tbi-config-manager",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "config, dotenv, env, environment, gcp, secret-manager, variables",
    "author": null,
    "author_email": "Albert Liu <dreamerhyde@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/26/8d/2a070e9a358920a237c0bc02a9d0aeadc36a06ac5ea6b572e981a96f5205/tbi_config_manager-0.0.8.tar.gz",
    "platform": null,
    "description": "# Config Manager\n\n### tbi-config-manager (0.0.8)\nA flexible configuration management library for Python with multiple provider support (Environment, GCP Secret Manager, etc.) and easy extensibility.\n\n\n# Features\n\n- **Multiple Provider Support**\n  - Built-in Environment Variables (.env) support\n  - Built-in GCP Secret Manager integration\n  - Configurable provider priority\n  - Easy to extend with custom providers\n\n- **Type Safety & Flexibility**\n  - Strong typing support\n  - Optional value handling\n  - Default value support\n  - Attribute-style access\n\n- **Developer Friendly**\n  - Simple API\n  - Clear error messages\n  - Comprehensive logging\n  - Easy to test\n\n\n# Installation\n\n```bash\n$ pip install tbi-config-manager\n```\n### Prerequisites\n- Python >=3.8\n- GCP credentials (optional, GCP project Service Account, for Secret Manager support)\n\n\n# How to Use\n\nBasic usage example:\n```python\nfrom config_manager import ConfigManager, Provider\n\n# Default to environment variables only\nconfig = ConfigManager()\nvalue = config.get(\"DATABASE_URL\")\n\n# With specific .env file\nconfig = ConfigManager(env_file=\".env.dev\")\n\n# ENV has higher priority than GCP.\nconfig = ConfigManager(\n    providers=[Provider.ENV, Provider.GCP],  \n    project_id=\"your-project-id\",\n    credentials_path=\"path/to/credentials.json\",\n    secret_prefix=\"APP\"  # Will use \"APP_\" as prefix\n)\n\n# GCP has higher priority than ENV.\nconfig = ConfigManager(\n    providers=[Provider.GCP, Provider.ENV]  \n    ...\n)\n\n# Only use GCP Secret Manager\nconfig = ConfigManager(\n    providers=[Provider.GCP]  \n    ...\n)\n\nDATABASE_URL = config.get(\"DATABASE_URL\")\n...\n```\n\nIn your `config.py`:\n```python\nfrom config_manager import ConfigManager, Provider\n\nconfig = ConfigManager(\n    providers=[Provider.ENV, Provider.GCP],  \n    project_id=\"config-manager\",\n    credentials_path=\"path/to/credentials.json\",\n    secret_prefix=\"PROD\"  # Will use \"PROD_\" as prefix\n)\n\n# Access configs as attributes\nDATABASE_URL = config.get(\"DATABASE_URL\")\nAPI_KEY = config.get(\"API_KEY\")\nENV = config.get(\"ENV\", default=\"dev\")\nPROVIDER = config.get(\"PROVIDER\", \"GCP\")  # Optional parameter with default\n```\n\n\n# How to Contribute\n\n## Setup\n```bash\ngit clone https://github.com/TaiwanBigdata/config-manager.git\ncd readgen\npython -m venv env\nsource env/bin/activate  # Linux/Mac\npip install -e .\n```\n\n## Implementing Custom Providers\n\nThe config-manager library is designed for extensibility. Here's how to implement your own configuration provider:\n\n### 1. Add New Provider Type\nAdd your provider type to the enum\n`src/config_manager/typing.py`\n```python\nfrom enum import Enum\n\nclass Provider(Enum):\n    \"\"\"Available configuration provider types\"\"\"\n    ENV = \"env\"      # Environment variables\n    GCP = \"gcp\"      # Google Cloud Secret Manager\n    REDIS = \"redis\"  # Your new provider\n```\n\n### 2. Implement Provider Class\nCreate your provider implementation under providers directory \n`src/config_manager/providers/redis.py`\n\n```python\nfrom config_manager import ConfigProvider\nfrom typing import Optional\n\n# Inherit from the base abstract class ConfigProvider\nclass RedisProvider(ConfigProvider):\n    def __init__(self, redis_url: str, prefix: str = \"\"):\n        self.redis_url = redis_url\n        self.prefix = prefix\n        self._client = redis.Redis.from_url(redis_url)\n    \n    # Required interface implementations\n    def get(self, key: str) -> Optional[str]: ...\n    def has(self, key: str) -> bool: ...\n    def reload(self) -> None: ...\n    @property\n    def name(self) -> str: ...\n```\n### 3. Register Provider\nRegister your provider in factory\n`src/config_manager/providers/factory.py`\n\n```python\nclass ProviderFactory:\n    ...\n\n# Register providers here\nProviderFactory.register(Provider.ENV, EnvProvider)\nProviderFactory.register(Provider.GCP, GCPProvider)\n```\n\n### 4. Export Provider\n\nExport your provider in the module level\n`src/config_manager/providers/__init__.py`\n```python\nfrom .redis import RedisProvider\n\n__all__ = [\n    ...\n    \"RedisProvider\",\n]\n```\n\nExport your provider in the package level\n`src/config_manager/__init__.py`\n```python\nfrom .providers import ProviderFactory, EnvProvider, GCPProvider, RedisProvider\n\n__all__ = [\n    ...\n    \"RedisProvider\",\n]\n```\n\n\n### Examples of Potential Custom Providers\n- AWS Secrets Manager\n- Azure Key Vault\n- HashiCorp Vault\n- Redis\n- MongoDB\n- Local JSON/YAML files\n- Remote HTTP endpoints\n\n### Each provider needs to implement the ConfigProvider interface and handle:\n\n- Configuration retrieval\n- Key existence checking\n- Reloading mechanism\n- Error handling\n\n\n# Dependencies\n\n### Core\n- python-dotenv>=1.0.0\n- google-cloud-secret-manager>=2.0.0\n- typing-extensions>=4.0.0\n\n\n# License\n\nThis project is licensed under the MIT License.\n\n\n# Project Structure\n\n```\nconfig-manager/\n\u251c\u2500\u2500 src/\n\u2502   \u2514\u2500\u2500 config_manager/       # Register providers at the project level\n\u2502       \u251c\u2500\u2500 providers/        # Register providers at the model level\n\u2502       \u2502   \u251c\u2500\u2500 base.py       # Abstract base class for configuration providers\n\u2502       \u2502   \u251c\u2500\u2500 composite.py  # Implements composite pattern to aggregate multiple config providers in priority order\n\u2502       \u2502   \u251c\u2500\u2500 env.py        # Local environment configuration for the `.env` provider\n\u2502       \u2502   \u251c\u2500\u2500 factory.py    # Register and initialize providers in this file\n\u2502       \u2502   \u2514\u2500\u2500 gcp.py        # GCP Secret Manager provider for accessing and managing configuration secrets in Google Cloud Platform\n\u2502       \u251c\u2500\u2500 utils/\n\u2502       \u2502   \u251c\u2500\u2500 cache.py\n\u2502       \u2502   \u2514\u2500\u2500 logger.py\n\u2502       \u251c\u2500\u2500 config.py         # Configuration manager that orchestrates multiple providers to retrieve config values\n\u2502       \u251c\u2500\u2500 exceptions.py\n\u2502       \u2514\u2500\u2500 typing.py         # Type definitions and enums\n\u251c\u2500\u2500 LICENSE\n\u251c\u2500\u2500 pyproject.toml\n\u251c\u2500\u2500 readgen.toml              # ReadGen - Readme Generator CLI tool configuration file\n\u251c\u2500\u2500 README.md\n\u2514\u2500\u2500 requirements.txt\n```\n\n\n---\n> This document was automatically generated by [ReadGen](https://github.com/TaiwanBigdata/readgen).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A flexible configuration management library for Python with multiple provider support (Environment, GCP Secret Manager, etc.) and easy extensibility.",
    "version": "0.0.8",
    "project_urls": {
        "Homepage": "https://github.com/TaiwanBigdata/config-manager",
        "Repository": "https://github.com/TaiwanBigdata/config-manager.git"
    },
    "split_keywords": [
        "config",
        " dotenv",
        " env",
        " environment",
        " gcp",
        " secret-manager",
        " variables"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50a004fd59f86cb8fa035d866d777b0b5e3a8e9bd1e3962418da4c07f54c7189",
                "md5": "0cbcaf4f3022a30d1190d1ad3454569f",
                "sha256": "cbf440ec61acd23fb0e9c5356e89fcf2e56e63e787aef5e957c9e9281c8c03c2"
            },
            "downloads": -1,
            "filename": "tbi_config_manager-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0cbcaf4f3022a30d1190d1ad3454569f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 13055,
            "upload_time": "2024-12-31T06:59:13",
            "upload_time_iso_8601": "2024-12-31T06:59:13.355476Z",
            "url": "https://files.pythonhosted.org/packages/50/a0/04fd59f86cb8fa035d866d777b0b5e3a8e9bd1e3962418da4c07f54c7189/tbi_config_manager-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "268d2a070e9a358920a237c0bc02a9d0aeadc36a06ac5ea6b572e981a96f5205",
                "md5": "ca2f774cc5f8bffaca9d811595459936",
                "sha256": "66580790bc472f842f790bef95336804499575bae46325491d3109a0584e79e7"
            },
            "downloads": -1,
            "filename": "tbi_config_manager-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "ca2f774cc5f8bffaca9d811595459936",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10480,
            "upload_time": "2024-12-31T06:59:15",
            "upload_time_iso_8601": "2024-12-31T06:59:15.761884Z",
            "url": "https://files.pythonhosted.org/packages/26/8d/2a070e9a358920a237c0bc02a9d0aeadc36a06ac5ea6b572e981a96f5205/tbi_config_manager-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-31 06:59:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TaiwanBigdata",
    "github_project": "config-manager",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "google-api-core",
            "specs": [
                [
                    "==",
                    "2.23.0"
                ]
            ]
        },
        {
            "name": "google-cloud-secret-manager",
            "specs": [
                [
                    "==",
                    "2.21.1"
                ]
            ]
        },
        {
            "name": "python-dotenv",
            "specs": [
                [
                    "==",
                    "1.0.1"
                ]
            ]
        }
    ],
    "lcname": "tbi-config-manager"
}
        
Elapsed time: 5.62316s