tbi-config-manager


Nametbi-config-manager JSON
Version 0.0.3 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-14 09:50:33
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
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.3)
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_"
)

# 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_"
)

# 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/
├── .gitignore
├── LICENSE
├── README.md
├── pyproject.toml
├── readgen.toml              # ReadGen - Readme Generator CLI tool configuration file
├── requirements.txt
└── src/
    └── config_manager/       # Register providers at the project level
        ├── config.py         # Configuration manager that orchestrates multiple providers to retrieve config values
        ├── exceptions.py
        ├── 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        # The Provider class
        ├── typing.py         # Type definitions and enums
        └── utils/
            ├── cache.py
            └── logger.py
```


---
> 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/05/13/e2c65dc707f22c848e9c37cffef7498480fb888d4b41c960fe54ff1286cb/tbi_config_manager-0.0.3.tar.gz",
    "platform": null,
    "description": "# Config Manager\n\n### tbi-config-manager (0.0.3)\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_\"\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_\"\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 .gitignore\n\u251c\u2500\u2500 LICENSE\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 pyproject.toml\n\u251c\u2500\u2500 readgen.toml              # ReadGen - Readme Generator CLI tool configuration file\n\u251c\u2500\u2500 requirements.txt\n\u2514\u2500\u2500 src/\n    \u2514\u2500\u2500 config_manager/       # Register providers at the project level\n        \u251c\u2500\u2500 config.py         # Configuration manager that orchestrates multiple providers to retrieve config values\n        \u251c\u2500\u2500 exceptions.py\n        \u251c\u2500\u2500 providers/        # Register providers at the model level\n        \u2502   \u251c\u2500\u2500 base.py       # Abstract base class for configuration providers\n        \u2502   \u251c\u2500\u2500 composite.py  # Implements composite pattern to aggregate multiple config providers in priority order\n        \u2502   \u251c\u2500\u2500 env.py        # Local environment configuration for the `.env` provider\n        \u2502   \u251c\u2500\u2500 factory.py    # Register and initialize providers in this file\n        \u2502   \u2514\u2500\u2500 gcp.py        # The Provider class\n        \u251c\u2500\u2500 typing.py         # Type definitions and enums\n        \u2514\u2500\u2500 utils/\n            \u251c\u2500\u2500 cache.py\n            \u2514\u2500\u2500 logger.py\n```\n\n\n---\n> This document was automatically generated by [ReadGen](https://github.com/TaiwanBigdata/readgen).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A flexible configuration management library for Python with multiple provider support (Environment, GCP Secret Manager, etc.) and easy extensibility.",
    "version": "0.0.3",
    "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": "26e4e8813d41c097ecf1ef862db429998d1e1026ea559d22932483a37a661e58",
                "md5": "18b58bfd3c5fcd59f04e965394b2e3d6",
                "sha256": "14dcbab877fc85281c04bb59c06e390d64c35df554db8c96a3ab4aa6a5060280"
            },
            "downloads": -1,
            "filename": "tbi_config_manager-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "18b58bfd3c5fcd59f04e965394b2e3d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 11526,
            "upload_time": "2024-12-14T09:50:30",
            "upload_time_iso_8601": "2024-12-14T09:50:30.604749Z",
            "url": "https://files.pythonhosted.org/packages/26/e4/e8813d41c097ecf1ef862db429998d1e1026ea559d22932483a37a661e58/tbi_config_manager-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0513e2c65dc707f22c848e9c37cffef7498480fb888d4b41c960fe54ff1286cb",
                "md5": "0389ee659bacc91b034b52fab8293e5b",
                "sha256": "1d2a2fbac5280e50673473e5da1a3e8825508ce8ebaac3b6cb1bca97dbc4fa10"
            },
            "downloads": -1,
            "filename": "tbi_config_manager-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "0389ee659bacc91b034b52fab8293e5b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 8985,
            "upload_time": "2024-12-14T09:50:33",
            "upload_time_iso_8601": "2024-12-14T09:50:33.134881Z",
            "url": "https://files.pythonhosted.org/packages/05/13/e2c65dc707f22c848e9c37cffef7498480fb888d4b41c960fe54ff1286cb/tbi_config_manager-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-14 09:50:33",
    "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: 0.40862s