/main.py
https://bitbucket.org/martijnbb/mbastiaan · Python · 109 lines · 67 code · 18 blank · 24 comment · 10 complexity · 366ce7ef2a9338d8cc2a1508b9aa61ef MD5 · raw file
- #!/usr/bin/python3
- ########################################################################
- # #
- # This file is part of the mbastiaan.nl project (or: mbsatiaan) #
- # #
- # Foobar is free software: you can redistribute it and/or modify #
- # it under the terms of the GNU General Public License as published by #
- # the Free Software Foundation, either version 3 of the License, or #
- # (at your option) any later version. #
- # #
- # mbastiaan is distributed in the hope that it will be useful, but #
- # WITHOUT ANY WARRANTY; without even the implied warranty of #
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
- # General Public License for more details. #
- # #
- # You should have received a copy of the GNU General Public License #
- # along with mbastiaan. If not, see <http://www.gnu.org/licenses/>. #
- # #
- ########################################################################
- import mbastiaan.model
- import mbastiaan.chat
- import mbastiaan.main
- import sys
- import os
- import logging
- log = logging.getLogger(__name__)
- INSTALLED_APPS = (
- mbastiaan.chat,
- mbastiaan.main
- )
- settings = None
- try:
- from mbastiaan import settings
- except ImportError:
- print("You can define Tornado options in settings.py. Use"\
- " the variable {APP,LISTEN}_OPTIONS")
- except AttributeError:
- print("You need to define OPTIONS in settings.py!")
- extra_app_options = settings.APP_OPTIONS if hasattr(settings, 'APP_OPTIONS') else {}
- extra_listen_options = settings.LISTEN_OPTIONS if hasattr(settings, 'LISTEN_OPTIONS') else {}
- if __name__ == '__main__':
- import tornado.web
- import os.path
- from sqlalchemy.orm.session import sessionmaker
- from mbastiaan.utils import urldispatch, model
- from sqlalchemy import create_engine
- logging.basicConfig(
- level=logging.DEBUG,
- format="[%(asctime)s %(name)s:%(lineno)s %(levelname)s] %(message)s"
- )
- # Get all patterns
- log = logging.getLogger(__name__)
- patterns = tuple(urldispatch.get_patterns(INSTALLED_APPS))
- log.info("URL patterns: %s", patterns)
- # Create engine and session
- engine = create_engine(
- "postgresql:///martijn",
- echo=False, pool_size=50, max_overflow=0
- )
- session = sessionmaker(engine)
- # Create application and pass database to it
- app_options = {
- "engine" : engine,
- "session" : session,
- "debug" : True,
- "template_path" : os.path.join(
- os.path.dirname(
- os.path.abspath(__file__)
- ),
- 'templates'
- ),
- "static_path" : os.path.join(
- os.path.dirname(
- os.path.abspath(__file__)
- ),
- 'static'
- )
- }
- app_options.update(extra_app_options)
- application = tornado.web.Application(patterns, **app_options)
- # Import all models and create correct database tables
- model.import_all(INSTALLED_APPS)
- mbastiaan.model.Base.metadata.create_all(engine)
- # Start application
- if (len(sys.argv) > 1 and sys.argv[1] == "shell"):
- import pdb; pdb.set_trace()
- os.exit(0)
- listen_options = dict(port=8888)
- listen_options.update(extra_listen_options)
- application.listen(**listen_options)
- tornado.ioloop.IOLoop.instance().start()