# Confstar
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-360/)
## What is this library?
Confstar is a config loader (*similar to django settings loader*) with ✨**magic-annotations**✨ handlers. <br> Also, Confstar is a part of [pie-audio](https://github.com/uselessvevo/pie-audio) project. I just wanted to make an independent repository for it.
## What are ✨magic-annotations✨?
Basically, it's micro-handlers that let's you to control field behaviour via [type annotations](https://peps.python.org/pep-0484/).
For example, you can specify how many elements list must have, lock the field to prevent it from editing and etc.
## How to use confstar?
It's really that simple:
* Import `Config` instance of `ConfLoader` (*or define your own*)
* Import it wherever you want
## Configuration module
```py
# configs/consts.py
from confstar.types import *
PUBLIC_STRING_VALUE = "String Value"
PRIVATE_INT_FIELD: Lock = 100
PUBLIC_MIN_FIELD: Min[3] = [1, 2]
PUBLIC_MAX_FIELD: Max[3] = [1, 2, 3]
PUBLIC_RANGE_FIELD: Range[1, 5] = 2
```
## Your application
```py
from confstar.loader import Config, ConfLoader
Config.add_handlers(Lock, Min, Max)
Config.import_module("configs.config")
# Or load configuration module by using relative file path
MyConfig = ConfLoader()
MyConfig.load_by_path("configs/myconfig.py")
# You can freely overwrite this field
# because it has no handlers attached to it
Config.PUBLIC_STRING_VALUE = "New string value!"
# Will throw an error
Config.PRIVATE_INT_FIELD = 321
Config.PUBLIC_MIN_FIELD = [1, 2, 3, 4]
Config.PUBLIC_MAX_FIELD.extend([4, 5, 6])
Config.PUBLIC_RANGE_FIELD = 6
```
## Writing your own handler
Of course, we have built-in magic-annotations, but if you want to write your own, it's really that easy:
1. Define your own handler
```py
from __future__ import annotations
from typing import Any, Type
from confstar import AnnotatedHandler
class MagicHandler(AnnotatedHandler):
def set(self, field: str, value: Any) -> Any:
"""
Set or throw and error if validation fail
"""
if not value % 2 == 0:
raise ValueError(f"An error has been occurred in {self.__class__.__name__}")
self._attributes[field] = value
def get(self, field: str) -> Any:
"""
Return field from `_attributes`
"""
return self._attributes[field] # or via `dict.get` method
def __class_getitem__(cls, value: Any) -> Type[MagicHandler]:
"""
Provide the type annotation logic
Field: HandlerType[<values>]
"""
```
2. Define alias to ignore linter (*which is optional*)
```py
Magic = type("Magic", (MagicHandler,), {})
```
3. Apply your handler on some field
```py
from my_handlers import Magic
MAGIC_FIELD: Magic = ...
```
Raw data
{
"_id": null,
"home_page": "",
"name": "confstar",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "configuration,config,settings,handlers",
"author": "",
"author_email": "Ivanov Cyril <jcommander13@yandex.ru>",
"download_url": "https://files.pythonhosted.org/packages/cb/45/ac31d36b39d13e35ed10fea3a971e1c1f56f466cf87d0a2e1d28c0b02312/confstar-1.0.0.tar.gz",
"platform": null,
"description": "# Confstar\r\n[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-360/)\r\n[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-360/)\r\n[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-360/)\r\n[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-360/)\r\n\r\n\r\n## What is this library?\r\n\r\nConfstar is a config loader (*similar to django settings loader*) with \u2728**magic-annotations**\u2728 handlers. <br> Also, Confstar is a part of [pie-audio](https://github.com/uselessvevo/pie-audio) project. I just wanted to make an independent repository for it.\r\n\r\n## What are \u2728magic-annotations\u2728?\r\nBasically, it's micro-handlers that let's you to control field behaviour via [type annotations](https://peps.python.org/pep-0484/).\r\n\r\nFor example, you can specify how many elements list must have, lock the field to prevent it from editing and etc.\r\n\r\n## How to use confstar?\r\n\r\nIt's really that simple:\r\n* Import `Config` instance of `ConfLoader` (*or define your own*)\r\n* Import it wherever you want\r\n\r\n\r\n## Configuration module\r\n```py\r\n# configs/consts.py\r\nfrom confstar.types import *\r\n\r\nPUBLIC_STRING_VALUE = \"String Value\"\r\nPRIVATE_INT_FIELD: Lock = 100\r\nPUBLIC_MIN_FIELD: Min[3] = [1, 2]\r\nPUBLIC_MAX_FIELD: Max[3] = [1, 2, 3]\r\nPUBLIC_RANGE_FIELD: Range[1, 5] = 2\r\n```\r\n\r\n## Your application\r\n```py\r\nfrom confstar.loader import Config, ConfLoader\r\n\r\n\r\nConfig.add_handlers(Lock, Min, Max)\r\nConfig.import_module(\"configs.config\")\r\n\r\n# Or load configuration module by using relative file path\r\nMyConfig = ConfLoader()\r\nMyConfig.load_by_path(\"configs/myconfig.py\")\r\n\r\n# You can freely overwrite this field\r\n# because it has no handlers attached to it\r\nConfig.PUBLIC_STRING_VALUE = \"New string value!\"\r\n\r\n# Will throw an error\r\nConfig.PRIVATE_INT_FIELD = 321\r\nConfig.PUBLIC_MIN_FIELD = [1, 2, 3, 4]\r\nConfig.PUBLIC_MAX_FIELD.extend([4, 5, 6])\r\nConfig.PUBLIC_RANGE_FIELD = 6\r\n```\r\n\r\n## Writing your own handler\r\n\r\nOf course, we have built-in magic-annotations, but if you want to write your own, it's really that easy:\r\n\r\n1. Define your own handler\r\n\r\n```py\r\nfrom __future__ import annotations\r\n\r\nfrom typing import Any, Type\r\n\r\nfrom confstar import AnnotatedHandler\r\n\r\n\r\nclass MagicHandler(AnnotatedHandler):\r\n\r\n def set(self, field: str, value: Any) -> Any:\r\n \"\"\"\r\n Set or throw and error if validation fail\r\n \"\"\"\r\n if not value % 2 == 0:\r\n raise ValueError(f\"An error has been occurred in {self.__class__.__name__}\")\r\n\r\n self._attributes[field] = value\r\n\r\n def get(self, field: str) -> Any:\r\n \"\"\"\r\n Return field from `_attributes`\r\n \"\"\"\r\n return self._attributes[field] # or via `dict.get` method\r\n\r\n def __class_getitem__(cls, value: Any) -> Type[MagicHandler]:\r\n \"\"\"\r\n Provide the type annotation logic\r\n \r\n Field: HandlerType[<values>]\r\n \"\"\"\r\n```\r\n\r\n2. Define alias to ignore linter (*which is optional*)\r\n\r\n```py\r\nMagic = type(\"Magic\", (MagicHandler,), {})\r\n```\r\n\r\n\r\n3. Apply your handler on some field\r\n\r\n```py\r\nfrom my_handlers import Magic\r\n\r\n\r\nMAGIC_FIELD: Magic = ...\r\n```\r\n",
"bugtrack_url": null,
"license": "GPLv2+",
"summary": "Extended Config Loader",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/uselessvevo/confstar"
},
"split_keywords": [
"configuration",
"config",
"settings",
"handlers"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "43d69578b6da6e478400f4152ad472d5d1c4f2de203b1c40cc385a3da6649ec6",
"md5": "8a46f32b7452079258615cf6d51cbfc8",
"sha256": "0a9f1764cb5cf98fed1bbd8ff469e3e45936784e75535f6fa8af1a6e17f84827"
},
"downloads": -1,
"filename": "confstar-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8a46f32b7452079258615cf6d51cbfc8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 12664,
"upload_time": "2023-11-22T14:36:36",
"upload_time_iso_8601": "2023-11-22T14:36:36.694225Z",
"url": "https://files.pythonhosted.org/packages/43/d6/9578b6da6e478400f4152ad472d5d1c4f2de203b1c40cc385a3da6649ec6/confstar-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cb45ac31d36b39d13e35ed10fea3a971e1c1f56f466cf87d0a2e1d28c0b02312",
"md5": "7255b238ca785d0cf6bdf19a5978bc65",
"sha256": "114cde75b185ee70d1ef5b9c41f5f8eb3ca6ae38475fdd32c49dc0962e9645f7"
},
"downloads": -1,
"filename": "confstar-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "7255b238ca785d0cf6bdf19a5978bc65",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 14598,
"upload_time": "2023-11-22T14:36:38",
"upload_time_iso_8601": "2023-11-22T14:36:38.702195Z",
"url": "https://files.pythonhosted.org/packages/cb/45/ac31d36b39d13e35ed10fea3a971e1c1f56f466cf87d0a2e1d28c0b02312/confstar-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-22 14:36:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "uselessvevo",
"github_project": "confstar",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "confstar"
}