# sanic-envconfig
[![Python Versions](https://img.shields.io/pypi/pyversions/sanic-envconfig.svg)](https://pypi.python.org/pypi/sanic-envconfig)
[![PyPI Version](https://img.shields.io/pypi/v/sanic-envconfig.svg)](https://pypi.python.org/pypi/sanic-envconfig)
[![Licence](http://img.shields.io/:license-mit-blue.svg)](https://github.com/jamesstidard/sanic-envconfig/blob/master/LICENCE.txt)
[![Build Status](https://travis-ci.org/jamesstidard/sanic-envconfig.svg?branch=master)](https://travis-ci.org/jamesstidard/sanic-envconfig)
[![Coverage Status](https://coveralls.io/repos/github/jamesstidard/sanic-envconfig/badge.svg)](https://coveralls.io/github/jamesstidard/sanic-envconfig)
This extension helps you bring commandline & environment variables into your Sanic config.
The extension also leverages type hints to correctly cast those variables to the appropriate type. This can also be overridden and extended for your own types.
## How it Works
Define your config class and subclass `sanic_envconfig.EnvConfig`. To not pollute your config, only those variables defined (and in uppercase) in your config class will pulled from your environment or commandline variables.
The values set in your class will be the default values, overridden when there is a environment variable or commandline argument with the same name available. The priority order being: `commandline arguments > enviroment variables > config`.
Casting of the commandline & environment variables is decided by looking at the type hints declared on config class. If no hint has been declared, the type of the default value will be used. When a default value is also not provided the variable will be returned as whatever type it exists in `os.environ` or `sys.argv` (most certainly a `str`).
This extension takes care of correctly casting the common types `str`, `bool`, `int` and `float`. Though, `sanic_envconfig.EnvConfig` can be extended for custom types. Additionally, the supplied casting can be overridden if desired.
## Just Sanic?
The extension, for the moment, is generic enough where it could be used in another context. Though, future releases may more tightly couple it with Sanic.
## Installation
```bash
$ pip install sanic_envconfig
```
## Example Usage
```bash
DEBUG: false
DB_URL: postgresql://localhost:5433/environ
WORKERS: 4
$ python example.py --db-url "postgresql://localhost:5433/commandline"
```
```python
# example.py
from sanic import Sanic
from sanic_envconfig import EnvConfig
class Config(EnvConfig):
DEBUG: bool = True
DB_URL: str = None
WORKERS: int = 1
app = Sanic(__name__)
app.config.from_object(Config)
app.config.DEBUG # False
app.config.DB_URL # 'postgresql://localhost:5433/commandline'
app.config.WORKERS # 4
```
## Custom Casting
To override or extend the casting system, `sanic_envconfig.EnvConfig` provides a decorator. The decorator takes the type as a parameter and hands the decorated function any values of that type from the environment variables.
```bash
THEME: blue
```
```python
from enum import Enum
from sanic_envconfig import EnvConfig
class Color(Enum):
RED = 0
BLUE = 1
class Config(EnvConfig):
THEME: Color = None
@Config.parse(Color)
def parse_color(value):
return Color[value.upper()]
Config.THEME # Color.BLUE
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共12个文件
txt:4个
py:4个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:sanic_envconfig-1.3.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
sanic_envconfig-1.3.1.tar.gz (12个子文件)
sanic_envconfig-1.3.1
PKG-INFO 611B
setup.cfg 38B
setup.py 2KB
README.md 3KB
sanic_envconfig
envconfig.py 3KB
utils.py 560B
__init__.py 391B
sanic_envconfig.egg-info
PKG-INFO 611B
requires.txt 38B
SOURCES.txt 294B
top_level.txt 16B
dependency_links.txt 1B
共 12 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功