Skip to main content

Application configuration from environment variables made easy

Project description

Application configuration from environment variables made easy

from environconfig import EnvironConfig
from environconfig import StringVar, IntVar
from environconfig import VarUnsetError

class AppConfig(EnvironConfig):
    __varprefix__ = 'MYAPP_'

    DB_NAME = StringVar(default='mydatabase')
    DB_HOSTNAME = StringVar(default='localhost')
    DB_PORT = IntVar(default=3306)
    DB_USERNAME = StringVar()
    DB_PASSWORD = StringVar()

    @CustomVar
    def DB_CONFIG(env):
        return {"hostname": env.DB_HOSTNAME,
                "port": env.DB_PORT,
                "user": env.DB_USERNAME,
                "password": env.DB_PASSWORD,
                "database": env.DB_NAME,
                "encoding": "utf-8"}
# Any environment variable defined will be retrieved and casted to
# the python value.
os.environ['MYAPP_DB_NAME'] = 'mydbname'
assert AppConfig.DB_NAME == 'mydbname'
try:
    user = AppConfig.DB_USERNAME
except VarUnsetError:
    # If the environment variable is not set and neither the default
    # value, this exception will be raised when the attribute is
    # accessed.
    pass
# Of course if you provide a default it will be available as a
# fallback.
assert AppConfig.DB_HOSTNAME == 'localhost'
os.environ['MYAPP_DB_PORT'] = 'this is not a valid integer'
try:
    port = AppConfig.DB_PORT
except VarTypeCastError:
    # Verification is made in access time.
    pass
# But you can verify the whole config
AppConfig.verify()  # Return `True` if all attributes have a value
                    # (or a default)

# Or a specific value
AppConfig.verify('DB_PORT')  # Return `True` if a value or a default is
                             # provided for DB_PORT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

environconfig-1.2.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page