Skip to content

Instantly share code, notes, and snippets.

@mattupstate
Created March 15, 2012 19:08

Revisions

  1. mattupstate revised this gist Mar 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app.py
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@
    from flask_extended import Flask

    app = Flask(__name__)
    app.config.from_yaml(os.join(app.root_path, 'config.yml')
    app.config.from_yaml(os.join(app.root_path, 'config.yml'))
  2. mattupstate created this gist Mar 15, 2012.
    5 changes: 5 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    os
    from flask_extended import Flask

    app = Flask(__name__)
    app.config.from_yaml(os.join(app.root_path, 'config.yml')
    17 changes: 17 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    COMMON: &common
    SECRET_KEY: insecure

    DEVELOPMENT: &development
    <<: *common
    DEBUG: True

    STAGING: &staging
    <<: *common
    SECRET_KEY: sortasecure

    PRODUCTION: &production
    <<: *common
    SECRET_KEY: shouldbereallysecureatsomepoint



    29 changes: 29 additions & 0 deletions flask_extended.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import os
    import yaml

    from flask import Flask as BaseFlask, Config as BaseConfig

    class Config(BaseConfig):
    """Flask config enhanced with a `from_yaml` method."""

    def from_yaml(self, config_file):
    env = os.environ.get('FLASK_ENV', 'development')
    self['ENVIRONMENT'] = env.lower()

    with open(config_file) as f:
    c = yaml.load(f)

    c = c.get(env, c)

    for key in c.iterkeys():
    if key.isupper():
    self[key] = c[key]

    class Flask(BaseFlask):
    """Extended version of `Flask` that implements custom config class"""

    def make_config(self, instance_relative=False):
    root_path = self.root_path
    if instance_relative:
    root_path = self.instance_path
    return Config(root_path, self.default_config)