use local dir for config by default

This commit is contained in:
termie 2013-11-26 11:01:24 -08:00
parent ce60f96137
commit ca4b81866a
6 changed files with 52 additions and 19 deletions

View File

@ -32,4 +32,14 @@ This is our documentation for how we get this set up::
# gunicorn: # gunicorn:
gunicorn refstack.web:app gunicorn refstack.web:app
# To actually configure this winner, check out the config section and
# crack open refstack.cfg in vim.
# `vim refstack.cfg`
# Now browse to http://localhost:8000 # Now browse to http://localhost:8000
Configuration
-------------
Coming soon!

View File

@ -18,7 +18,7 @@ sqlalchemy.url = driver://user:pass@localhost/dbname
# path to migration scripts # path to migration scripts
script_location = alembic script_location = alembic
sqlalchemy.url = sqlite:///refstack.db sqlalchemy.url = sqlite:///db.sqlite
# Logging configuration # Logging configuration

View File

@ -15,7 +15,11 @@ from .config import DefaultConfig
#from .admin import admin #from .admin import admin
#from .extensions import db, mail, cache, login_manager, oid #from .extensions import db, mail, cache, login_manager, oid
from .extensions import db, mail, login_manager, oid from .extensions import db, mail, login_manager, oid
from .utils import INSTANCE_FOLDER_PATH
from refstack import utils
INSTANCE_FOLDER_PATH = utils.INSTANCE_FOLDER_PATH
# For import * # For import *
@ -38,9 +42,17 @@ def create_app(config=None, app_name=None, blueprints=None):
if blueprints is None: if blueprints is None:
blueprints = DEFAULT_BLUEPRINTS blueprints = DEFAULT_BLUEPRINTS
# NOTE(termie): Flask has this new instance_path stuff that allows
# you to keep config and such in different places, but I don't really
# see how that is going to be very helpful, so we're going to stick
# to using config relative to the root unless we explicitly set such
# a path in the INSTANCE_FOLDER_PATH environment variable.
app = Flask(app_name, app = Flask(app_name,
instance_path=INSTANCE_FOLDER_PATH, instance_path=INSTANCE_FOLDER_PATH,
instance_relative_config=True) instance_relative_config=True)
configure_app(app, config) configure_app(app, config)
configure_hook(app) configure_hook(app)
configure_blueprints(app, blueprints) configure_blueprints(app, blueprints)
@ -50,6 +62,9 @@ def create_app(config=None, app_name=None, blueprints=None):
configure_template_filters(app) configure_template_filters(app)
configure_error_handlers(app) configure_error_handlers(app)
if app.debug:
print utils.dump_config(app)
return app return app
@ -59,8 +74,10 @@ def configure_app(app, config=None):
# http://flask.pocoo.org/docs/api/#configuration # http://flask.pocoo.org/docs/api/#configuration
app.config.from_object(DefaultConfig) app.config.from_object(DefaultConfig)
# http://flask.pocoo.org/docs/config/#instance-folders # If we've set the INSTANCE_FOLDER_PATH environment var, this may be
app.config.from_pyfile('production.cfg', silent=True) # loaded from an instance folder, otherwise relative to flask.root_path.
# http://flask.pocoo.org/docs/config/#instance-folders
app.config.from_pyfile('refstack.cfg', silent=True)
if config: if config:
app.config.from_object(config) app.config.from_object(config)
@ -128,7 +145,7 @@ def configure_logging(app):
import logging import logging
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
# Set info level on logger, which might be overwritten by handers. # Set info level on logger, which might be overwritten by handlers.
# Suppress DEBUG messages. # Suppress DEBUG messages.
app.logger.setLevel(logging.INFO) app.logger.setLevel(logging.INFO)

View File

@ -4,16 +4,15 @@
import os import os
from utils import make_dir, INSTANCE_FOLDER_PATH from utils import make_dir, INSTANCE_FOLDER_PATH, PROJECT_ROOT
class BaseConfig(object): class BaseConfig(object):
PROJECT = "fbone" PROJECT = "refstack"
# Get app root path, also can use flask.root_path. # The app root path, also can use flask.root_path.
# ../../config.py PROJECT_ROOT = PROJECT_ROOT
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
DEBUG = False DEBUG = False
TESTING = False TESTING = False

View File

@ -6,15 +6,17 @@
Utils has nothing to do with models and views. Utils has nothing to do with models and views.
""" """
import string
import random
import os
from datetime import datetime from datetime import datetime
import logging
import os
import pprint
import random
import string
# Instance folder path, make it independent. # Instance folder path, if set, otherwise project root.
INSTANCE_FOLDER_PATH = os.path.join('/tmp', 'instance') PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
INSTANCE_FOLDER_PATH = os.environ.get('INSTANCE_FOLDER_PATH', PROJECT_ROOT)
ALLOWED_AVATAR_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) ALLOWED_AVATAR_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
@ -104,3 +106,11 @@ def make_dir(dir_path):
os.mkdir(dir_path) os.mkdir(dir_path)
except Exception, e: except Exception, e:
raise e raise e
### Begin Non-Fbone stuff
def dump_config(app):
"""Useful to dump app config for debug purposes."""
return pprint.pformat(dict(app.config.iteritems()))

View File

@ -33,9 +33,6 @@ from refstack import utils
from refstack.models import * from refstack.models import *
# TODO(termie): temporary hack for first-run experience
utils.make_dir(utils.INSTANCE_FOLDER_PATH)
# TODO(termie): transition all the routes below to blueprints # TODO(termie): transition all the routes below to blueprints
# TODO(termie): use extensions setup from the create_app() call # TODO(termie): use extensions setup from the create_app() call