-
Notifications
You must be signed in to change notification settings - Fork 3
/
dwconfig.py
231 lines (207 loc) · 7.3 KB
/
dwconfig.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
226
227
228
229
230
231
#
# Configuration handling that's specific to dwiki.
import os
import time
# My hate is burning department of hate.
# FIXME: there should be a better way than this.
__pychecker__ = "no-shadowbuiltin no-local"
from optparse import OptionParser
__pychecker__ = ""
import config, derrors
import storage, model, htmlview, httpcore, wsgicore
# We modify the configuration object somewhat to add more requirements.
# In particular, we need 'rooturl'.
isPosInt = ('blog-display-howmany', 'atomfeed-display-howmany',
'feed-max-size', 'render-heuristic-ttl', 'bfc-cache-ttl',
'bfc-atom-ttl', 'bfc-atom-nocond-ttl',
'imc-cache-entries', 'imc-cache-ttl', 'imc-resp-max-size',)
isPosFloat = ('bfc-time-min', 'bfc-time-triv', 'bfc-load-min',
'slow-requests-by', )
timeFmts = ('%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M',
'%Y-%m-%d', '%Y/%m/%d',)
class SimpleDWConf(config.Configuration):
wordlist_items = ( "bad-robots", "banned-robots", "bfc-skip-robots",
"literal-words", )
list_items = ('atomfeed-virt-only-in', 'atomfeed-virt-only-adv',
'canon-hosts', )
ip_ranges = ('banned-ips', 'banned-comment-ips',
'feed-max-size-ips', 'bad-robot-ips', )
def __init__(self):
super(SimpleDWConf, self).__init__()
self.must_exist.append("rooturl")
def setDefault(self, cv, val):
if cv not in self:
self[cv] = val
# True if the value is an integer timestamp or a timestamp in
# Atom timestamp format.
def isIntOrTimestr(self, what):
self.hasvalue(what)
try:
res = int(self[what])
if res < 0:
raise derrors.CfgErr("for setting %s: value is negative: %d" % (what, res))
self[what] = res
return
except ValueError:
pass
for tf in timeFmts:
try :
r = time.strptime(self[what], tf)
self[what] = time.mktime(r)
return
except ValueError:
pass
raise derrors.CfgErr("for setting %s: value is neither an integer not a recognizable timestamp: '%s'" % (what, self[what]))
def checkGoodConfig(self):
super(SimpleDWConf, self).checkGoodConfig()
if 'staticurl' in self:
self.hasvalue('staticurl')
self.isabsdir("staticdir")
if 'staticurl' in self:
if self['staticurl'][0] != '/':
self['staticurl'] = os.path.join(self['rooturl'], self['staticurl'])
if 'comments-on' in self:
self.isabsdir("commentsdir")
for cv in isPosInt:
if cv in self:
self.isposint(cv)
for cv in isPosFloat:
if cv in self:
self.isposfloat(cv)
# feed-max-size is in kilobytes, so multiply to get true
# bytes, unless it looks like we've already multiplied.
if 'feed-max-size' in self and \
self['feed-max-size'] < 2048:
self['feed-max-size'] = self['feed-max-size'] * 1024
if 'feed-max-size-ips' in self:
self.hasvalue('feed-max-size')
# This is somewhat of a hack.
if 'global-authseed-file' in self:
self.loadAuthSeed()
if 'remap-normal-to-showcomments' in self:
self['comments-in-normal'] = True
if 'bfc-cache-ttl' in self:
self.isabsdir('cachedir')
self.setDefault('bfc-time-min', 0.75)
self.setDefault('bfc-time-triv', 0.09)
if 'render-cache' in self:
self.isabsdir('cachedir')
if 'imc-resp-max-size' in self and \
self['imc-resp-max-size'] < 4096:
self['imc-resp-max-size'] = self['imx-resp-max-size'] * 1024
if 'imc-cache-entries' in self:
self.hasvalue('imc-cache-ttl')
self.setDefault('imc-resp-max-size', 256*1024)
if 'feed-start-time' in self and \
not isinstance(self['feed-start-time'], int):
self.isIntOrTimestr('feed-start-time')
# GORY HACK ALERT
if 'atomfeed-tag-time' in self and \
not isinstance(self['atomfeed-tag-time'], int):
self.isIntOrTimestr('atomfeed-tag-time')
# canonicalize all wordlist cases.
for lst in self.wordlist_items:
if lst not in self:
continue
self.hasvalue(lst)
v = self[lst]
if not isinstance(v, list):
self[lst] = [x.strip() for x in v.split(" | ")]
# ditto for the simpler list_items:
for lst in self.list_items:
if lst not in self:
continue
self.hasvalue(lst)
v = self[lst]
if not isinstance(v, list):
self[lst] = v.split()
def loadAuthSeed(self):
fname = self['global-authseed-file']
try:
fp = open(fname, 'rb')
self['global-authseed'] = fp.read()
fp.close()
except EnvironmentError as e:
raise derrors.CfgErr("cannot read global-authseed-file '%s': %s" % (fname, str(e)))
def setup_options(usage, version):
parser = OptionParser(usage=usage, version=version)
parser.add_option('-T', '--dump-times', dest="dumptime",
action="store_true", help="dump request times")
parser.add_option('-A', '--dump-atom', dest="dumpatom",
action="store_true",
help="dump Atom conditional get information")
parser.add_option('-C', '--config-option', dest='extraconfig',
action="append", metavar="OPT",
help="add/override a config option, with OPT in the form NAME:VALUE or just NAME.")
parser.add_option('-D', '--delete-option', dest='rmconfig',
action='append', metavar="OPT",
help="remove OPT from the configuration.")
parser.add_option('', '--no-cache', dest='noCache',
action="store_true", help="disable any caches.")
parser.add_option('', '--no-brute', dest="noBruteCache",
action="store_true", help="disable the BFC and the IMC.")
parser.add_option('', '--stamp', dest='stampMsgs',
action="store_true",
help="add timestamp and originator IP address to messages from -T and -A.")
parser.set_defaults(dumptime=False, dumpatom=False, extraconfig = [],
rmconfig = [], noCache = False,
noBruteCache = False, stampMsgs = False)
return parser
def handle_options(cfg, options):
# hack alert:
if options.dumptime:
cfg['dump-req-times'] = True
if options.dumpatom:
cfg['dump-atom-reqs'] = True
if options.stampMsgs:
cfg['stamp-messages'] = True
# Bonus hack for -C
if options.extraconfig:
for os in options.extraconfig:
if ':' not in os:
cfg[os] = True
else:
on, ov = os.split(':', 1)
cfg[on] = ov
# Ditto for -D, which is at least simpler.
if options.rmconfig:
for os in options.rmconfig:
cfg.rm(os)
# Forcefully disable caching.
if options.noCache:
cfg.rm("cachedir")
cfg.rm("render-cache")
cfg.rm("bfc-cache-ttl")
cfg.rm("imc-cache-entries")
if options.noBruteCache:
cfg.rm("bfc-cache-ttl")
cfg.rm("imc-cache-entries")
# We rely on being able to rerun checkGoodConfig(), which may
# take some work in the underlying system.
cfg.checkGoodConfig()
# We might as well do this centrally too, since everyone duplicates
# the code anyways.
def genR(root):
return {'dirroot': root}
def materialize(cfgfile, options, stype = 'unset'):
cfg = config.load(cfgfile, SimpleDWConf())
# allow callers to skip generating options if they want.
# this is convenient for pure WSGI applications
if options:
handle_options(cfg, options)
# Record WSGI server type in the configuration for later use
# in some circumstances.
cfg['wsgi-server-type'] = stype
ms = model.Model(cfg)
webs = htmlview.WebServices(cfg, ms)
staticstore = None
cachestore = None
if 'staticurl' in cfg:
staticstore = storage.StoragePool(genR(cfg['staticdir']))
if 'cachedir' in cfg:
cachestore = storage.CacheStoragePool(genR(cfg['cachedir']))
# Create the WSGI application.
procfunc = wsgicore.genWSGITop(httpcore.genDwikiStack(cfg),
cfg, ms, webs, staticstore,
cachestore)
return procfunc, (cfg, ms, webs, staticstore, cachestore)