Created
July 25, 2015 02:27
-
-
Save shouhei/12a37d5d733826ac0980 to your computer and use it in GitHub Desktop.
flask-Classyのregisterをリフレクションで一括実行する奴
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import inspect | |
| import os | |
| from flask import Flask | |
| import controller # FlaskViewを継承したViewがあるパッケージ | |
| # prepare application | |
| app = Flask(__name__) | |
| app.config.from_object('config.Config') | |
| # prepare controller | |
| # 下記のcontrollerはパッケージ | |
| for pack in os.listdir(controller.__path__[0])[2:]: | |
| # 以下のstringのcontrollerも同様 | |
| data = __import__(inspect.getmodulename("controller."+pack), fromlist=['controller']) | |
| # FlaskView以外のViewファイルをインポートしてregisterを実行 | |
| for view in filter(lambda item: (re.search("[^Flask]View$", item[0])), inspect.getmembers(data,inspect.isclass)): | |
| view[1].register(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment