Skip to content

Commit

Permalink
Refactor code to properly use the config object
Browse files Browse the repository at this point in the history
  • Loading branch information
porduna committed Jul 15, 2014
1 parent 09f9054 commit 996cd3b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
19 changes: 9 additions & 10 deletions labmanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# Import the Flask global application and the configuration
#
from application import app
import config as _config

#
# Blueprints (application modules). The following are present:
Expand All @@ -40,7 +39,7 @@ def load_rlms_modules():
Expand documentation on how to add a new one.
"""
if len(_config.RLMS) == 0:
if len(app.config['RLMS']) == 0:
print >> sys.stderr, "Warning: RLMS configuration variable empty or not found."
print >> sys.stderr, "Warning: This means that this LabManager can not handle any remote lab, which"
print >> sys.stderr, "Warning: does not make sense. You should add a RLMS = [] variable in your "
Expand All @@ -50,7 +49,7 @@ def load_rlms_modules():
import labmanager.rlms.ext.virtual as virtual
assert virtual is not None # pyflakes warning

for _rlms in _config.RLMS:
for _rlms in app.config['RLMS']:
__import__('labmanager.rlms.ext.%s' % _rlms)

# This will register all the RLMSs in the global registry. So it will
Expand All @@ -69,11 +68,13 @@ def register_blueprints():
for url, blueprint in _BLUEPRINTS.items():
app.register_blueprint(blueprint, url_prefix='/rlms' + url)

def bootstrap():
load_views()
load_rlms_modules()
register_blueprints()
# print app.url_map
load_views()
load_rlms_modules()
register_blueprints()
# print app.url_map

# Maintained for compatibility (it might be deployed in certain .wsgi files in other servers)
def bootstrap(): pass

def run():
from .db import check_version
Expand All @@ -83,8 +84,6 @@ def run():
print >> sys.stderr, "And then run this script again"
sys.exit(-1)

bootstrap()

parser = optparse.OptionParser(usage = "Run in development mode the LabManager. In production, please use WSGI.")

parser.add_option('-p', '--port', dest='port', metavar="PORT",
Expand Down
3 changes: 2 additions & 1 deletion labmanager/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os

from flask import Flask, render_template, redirect, url_for
from labmanager.db import db_session

app = Flask(__name__)
app.config.from_object('config')
Expand Down Expand Up @@ -74,6 +73,8 @@ def get_timezone():
#
# Initialize administration panels
#
from labmanager.db import db_session

from .views.admin import init_admin
init_admin(app, db_session)

Expand Down
12 changes: 6 additions & 6 deletions labmanager/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config import SQLALCHEMY_ENGINE_STR, USE_PYMYSQL
from labmanager.utils import data_filename
from labmanager.application import app

if USE_PYMYSQL:
if app.config.get('USE_PYMYSQL', False):
import pymysql_sa
pymysql_sa.make_default_mysql_dialect()

engine = create_engine(SQLALCHEMY_ENGINE_STR, convert_unicode=True, pool_recycle=3600)
engine = create_engine(app.config['SQLALCHEMY_ENGINE_STR'], convert_unicode=True, pool_recycle=3600)

db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
Expand All @@ -34,8 +34,8 @@
def create_alembic_config():
alembic_config = Config("alembic.ini")
alembic_config.set_main_option("script_location", os.path.abspath(data_filename('alembic')))
alembic_config.set_main_option("url", SQLALCHEMY_ENGINE_STR)
alembic_config.set_main_option("sqlalchemy.url", SQLALCHEMY_ENGINE_STR)
alembic_config.set_main_option("url", app.config['SQLALCHEMY_ENGINE_STR'])
alembic_config.set_main_option("sqlalchemy.url", app.config['SQLALCHEMY_ENGINE_STR'])
return alembic_config

def init_db(drop = False):
Expand Down Expand Up @@ -66,7 +66,7 @@ def check_version():
script = ScriptDirectory.from_config(alembic_config)
head = script.get_current_head()

engine = create_engine(SQLALCHEMY_ENGINE_STR)
engine = create_engine(app.config['SQLALCHEMY_ENGINE_STR'])

context = MigrationContext.configure(engine)
current_rev = context.get_current_revision()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PyYAML==3.10
SQLAlchemy==0.7.10
alembic==0.6.4
requests==2.1.0
Flask-Testing==0.4.1

git+https://github.com/gateway4labs/rlms_unr.git
git+https://github.com/gateway4labs/rlms_weblabdeusto.git
Expand Down
3 changes: 1 addition & 2 deletions run_wsgi.wsgi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ os.chdir(LABMANAGER_DIR)

import config

from labmanager import app as application, bootstrap
bootstrap()
from labmanager import app as application
application.config.from_object('config')

0 comments on commit 996cd3b

Please sign in to comment.