PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/demisauce/app.py

https://github.com/araddon/demisauce
Python | 111 lines | 83 code | 20 blank | 8 comment | 4 complexity | 19e88d14bced36a7271da38fb3b1a368 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import tornado.auth
  4. import tornado.httpserver
  5. import tornado.ioloop
  6. import tornado.options
  7. import tornado.web
  8. import tornado.escape
  9. from tornado.options import define, options
  10. import os, logging, functools
  11. import jinja2
  12. #from demisaucepy import options as dsoptions
  13. import demisaucepy.options
  14. import demisauce
  15. tornado.options.parse_command_line() # must force load of options for metaclass
  16. from demisaucepy import cache_setup
  17. from demisaucepy.cache import DummyCache, MemcacheCache
  18. import demisauce
  19. from demisauce import model
  20. from demisauce.lib.cacheextension import FragmentCacheExtension
  21. import demisaucepy
  22. from demisauce.lib import helpers
  23. from demisauce.appbase import AppBase
  24. log = logging.getLogger("demisauce")
  25. SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
  26. PROJECT_ROOT = os.path.realpath(SITE_ROOT + '/../../' )
  27. if "/Users" in SITE_ROOT:
  28. db_root = os.path.realpath(PROJECT_ROOT + '/database/' )
  29. else:
  30. db_root = os.path.realpath(SITE_ROOT + '/' )
  31. class Jinja2Environment( jinja2.Environment ):
  32. def load( self, template_path):
  33. tmpl = self.get_template( template_path )
  34. if tmpl:
  35. setattr(tmpl, "generate", tmpl.render)
  36. return tmpl
  37. class Application(tornado.web.Application):
  38. def __init__(self):
  39. template_path = os.path.join(os.path.dirname(__file__), "demisauce/views")
  40. # Have one global connection to the DB across app
  41. cache_setup.load_cache()
  42. memcache_cache = MemcacheCache(options.memcached_servers)
  43. self.db = model.get_database(cache=memcache_cache)
  44. log.debug("gearman_servers = %s" % options.gearman_servers)
  45. settings = {
  46. "title": u"Local 151",
  47. "template_path": template_path,
  48. "static_path":os.path.join(os.path.dirname(__file__), "demisauce/static"),
  49. "xsrf_cookies": False,
  50. "cookie_secret":"32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
  51. "login_url":"/user/signin",
  52. "redis_host":options.redis_host,
  53. "demisauce_url":options.demisauce_url,
  54. "asset_url":options.asset_url,
  55. "oauth_callback":("%saccount/" % options.base_url),
  56. "debug":options.debug,
  57. "base_url":options.base_url,
  58. "twitter_consumer_key":options.twitter_consumer_key,
  59. "twitter_consumer_secret":options.twitter_consumer_secret,
  60. "facebook_api_key":options.facebook_api_key,
  61. "facebook_secret":options.facebook_secret,
  62. "sqlalchemy_default_url":options.sqlalchemy_default_url,
  63. "sqlalchemy_default_echo":options.sqlalchemy_default_echo,
  64. #"template_path":template_path,
  65. }## "ui_modules": {"Entry": EntryModule},
  66. from demisauce import controllers
  67. _handlers = [] + controllers._controllers
  68. from demisauce.controllers import account, home, dashboard, template,\
  69. admin, site, api, service
  70. _handlers += account._controllers + \
  71. home._controllers + dashboard._controllers + template._controllers + \
  72. admin._controllers + site._controllers + api._controllers + \
  73. service._controllers
  74. from demisauce.controllers import CustomErrorHandler
  75. self.error_handler = CustomErrorHandler
  76. jinja_env = Jinja2Environment(loader=jinja2.FileSystemLoader(template_path),
  77. extensions=[FragmentCacheExtension])
  78. #jinja_env.fragment_cache = DummyCache()
  79. jinja_env.fragment_cache = memcache_cache
  80. # custom filters etc
  81. jinja_env.filters.update(helpers._filters)
  82. settings.update({"jinja2_env":jinja_env})
  83. # start web app
  84. tornado.web.Application.__init__(self, _handlers, **settings)
  85. def main():
  86. application = Application()
  87. http_server = tornado.httpserver.HTTPServer(application)
  88. http_server.listen(options.port)
  89. tornado.ioloop.IOLoop.instance().start()
  90. if __name__ == "__main__":
  91. main()