wsgiapp.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. import os
  6. from gunicorn.errors import ConfigError
  7. from gunicorn.app.base import Application
  8. from gunicorn import util
  9. class WSGIApplication(Application):
  10. def init(self, parser, opts, args):
  11. self.app_uri = None
  12. if opts.paste:
  13. from .pasterapp import has_logging_config
  14. config_uri = os.path.abspath(opts.paste)
  15. config_file = config_uri.split('#')[0]
  16. if not os.path.exists(config_file):
  17. raise ConfigError("%r not found" % config_file)
  18. self.cfg.set("default_proc_name", config_file)
  19. self.app_uri = config_uri
  20. if has_logging_config(config_file):
  21. self.cfg.set("logconfig", config_file)
  22. return
  23. if len(args) > 0:
  24. self.cfg.set("default_proc_name", args[0])
  25. self.app_uri = args[0]
  26. def load_config(self):
  27. super().load_config()
  28. if self.app_uri is None:
  29. if self.cfg.wsgi_app is not None:
  30. self.app_uri = self.cfg.wsgi_app
  31. else:
  32. raise ConfigError("No application module specified.")
  33. def load_wsgiapp(self):
  34. return util.import_app(self.app_uri)
  35. def load_pasteapp(self):
  36. from .pasterapp import get_wsgi_app
  37. return get_wsgi_app(self.app_uri, defaults=self.cfg.paste_global_conf)
  38. def load(self):
  39. if self.cfg.paste is not None:
  40. return self.load_pasteapp()
  41. else:
  42. return self.load_wsgiapp()
  43. def run():
  44. """\
  45. The ``gunicorn`` command line runner for launching Gunicorn with
  46. generic WSGI applications.
  47. """
  48. from gunicorn.app.wsgiapp import WSGIApplication
  49. WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  50. if __name__ == '__main__':
  51. run()