Browse Source

Let flask handle the loading of the config

sh4nks 8 years ago
parent
commit
9da969a36c
3 changed files with 24 additions and 24 deletions
  1. 2 0
      MANIFEST.in
  2. 15 4
      flaskbb/app.py
  3. 7 20
      flaskbb/cli/commands.py

+ 2 - 0
MANIFEST.in

@@ -3,6 +3,8 @@ include AUTHORS
 include CHANGES
 include README.md
 graft flaskbb
+graft tests
+graft migrations
 prune flaskbb/themes/*/node_modules
 prune flaskbb/themes/*/.sass-cache
 prune flaskbb/themes/*/src/*

+ 15 - 4
flaskbb/app.py

@@ -18,6 +18,7 @@ from sqlalchemy.engine import Engine
 from flask import Flask, request
 from flask_login import current_user
 
+from flaskbb._compat import string_types
 # views
 from flaskbb.user.views import user
 from flaskbb.message.views import message
@@ -52,16 +53,26 @@ from flaskbb.utils.settings import flaskbb_config
 def create_app(config=None):
     """Creates the app.
 
-    :param config: The configuration object.
+    :param config: The configuration file or object.
+                   The environment variable is weightet as the heaviest.
+                   For example, if the config is specified via an file
+                   and a ENVVAR, it will load the config via the file and
+                   later overwrite it from the ENVVAR.
     """
-
     # Initialize the app
     app = Flask("flaskbb")
 
     # Use the default config and override it afterwards
     app.config.from_object('flaskbb.configs.default.DefaultConfig')
-    # Update the config
-    app.config.from_object(config)
+
+    if isinstance(config, string_types):
+        # config has to be of type string (str, unicode) in order
+        # to be recognized as file
+        app.config.from_pyfile(config)
+    else:
+        # try to update the config from the object
+        app.config.from_object(config)
+
     # try to update the config via the environment variable
     app.config.from_envvar("FLASKBB_SETTINGS", silent=True)
 

+ 7 - 20
flaskbb/cli/commands.py

@@ -144,29 +144,16 @@ def get_version(ctx, param, value):
     ctx.exit()
 
 
-def createapp(info):
-    config_to_use = getattr(info, 'config_to_use', Config)
-
-    click.echo('creating app with config: {}'.format(config_to_use))
-    return create_app(config_to_use)
+def createapp(script_info):
+    config_file = getattr(script_info, 'config_file')
+    if config_file is not None:
+        click.secho('[+] Using config: {}'.format(config_file), fg="cyan")
+    return create_app(config_file)
 
 
 def set_config(ctx, param, value):
-    if value:
-        config_to_use = value
-        try:
-            mod, obj = config_to_use.rsplit('.', 1)
-            import importlib
-            m = importlib.import_module(mod)
-            config = getattr(m, obj)
-        except (ValueError, ImportError):
-            raise FlaskBBCLIError("Cannot load the specified module.",
-                                  fg="red")
-        except AttributeError:
-            raise FlaskBBCLIError("Cannot find the config object in "
-                                  "the specified module.", fg="red")
-
-        ctx.ensure_object(ScriptInfo).config_to_use = config
+    """This will pass the config file to the create_app function."""
+    ctx.ensure_object(ScriptInfo).config_file = value
 
 
 @click.group(cls=FlaskGroup, create_app=createapp)