forked from tornadoweb/tornado
-
Notifications
You must be signed in to change notification settings - Fork 96
/
config.py
83 lines (66 loc) · 3 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# coding: utf-8
#
$license
import os
import sys
import ConfigParser
from cyclone.util import ObjectDict
def tryget(func, section, option, default=None):
try:
return func(section, option)
except ConfigParser.NoOptionError:
return default
def my_parse_config(filename):
cp = ConfigParser.RawConfigParser()
cp.read([filename])
conf = dict(raw=cp, config_file=filename)
# server settings
conf["debug"] = tryget(cp.getboolean, "server", "debug", False)
conf["xheaders"] = tryget(cp.getboolean, "server", "xheaders", False)
conf["cookie_secret"] = cp.get("server", "cookie_secret")
conf["xsrf_cookies"] = tryget(cp.getboolean,
"server", "xsrf_cookies", False)
# make relative path absolute to this file's parent directory
root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
getpath = lambda k, v: os.path.join(root, tryget(cp.get, k, v))
# locale, template and static directories
conf["locale_path"] = getpath("frontend", "locale_path")
conf["static_path"] = getpath("frontend", "static_path")
conf["template_path"] = getpath("frontend", "template_path")
# sqlite support
if tryget(cp.getboolean, "sqlite", "enabled", False) is True:
conf["sqlite_settings"] = \
ObjectDict(database=cp.get("sqlite", "database"))
# redis support
if tryget(cp.getboolean, "redis", "enabled", False) is True:
conf["redis_settings"] = ObjectDict(
unixsocket=tryget(cp.get, "redis", "unixsocket", None),
host=tryget(cp.get, "redis", "host", "127.0.0.1"),
port=tryget(cp.getint, "redis", "port", 6379),
dbid=tryget(cp.getint, "redis", "dbid", 0),
poolsize=tryget(cp.getint, "redis", "poolsize", 10))
# mysql support
if tryget(cp.getboolean, "mysql", "enabled", False) is True:
conf["mysql_settings"] = ObjectDict(
host=cp.get("mysql", "host"),
port=cp.getint("mysql", "port"),
username=tryget(cp.get, "mysql", "username"),
password=tryget(cp.get, "mysql", "password"),
database=tryget(cp.get, "mysql", "database"),
poolsize=tryget(cp.getint, "mysql", "poolsize", 10),
debug=tryget(cp.getboolean, "mysql", "debug", False))
# email support
if tryget(cp.getboolean, "email", "enabled", False) is True:
conf["email_settings"] = ObjectDict(
host=cp.get("email", "host"),
port=tryget(cp.getint, "email", "port"),
tls=tryget(cp.getboolean, "email", "tls"),
username=tryget(cp.get, "email", "username"),
password=tryget(cp.get, "email", "password"))
return conf
def parse_config(filename):
try:
return my_parse_config(filename)
except Exception, e:
print("Error parsing %s: %s" % (filename, e))
sys.exit(1)