-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.py
225 lines (197 loc) · 6.39 KB
/
settings.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
#
# Author: jimin.huang
#
# Created Time: 2015年09月03日 星期四 14时26分03秒
#
'''
There are three types of settings: ``debug``, ``run``, ``deploy``.
Change the settings in the conf file of each type and suppose the type whe-
n running.
Suppose the type of settings
----------------------------
Suppose the type of settings in a command line::
python server.py --config=debug
Debug mode
-----------------------------
When in debug mode:
* Server autoreload when changes detected in the source code.
* The level of ``logging`` is set to ``DEBUG``, default is ``INFO``
Defined settings
-----------------------------
================= ============================ ======== ===========
Name Usage Group Default
================= ============================ ======== ===========
port The port listened by server basic 8888
name Server name basic my_homepage
debug Debug mode basic False
xsrf_cookies Xsrf protection basic True
static_path Path of static files app CONSTANT
template_path Path of frontend templates app CONSTANT
ui_modules Frontend modules app FILE
login_url Url of login page app '/login'
cookie_secret Cookie secret app random gen-
eration
database_type Database type database mysql
database_address Database address database 127.0.0.1
database_port Database port database 3306
database_user Database user database admin
database_password Database password database my_homepage
mail_type Mail server type mail smtp
mail_server Mail server address mail 127.0.0.1
mail_port Mail server port mail 375
mail_user Mail user mail noreply
mail_password Mail password mail my_noreply
alert_list Send alert mails to mail \[\]
================= ============================ ======== ===========
Methods
-----------------------------
'''
import os
import base64
import uuid
import logging
from tornado.options import define, options, parse_command_line
from tornado.options import parse_config_file
from tornado.log import enable_pretty_logging
from modules import get_ui_modules
# TODO: Settings load from environments
# TODO: Logging settings which create loggers seperately for mail and database
# and allow to send mail or HTTP requests
# TODO: Test of load_settings
# TODO: Define settings of alert mails
# Path Defined
ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(ROOT, 'static')
TEMPLATE_ROOT = os.path.join(ROOT, 'template')
LOG_ROOT = os.path.join(ROOT, 'log')
# Define settings
# It's so long since someone don't hope you modify settings here
define("config", help="The config file of Tornado", group="basic")
define("port", default=8888, help="The port listened by server", group="basic")
define("name", default='my_homepage', help="Server name", group="basic")
define("debug", default=False, help="Debug mode", group="basic")
define("xsrf_cookies", default=True, help="Xsrf protection", group="basic")
define(
"static_path",
default=STATIC_ROOT,
help="The path of static files",
group="application"
)
define(
"template_path",
default=TEMPLATE_ROOT,
help="The path of frontend templates",
group="application"
)
define(
"ui_modules",
default=get_ui_modules(),
help="The frontend modules",
group="application"
)
define(
"login_url",
default='/login',
help="Url of login page",
group="application"
)
define(
"cookie_secret",
default=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes),
help="Cookie secret",
group="application"
)
define(
"database_type",
default='mysql',
help="The type of database adopted",
group="database"
)
define(
"database_address",
default='127.0.0.1',
help="The address of database",
group="database"
)
define(
"database_port",
default=3306,
help="The port of database",
group="database"
)
define(
"database_user",
default='admin',
help="The user of database",
group="database"
)
define(
"database_password",
default='my_homepage',
help="The password of database",
group="database"
)
define(
"mail_type",
default='smtp',
help="The type of mail server",
group="mail"
)
define(
"mail_server",
default='127.0.0.1',
help="The mail server",
group="mail"
)
define(
"mail_port",
default='375',
help="The port of mail server",
group="mail"
)
define(
"mail_user",
default='noreply',
help="The user of mail server",
group="mail"
)
define(
"mail_password",
default='my_noreply',
help="The password of mail server",
group="mail"
)
define(
"alert_list",
multiple=True,
default=[],
help="The receive list of alert mails", group="mail"
)
def load_settings():
'''
Load settings from command line and config file.
'''
# Log file prefix
options.log_file_prefix = LOG_ROOT + '/' + options.name + '.log'
# Parse command line
options.logging = 'none' # To turn off logging settings
parse_command_line()
# Load settings from another config file if given
if options.config:
parse_config_file(options.config + '.conf')
# Logging settings
if not options.logging: # There are no logging settings before
options.logging = 'debug' if options.debug else 'info'
enable_pretty_logging(options=options)
# Output settings
pretty_str = pretty_settings_logging()
logging.info(pretty_str)
print pretty_str
def pretty_settings_logging():
'''
Output a pretty format server settings.
'''
return '\n'.join(
[key+' : '+str(value) for key, value in options.as_dict().iteritems()]
)