Manage project configuration on multiple environments with ease.
Project description
Figa can read from multiple config sources including several file formats, environment variables, and even the Internet, allowing you to configure your project however you want.
Figa supports many sources, including:
Environment variables
Dict objects
JSON (.json)
HOCON (.hocon, .conf)
INI, CFG (.ini, .cfg)
YAML (.yaml, .yml)
TOML (.toml)
.properties (.properties)
Internet resources
$ pip install figa
Usage
import figa
@figa.config
class MyConfig:
development = "~/config.yml" # use YAML file for config when developing
production = "env", "cfg_" # use environment variables with cfg_ prefix in production
config = MyConfig("development")
>>> config.key == config["key"] # config can be accessed using dots or indexing
True
Environment Detection
You can implement your own function that detects where to pull config values from.
@figa.config
class Config:
development = "~/config.yml" # use YAML file for config when developing
production = "env", "cfg_" # use environment variables with cfg_ prefix in production
def get_env(self):
if "ON_HEROKU" in figa.env: # figa.env is shortcut for os.environ
return "production"
else:
return "development"
>>> config = Config() # if no environment is passed, get_env() will be called.
File Types
By default, the config file type will be guessed from the file extension. This can also be set explicitly:
@figa.config
class MyConfig:
example = "ini", "./config.conf"
# .conf would be detected as HOCON, but we set to INI
Default Values
Default values can be set that will be included on every environment.
@figa.config
class MyConfig:
default = {"name": "My App"}
dev = {"host": "localhost"}
prod = {"host": "myapp.com"}
>>> dev_cfg = MyConfig("dev")
>>> prod_cfg = MyConfig("prod")
>>> dev_cfg.name == prod_cfg.name # "name" config item is included in both
True
Required Values and Type Checking
In your Config class you can set required values and types that will be checked when the config is loaded. This helps ensure that your code doesn’t run with missing information.
@figa.config
class Config:
# two required config values: `string` & `sub.number`
__required__ = {
"string": str,
"sub": {
"number": int
}
}
If any values are missing, an error will be raised:
@figa.config
class Config:
# two required config values: `string` & `sub.number`
__required__ = {
"string": str,
"sub": {
"number": int
}
}
missing_vals = { # this config is missing sub.number
"string": "hello, world",
"sub": {}
}
>>> cfg = Config("missing_vals")
ValueError: Missing required item 'sub.number'
Figa will automatically convert strings and numbers for you where possible.
@figa.config
class Config:
__required__ = {
"stringval": str,
"numberval": int
}
values = {
"stringval": 100,
"numberval": "42"
}
>>> cfg = Config("values")
>>> cfg.stringval
'100'
>>> cfg.numberval
42
This project is published under the MIT License. See LICENSE.md.
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
File details
Details for the file figa-0.1a5.tar.gz
.
File metadata
- Download URL: figa-0.1a5.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56c51b49e40522b0f452c7d0e3b771bee1391a81159cd5399420ec5309989d2d |
|
MD5 | 0dc9152d62193c1f0402bfcd76e9e6ee |
|
BLAKE2b-256 | 742b90b7fa95900fb89f16b3f952e1c9e2a9281b6a83e9e0c0659fea83f04b6d |