PageRenderTime 40ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/kpages/app.py

https://github.com/comger/kpages
Python | 113 lines | 79 code | 22 blank | 12 comment | 13 complexity | 8a5ddb53ee5db7e4d61b05233e7f7d41 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. author comger@gmail.com
  5. """
  6. import tornado.web
  7. import tornado.ioloop
  8. import tornado.options
  9. from tornado.options import define, options
  10. from inspect import isclass
  11. from tornado.httpserver import HTTPServer
  12. from optparse import OptionParser, OptionGroup
  13. from kpages.router import load_handlers
  14. from kpages.context import LogicContext
  15. from kpages.utility import refresh_config, app_path, set_default_encoding, get_members
  16. def get_ui_modules():
  17. """
  18. return ui module members in ACTION_DIR
  19. """
  20. m_filter = lambda m: isclass(m) and issubclass(m, tornado.web.UIModule)
  21. ms = get_members(__conf__.ACTION_DIR, member_filter=m_filter)
  22. if 'tornado.web.UIModule' in ms:
  23. del ms['tornado.web.UIModule']
  24. newms = {}
  25. for key,val in ms.items():
  26. newms[key.replace('.','_')] = val
  27. return newms
  28. def get_ui_methods():
  29. """
  30. return uimethod methods in ACTION_DIR
  31. """
  32. m_filter = lambda m: hasattr(m,'__reg_ui__') and m.__reg_ui__ ==True
  33. ms = get_members(__conf__.ACTION_DIR, member_filter=m_filter)
  34. newms = {}
  35. for key,val in ms.items():
  36. newms[key.replace('.','_')] = val
  37. return newms
  38. class WebApp(object):
  39. settings = property(lambda self: __conf__.__dict__)
  40. handlers = property(lambda self: self._handlers)
  41. uimodules = property(lambda self: self._modules)
  42. uimethods = property(lambda self: self._methods)
  43. webapp = property(lambda self: self._webapp)
  44. def __init__(self, port=None,bindip=None, handlers=None, callback=None):
  45. self._port = port or __conf__.PORT
  46. self._ip = bindip or __conf__.BIND_IP
  47. self._callback = callback
  48. self._handlers = load_handlers(__conf__.ACTION_DIR)
  49. self._modules = get_ui_modules()
  50. self._methods = get_ui_methods()
  51. self._webapp = self._get_webapp()
  52. __conf__.APP = self._webapp
  53. def _get_webapp(self):
  54. settings = {"debug": __conf__.DEBUG,
  55. "static_path": app_path(__conf__.STATIC_DIR_NAME),
  56. "template_path": app_path(__conf__.TEMPLATE_DIR_NAME),
  57. "gzip": __conf__.GZIP,
  58. "cookie_secret": __conf__.COOKIE_SECRET,
  59. "ui_modules":self.uimodules,
  60. "ui_methods":self.uimethods,
  61. "xsrf_cookies": __conf__.XSRF_COOKIES}
  62. return tornado.web.Application(self._handlers, **settings)
  63. def _run_server(self):
  64. if __conf__.DEBUG:
  65. print('web app')
  66. self._webapp.listen(self._port, address=self._ip, max_buffer_size = __conf__.max_buffer_size)
  67. else:
  68. server = HTTPServer(self._webapp, xheaders=True, max_buffer_size = __conf__.max_buffer_size)
  69. server.bind(self._port, address=self._ip)
  70. server.start(0)
  71. #tornado.ioloop.IOLoop.instance().start()
  72. tornado.ioloop.IOLoop.current().start()
  73. def run(self):
  74. if self._callback:
  75. self._callback(self)
  76. self._run_server()
  77. define("port", default=None, help="run on the given port", type=int)
  78. define("config", default='setting.py', help="set config for server")
  79. define("ip", help="bind accept ip for server")
  80. define("debug", default=None, help="Debug Mode")
  81. define("ndebug", help="No Debug Mode")
  82. def run(callback=None):
  83. set_default_encoding()
  84. tornado.options.parse_command_line()
  85. opts = options
  86. refresh_config(opts.config)
  87. __conf__.PORT = opts.port or __conf__.PORT
  88. __conf__.DEBUG = opts.debug or __conf__.DEBUG
  89. __conf__.BIND_IP = opts.ip or __conf__.BIND_IP
  90. WebApp(callback=callback).run()
  91. __all__ = ["run", "WebApp", "get_ui_modules", "get_ui_methods"]