Add the capability to load gunicorn.base.Application without the loading

of the arguments of the command line.
This commit is contained in:
Stephane Wirtel 2013-12-01 15:51:20 +01:00 committed by benoitc
parent 26a796f200
commit e273052e3f

View File

@ -13,12 +13,11 @@ from gunicorn.config import Config, get_default_config_file
from gunicorn import debug from gunicorn import debug
from gunicorn.six import execfile_ from gunicorn.six import execfile_
class Application(object): class BaseApplication(object):
"""\ """
An application interface for configuring and loading An application interface for configuring and loading
the various necessities for any given web framework. the various necessities for any given web framework.
""" """
def __init__(self, usage=None, prog=None): def __init__(self, usage=None, prog=None):
self.usage = usage self.usage = usage
self.cfg = None self.cfg = None
@ -29,14 +28,43 @@ class Application(object):
def do_load_config(self): def do_load_config(self):
try: try:
self.load_default_config()
self.load_config() self.load_config()
except Exception as e: except Exception as e:
sys.stderr.write("\nError: %s\n" % str(e)) sys.stderr.write("\nError: %s\n" % str(e))
sys.stderr.flush() sys.stderr.flush()
sys.exit(1) sys.exit(1)
def load_config_from_file(self, filename): def load_default_config(self):
# init configuration
self.cfg = Config(self.usage, prog=self.prog)
def init(self, parser, opts, args):
raise NotImplementedError
def load(self):
raise NotImplementedError
def reload(self):
self.do_load_config()
if self.cfg.spew:
debug.spew()
def wsgi(self):
if self.callable is None:
self.callable = self.load()
return self.callable
def run(self):
try:
Arbiter(self).run()
except RuntimeError as e:
sys.stderr.write("\nError: %s\n\n" % e)
sys.stderr.flush()
sys.exit(1)
class Application(BaseApplication):
def load_config_from_file(self, filename):
if not os.path.exists(filename): if not os.path.exists(filename):
raise RuntimeError("%r doesn't exist" % filename) raise RuntimeError("%r doesn't exist" % filename)
@ -67,9 +95,6 @@ class Application(object):
return cfg return cfg
def load_config(self): def load_config(self):
# init configuration
self.cfg = Config(self.usage, prog=self.prog)
# parse console args # parse console args
parser = self.cfg.parser() parser = self.cfg.parser()
args = parser.parse_args() args = parser.parse_args()
@ -98,22 +123,6 @@ class Application(object):
continue continue
self.cfg.set(k.lower(), v) self.cfg.set(k.lower(), v)
def init(self, parser, opts, args):
raise NotImplementedError
def load(self):
raise NotImplementedError
def reload(self):
self.do_load_config()
if self.cfg.spew:
debug.spew()
def wsgi(self):
if self.callable is None:
self.callable = self.load()
return self.callable
def run(self): def run(self):
if self.cfg.check_config: if self.cfg.check_config:
try: try:
@ -139,9 +148,5 @@ class Application(object):
if pythonpath not in sys.path: if pythonpath not in sys.path:
sys.path.insert(0, pythonpath) sys.path.insert(0, pythonpath)
try: super(Application, self).run()
Arbiter(self).run()
except RuntimeError as e:
sys.stderr.write("\nError: %s\n\n" % e)
sys.stderr.flush()
sys.exit(1)