mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Slight touches to the Config class.
This commit is contained in:
parent
898d770d14
commit
4fe8608196
@ -43,98 +43,83 @@ class Config(object):
|
|||||||
before_exec=lambda server: server.log.info("Forked child, reexecuting")
|
before_exec=lambda server: server.log.info("Forked child, reexecuting")
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, cmdopts, path=None):
|
def __init__(self, opts, path=None):
|
||||||
if not path:
|
self.cfg = self.DEFAULTS.copy()
|
||||||
self.config_file = os.path.join(os.getcwd(),
|
|
||||||
self.DEFAULT_CONFIG_FILE)
|
|
||||||
else:
|
|
||||||
self.config_file = os.path.abspath(os.path.normpath(path))
|
|
||||||
self.cmdopts = cmdopts
|
|
||||||
self.conf = {}
|
|
||||||
self.load()
|
|
||||||
|
|
||||||
def _load_file(self):
|
if path is None:
|
||||||
"""
|
path = os.path.join(os.getcwd(), self.DEFAULT_CONFIG_FILE)
|
||||||
Returns a dict of stuff found in the config file.
|
if os.path.exists(path):
|
||||||
Defaults to $PWD/gunicorn.conf.py.
|
try:
|
||||||
"""
|
execfile(path, globals(), self.cfg)
|
||||||
if not os.path.exists(self.config_file):
|
except Exception, e:
|
||||||
return {}
|
sys.exit("Could not read config file: %r\n %s" % (path, e))
|
||||||
|
self.cfg.pop("__builtins__")
|
||||||
|
|
||||||
config = {}
|
opts = [(k, v) for (k, v) in opts.iteritems() if v is not None]
|
||||||
try:
|
self.cfg.update(dict(opts))
|
||||||
execfile(self.config_file, globals(), config)
|
|
||||||
except:
|
|
||||||
sys.exit("Could not read config file %r" % (self.config_file,))
|
|
||||||
|
|
||||||
config.pop("__builtins__", None)
|
|
||||||
return config
|
|
||||||
|
|
||||||
def load(self):
|
|
||||||
self.conf = self.DEFAULTS.copy()
|
|
||||||
self.conf.update(self._load_file())
|
|
||||||
for key, value in list(self.cmdopts.items()):
|
|
||||||
if value and value is not None:
|
|
||||||
self.conf[key] = value
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
return getattr(self, key)
|
return getattr(self, key)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
return self.conf[key]
|
return self.cfg[key]
|
||||||
|
|
||||||
def __getattr__(self, key):
|
def __getattr__(self, key):
|
||||||
try:
|
try:
|
||||||
getattr(super(Config, self), key)
|
super(Config, self).__getattribute__(key)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
if key in self.conf:
|
if key in self.cfg:
|
||||||
return self.conf[key]
|
return self.cfg[key]
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
return (key in self.conf)
|
return (key in self.cfg)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.conf.iteritems()
|
return self.cfg.iteritems()
|
||||||
|
|
||||||
|
def get(self, key, default=None):
|
||||||
|
return self.cfg.get(key, default)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def worker_class(self):
|
def worker_class(self):
|
||||||
uri = self.conf.get('workertype', None) or 'egg:gunicorn#sync'
|
uri = self.cfg.get('workerclass', None) or 'egg:gunicorn#sync'
|
||||||
|
print uri
|
||||||
return util.load_worker_class(uri)
|
return util.load_worker_class(uri)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def workers(self):
|
def workers(self):
|
||||||
if not self.conf.get('workers'):
|
if not self.cfg.get('workers'):
|
||||||
raise RuntimeError("invalid workers number")
|
raise RuntimeError("invalid workers number")
|
||||||
workers = int(self.conf["workers"])
|
workers = int(self.cfg["workers"])
|
||||||
if not workers:
|
if not workers:
|
||||||
raise RuntimeError("number of workers < 1")
|
raise RuntimeError("number of workers < 1")
|
||||||
if self.conf['debug'] == True:
|
if self.cfg['debug'] == True:
|
||||||
workers = 1
|
workers = 1
|
||||||
return workers
|
return workers
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def address(self):
|
def address(self):
|
||||||
if not self.conf['bind']:
|
if not self.cfg['bind']:
|
||||||
raise RuntimeError("Listener address is not set")
|
raise RuntimeError("Listener address is not set")
|
||||||
return util.parse_address(util.to_bytestring(self.conf['bind']))
|
return util.parse_address(util.to_bytestring(self.cfg['bind']))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def umask(self):
|
def umask(self):
|
||||||
if not self.conf.get('umask'):
|
if not self.cfg.get('umask'):
|
||||||
return 0
|
return 0
|
||||||
umask = self.conf['umask']
|
umask = self.cfg['umask']
|
||||||
if isinstance(umask, basestring):
|
if isinstance(umask, basestring):
|
||||||
return int(umask, 0)
|
return int(umask, 0)
|
||||||
return umask
|
return umask
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def uid(self):
|
def uid(self):
|
||||||
if not self.conf.get('user'):
|
if not self.cfg.get('user'):
|
||||||
return os.geteuid()
|
return os.geteuid()
|
||||||
|
|
||||||
user = self.conf.get('user')
|
user = self.cfg.get('user')
|
||||||
if user.isdigit() or isinstance(user, int):
|
if user.isdigit() or isinstance(user, int):
|
||||||
uid = int(user)
|
uid = int(user)
|
||||||
else:
|
else:
|
||||||
@ -143,28 +128,20 @@ class Config(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def gid(self):
|
def gid(self):
|
||||||
if not self.conf.get('group'):
|
if not self.cfg.get('group'):
|
||||||
return os.getegid()
|
return os.getegid()
|
||||||
group = self.conf.get('group')
|
group = self.cfg.get('group')
|
||||||
if group.isdigit() or isinstance(group, int):
|
if group.isdigit() or isinstance(group, int):
|
||||||
gid = int(group)
|
gid = int(group)
|
||||||
else:
|
else:
|
||||||
gid = grp.getgrnam(group).gr_gid
|
gid = grp.getgrnam(group).gr_gid
|
||||||
|
|
||||||
return gid
|
return gid
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def proc_name(self):
|
def proc_name(self):
|
||||||
if not self.conf.get('proc_name'):
|
if not self.cfg.get('proc_name'):
|
||||||
return self.conf.get('default_proc_name')
|
return self.cfg.get('default_proc_name')
|
||||||
return self.conf.get('proc_name')
|
return self.cfg.get('proc_name')
|
||||||
|
|
||||||
def _hook(self, hookname, *args):
|
|
||||||
hook = self.conf.get(hookname)
|
|
||||||
if not hook: return
|
|
||||||
if not callable(hook):
|
|
||||||
raise RuntimeError("%s hook isn't a callable" % hookname)
|
|
||||||
return hook(*args)
|
|
||||||
|
|
||||||
def after_fork(self, *args):
|
def after_fork(self, *args):
|
||||||
return self._hook("after_fork", *args)
|
return self._hook("after_fork", *args)
|
||||||
@ -175,3 +152,10 @@ class Config(object):
|
|||||||
def before_exec(self, *args):
|
def before_exec(self, *args):
|
||||||
return self._hook("before_exec", *args)
|
return self._hook("before_exec", *args)
|
||||||
|
|
||||||
|
def _hook(self, hookname, *args):
|
||||||
|
hook = self.cfg.get(hookname)
|
||||||
|
if not hook:
|
||||||
|
return
|
||||||
|
if not callable(hook):
|
||||||
|
raise RuntimeError("%r hook isn't a callable" % hookname)
|
||||||
|
return hook(*args)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user