/
pywb_init.py
387 lines (281 loc) · 12.3 KB
/
pywb_init.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from pywb.utils.loaders import load_yaml_config
from pywb.framework.archivalrouter import ArchivalRouter, Route
from pywb.framework.proxy import ProxyArchivalRouter
from pywb.framework.wbrequestresponse import WbRequest
from pywb.framework.memento import MementoRequest
from pywb.framework.basehandlers import BaseHandler
from pywb.webapp.views import J2TemplateView
from pywb.webapp.views import J2HtmlCapturesView, init_view
from pywb.webapp.live_rewrite_handler import RewriteHandler
from pywb.webapp.query_handler import QueryHandler
from pywb.webapp.handlers import WBHandler
from pywb.webapp.handlers import StaticHandler
from pywb.webapp.handlers import DebugEchoHandler, DebugEchoEnvHandler
from pywb.webapp.cdx_api_handler import CDXAPIHandler
from pywb import DEFAULT_CONFIG
import os
import logging
import six
#=================================================================
class DictChain(object):
def __init__(self, *dicts):
self.dicts = dicts
def get(self, key, default_val=None):
for d in self.dicts:
val = d.get(key)
if val is not None:
return val
return default_val
def __contains__(self, key):
return self.get(key) is not None
def __getitem__(self, key):
return self.get(key)
def __setitem__(self, key, value):
self.dicts[0][key] = value
#=================================================================
def create_wb_handler(query_handler, config):
wb_handler_class = config.get('wb_handler_class', WBHandler)
wb_handler = wb_handler_class(
query_handler,
config=config,
)
return wb_handler
#=================================================================
def create_live_handler(config):
wb_handler_class = config.get('wb_handler_class', RewriteHandler)
live_handler = wb_handler_class(config)
return live_handler
#=================================================================
def init_route_config(value, config):
if isinstance(value, str) or isinstance(value, list):
value = dict(index_paths=value)
route_config = DictChain(value, config)
return route_config
#=================================================================
def init_collection(route_config):
ds_rules_file = route_config.get('domain_specific_rules', None)
html_view = init_view(route_config, 'query_html', J2HtmlCapturesView)
server_cls = route_config.get('server_cls')
query_handler = QueryHandler.init_from_config(route_config,
ds_rules_file,
html_view,
server_cls)
return query_handler
#=================================================================
def add_cdx_api_handler(name, cdx_api_suffix, routes, query_handler,
route_class=Route):
# if bool, use -cdx suffix, else use custom string
# as the suffix
if isinstance(cdx_api_suffix, bool):
name += '-cdx'
else:
name += str(cdx_api_suffix)
logging.debug('Adding CDX API Handler: ' + name)
routes.append(route_class(name, CDXAPIHandler(query_handler)))
#=================================================================
def create_cdx_server_app(passed_config):
"""
Create a cdx server api-only app
For each collection, create a /<coll>-cdx access point
which follows the cdx api
"""
defaults = load_yaml_config(DEFAULT_CONFIG)
config = DictChain(passed_config, defaults)
collections = config.get('collections', {})
static_routes = {}
# collections based on file system
if config.get('enable_auto_colls', True):
colls_loader_cls = config.get('colls_loader_cls', DirectoryCollsLoader)
dir_loader = colls_loader_cls(config, static_routes, collections)
dir_loader()
#collections.update(dir_loader())
routes = []
for name, value in six.iteritems(collections):
route_config = init_route_config(value, config)
query_handler = init_collection(route_config)
cdx_api_suffix = route_config.get('enable_cdx_api', True)
add_cdx_api_handler(name, cdx_api_suffix, routes, query_handler)
return ArchivalRouter(routes)
#=================================================================
class DirectoryCollsLoader(object):
def __init__(self, config, static_routes, colls):
self.config = config
self.static_routes = static_routes
self.colls = colls
def __call__(self):
colls = self.colls
static_dir = self.config.get('paths')['static_path']
static_shared_prefix = self.config.get('static_shared_prefix')
if static_dir and static_shared_prefix and os.path.isdir(static_dir):
static_dir = os.path.abspath(static_dir) + os.path.sep
self.static_routes[static_shared_prefix] = static_dir
root_dir = self.config.get('collections_root', '')
if not root_dir or not os.path.isdir(root_dir):
return colls
for name in os.listdir(root_dir):
full = os.path.join(root_dir, name)
if not os.path.isdir(full):
continue
coll_config = self.load_coll_dir(full, name)
if coll_config:
# if already exists, override existing config with coll specific
if name in colls:
colls[name].update(coll_config)
else:
colls[name] = coll_config
return colls
def _norm_path(self, root_dir, path):
result = os.path.normpath(os.path.join(root_dir, path))
return result
def _add_dir_if_exists(self, coll, root_dir, dir_key, required=False):
curr_val = coll.get(dir_key)
if curr_val:
# add collection path only if relative path, and not a url
if '://' not in curr_val and not os.path.isabs(curr_val):
coll[dir_key] = self._norm_path(root_dir, curr_val) + os.path.sep
return False
thedir = self.config.get('paths')[dir_key]
fulldir = os.path.join(root_dir, thedir)
if os.path.isdir(fulldir):
fulldir = os.path.abspath(fulldir) + os.path.sep
coll[dir_key] = fulldir
return True
elif required:
msg = 'Dir "{0}" does not exist for "{1}"'.format(fulldir, dir_key)
raise Exception(msg)
else:
return False
def load_yaml_file(self, root_dir, filename):
filename = os.path.join(root_dir, filename)
if os.path.isfile(filename):
return load_yaml_config(filename)
else:
return {}
def load_coll_dir(self, root_dir, name):
# Load config.yaml
coll_config = self.load_yaml_file(root_dir, 'config.yaml')
# Load metadata.yaml
metadata = self.load_yaml_file(root_dir, 'metadata.yaml')
coll_config['metadata'] = metadata
self._add_dir_if_exists(coll_config, root_dir, 'index_paths', True)
# inherit these properties from base, in case archive_paths is shared
shared_config = DictChain(coll_config, self.config)
self._add_dir_if_exists(shared_config, root_dir, 'archive_paths', True)
if self._add_dir_if_exists(coll_config, root_dir, 'static_path', False):
self.static_routes['static/' + name] = coll_config['static_path']
# Custom templates dir
templates_dir = self.config.get('paths').get('templates_dir')
if templates_dir:
template_dir = os.path.join(root_dir, templates_dir)
# Check all templates
template_files = self.config.get('paths')['template_files']
for tname, tfile in six.iteritems(template_files):
if tname in coll_config:
# Already set
coll_config[tname] = self._norm_path(root_dir, coll_config[tname])
# If templates override dir
elif templates_dir:
full = os.path.join(template_dir, tfile)
if os.path.isfile(full):
coll_config[tname] = full
return coll_config
#=================================================================
def create_wb_router(passed_config=None):
passed_config = passed_config or {}
defaults = load_yaml_config(DEFAULT_CONFIG)
config = DictChain(passed_config, defaults)
routes = []
port = config.get('port')
collections = config.get('collections', {})
static_routes = config.get('static_routes', {})
root_route = None
# collections based on file system
if config.get('enable_auto_colls', True):
colls_loader_cls = config.get('colls_loader_cls', DirectoryCollsLoader)
dir_loader = colls_loader_cls(config, static_routes, collections)
dir_loader()
#collections.update(dir_loader())
if config.get('enable_memento', False):
request_class = MementoRequest
else:
request_class = WbRequest
# store live and replay handlers
handler_dict = {}
# setup template globals
templates_dirs = config['templates_dirs']
jinja_env = J2TemplateView.init_shared_env(paths=templates_dirs,
packages=config['template_packages'])
jinja_env.globals.update(config.get('template_globals', {}))
for static_name, static_path in six.iteritems(static_routes):
routes.append(Route(static_name, StaticHandler(static_path)))
for name, value in six.iteritems(collections):
if isinstance(value, BaseHandler):
handler_dict[name] = value
new_route = Route(name, value, config=config)
if name != '':
routes.append(new_route)
else:
root_route = new_route
continue
route_config = init_route_config(value, config)
route_class = route_config.get('route_class', Route)
if route_config.get('index_paths') == '$liveweb':
live = create_live_handler(route_config)
handler_dict[name] = live
new_route = route_class(name, live, config=route_config)
if name != '':
routes.append(new_route)
else:
root_route = new_route
continue
query_handler = init_collection(route_config)
wb_handler = create_wb_handler(
query_handler=query_handler,
config=route_config,
)
handler_dict[name] = wb_handler
logging.debug('Adding Collection: ' + name)
new_route = route_class(name, wb_handler,
config=route_config,
request_class=request_class)
if name != '':
routes.append(new_route)
else:
root_route = new_route
# cdx query handler
cdx_api_suffix = route_config.get('enable_cdx_api', False)
if cdx_api_suffix:
add_cdx_api_handler(name, cdx_api_suffix, routes, query_handler,
route_class=route_class)
if config.get('debug_echo_env', False):
routes.append(Route('echo_env', DebugEchoEnvHandler()))
if config.get('debug_echo_req', False):
routes.append(Route('echo_req', DebugEchoHandler()))
if root_route:
routes.append(root_route)
# resolve any cross handler references
for route in routes:
if hasattr(route.handler, 'resolve_refs'):
route.handler.resolve_refs(handler_dict)
# default to regular archival mode
router = ArchivalRouter
if config.get('enable_http_proxy', False):
router = ProxyArchivalRouter
view = init_view(config, 'proxy_select_html')
if 'proxy_options' not in passed_config:
passed_config['proxy_options'] = {}
if view:
passed_config['proxy_options']['proxy_select_view'] = view
view = init_view(config, 'proxy_cert_download_html')
if view:
passed_config['proxy_options']['proxy_cert_download_view'] = view
# Finally, create wb router
return router(
routes,
port=port,
abs_path=config.get('absolute_paths', True),
home_view=init_view(config, 'home_html'),
error_view=init_view(config, 'error_html'),
info_view=init_view(config, 'info_json'),
config=config
)