summaryrefslogtreecommitdiffstats
path: root/app/__init__.py
blob: 6c6b08c17bbce11fa8beda5541dab6dbba445a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-

from flask import Flask, render_template, g
from .main import main
from .backend import Storage
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def create_app(config=None):
    """Creates the Flask app."""
    app = Flask(__name__)

    configure_app(app)
    configure_error_handlers(app)
    init_app(app)

    for blueprint in [main]:
        app.register_blueprint(blueprint)

    return app


def configure_app(app):
    app.config.from_pyfile('../config.cfg')

    # http://flask.pocoo.org/docs/0.10/errorhandling/
    if not app.debug:
        import logging
        from logging.handlers import RotatingFileHandler
        file_handler = RotatingFileHandler(app.config['LOG_FILE_PATH'])
        file_handler.setLevel(logging.WARNING)
        file_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s'
            '[in %(pathname)s:%(lineno)d]'))
        app.logger.addHandler(file_handler)


def init_app(app):
    import os
    @app.before_request
    def init():
        g.studies = {}
        for i, study in enumerate(app.config['STUDIES'].items()):
          abbr = study[0]
          g.studies[abbr] = Storage(os.path.join('app', 'static','studies', abbr))

          modules = app.config['STUDIES'][study[0]]
          # extend module list with git values
          for module in g.studies[abbr].get_modules():
            # check if module is already listed
            if all(map(lambda (k,v): v != module, modules)):
              slug = module.decode('ascii', errors='ignore')
              app.config['STUDIES'][study[0]].append((slug, module))


## populate Module-List
#fit = {}
#for i, study in enumerate(app.config['STUDIES'].items()):
#  abbr = study[0]
#  fit[abbr] = Fit(os.path.join('static','studies',abbr + '.git'))
#
#  modules = app.config['STUDIES'][study[0]]
#  # extend module list with git values
#  for module in fit[abbr].get_modules():
#    # check if module is already listed
#    if all(map(lambda (k,v): v != module, modules)):
#      slug = module.decode('ascii', errors='ignore')
#      app.config['STUDIES'][study[0]].append((slug, module))

def configure_error_handlers(app):
    @app.route('/forbidden')
    @app.errorhandler(400)
    @app.errorhandler(403)
    @app.errorhandler(404)
    def errorhandler(e):
        return render_template('error.html', error=e), e.code