PageRenderTime 22ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/main.py

https://bitbucket.org/martijnbb/mbastiaan
Python | 109 lines | 67 code | 18 blank | 24 comment | 10 complexity | 366ce7ef2a9338d8cc2a1508b9aa61ef MD5 | raw file
  1. #!/usr/bin/python3
  2. ########################################################################
  3. # #
  4. # This file is part of the mbastiaan.nl project (or: mbsatiaan) #
  5. # #
  6. # Foobar is free software: you can redistribute it and/or modify #
  7. # it under the terms of the GNU General Public License as published by #
  8. # the Free Software Foundation, either version 3 of the License, or #
  9. # (at your option) any later version. #
  10. # #
  11. # mbastiaan is distributed in the hope that it will be useful, but #
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
  14. # General Public License for more details. #
  15. # #
  16. # You should have received a copy of the GNU General Public License #
  17. # along with mbastiaan. If not, see <http://www.gnu.org/licenses/>. #
  18. # #
  19. ########################################################################
  20. import mbastiaan.model
  21. import mbastiaan.chat
  22. import mbastiaan.main
  23. import sys
  24. import os
  25. import logging
  26. log = logging.getLogger(__name__)
  27. INSTALLED_APPS = (
  28. mbastiaan.chat,
  29. mbastiaan.main
  30. )
  31. settings = None
  32. try:
  33. from mbastiaan import settings
  34. except ImportError:
  35. print("You can define Tornado options in settings.py. Use"\
  36. " the variable {APP,LISTEN}_OPTIONS")
  37. except AttributeError:
  38. print("You need to define OPTIONS in settings.py!")
  39. extra_app_options = settings.APP_OPTIONS if hasattr(settings, 'APP_OPTIONS') else {}
  40. extra_listen_options = settings.LISTEN_OPTIONS if hasattr(settings, 'LISTEN_OPTIONS') else {}
  41. if __name__ == '__main__':
  42. import tornado.web
  43. import os.path
  44. from sqlalchemy.orm.session import sessionmaker
  45. from mbastiaan.utils import urldispatch, model
  46. from sqlalchemy import create_engine
  47. logging.basicConfig(
  48. level=logging.DEBUG,
  49. format="[%(asctime)s %(name)s:%(lineno)s %(levelname)s] %(message)s"
  50. )
  51. # Get all patterns
  52. log = logging.getLogger(__name__)
  53. patterns = tuple(urldispatch.get_patterns(INSTALLED_APPS))
  54. log.info("URL patterns: %s", patterns)
  55. # Create engine and session
  56. engine = create_engine(
  57. "postgresql:///martijn",
  58. echo=False, pool_size=50, max_overflow=0
  59. )
  60. session = sessionmaker(engine)
  61. # Create application and pass database to it
  62. app_options = {
  63. "engine" : engine,
  64. "session" : session,
  65. "debug" : True,
  66. "template_path" : os.path.join(
  67. os.path.dirname(
  68. os.path.abspath(__file__)
  69. ),
  70. 'templates'
  71. ),
  72. "static_path" : os.path.join(
  73. os.path.dirname(
  74. os.path.abspath(__file__)
  75. ),
  76. 'static'
  77. )
  78. }
  79. app_options.update(extra_app_options)
  80. application = tornado.web.Application(patterns, **app_options)
  81. # Import all models and create correct database tables
  82. model.import_all(INSTALLED_APPS)
  83. mbastiaan.model.Base.metadata.create_all(engine)
  84. # Start application
  85. if (len(sys.argv) > 1 and sys.argv[1] == "shell"):
  86. import pdb; pdb.set_trace()
  87. os.exit(0)
  88. listen_options = dict(port=8888)
  89. listen_options.update(extra_listen_options)
  90. application.listen(**listen_options)
  91. tornado.ioloop.IOLoop.instance().start()