mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
app: separate json_encode() func
compat: py2 fixes
This commit is contained in:
parent
c45f5cb749
commit
94d6098238
@ -128,7 +128,7 @@ class BaseWARCWriter(object):
|
|||||||
warcinfo.seek(0)
|
warcinfo.seek(0)
|
||||||
|
|
||||||
record = ArcWarcRecord('warc', 'warcinfo', headers, warcinfo,
|
record = ArcWarcRecord('warc', 'warcinfo', headers, warcinfo,
|
||||||
None, '', len(warcinfo.getbuffer()))
|
None, '', len(warcinfo.getvalue()))
|
||||||
|
|
||||||
return record
|
return record
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -38,7 +38,7 @@ setup(
|
|||||||
'pywb>=0.30.0',
|
'pywb>=0.30.0',
|
||||||
],
|
],
|
||||||
dependency_links=[
|
dependency_links=[
|
||||||
'git+https://github.com/ikreymer/pywb.git@develop#egg=pywb-0.30.0-develop',
|
#'git+https://github.com/ikreymer/pywb.git@develop#egg=pywb-0.30.0-develop',
|
||||||
],
|
],
|
||||||
zip_safe=True,
|
zip_safe=True,
|
||||||
entry_points="""
|
entry_points="""
|
||||||
|
@ -6,6 +6,7 @@ import traceback
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from six.moves.urllib.parse import parse_qsl
|
from six.moves.urllib.parse import parse_qsl
|
||||||
|
import six
|
||||||
|
|
||||||
JSON_CT = 'application/json; charset=utf-8'
|
JSON_CT = 'application/json; charset=utf-8'
|
||||||
|
|
||||||
@ -66,31 +67,32 @@ class ResAggApp(object):
|
|||||||
|
|
||||||
out_headers, res, errs = result
|
out_headers, res, errs = result
|
||||||
|
|
||||||
if res:
|
if not res:
|
||||||
if isinstance(res, dict):
|
return self.send_error(errs, start_response)
|
||||||
res = json.dumps(res).encode('utf-8')
|
|
||||||
out_headers['Content-Type'] = JSON_CT
|
|
||||||
out_headers['Content-Length'] = str(len(res))
|
|
||||||
res = [res]
|
|
||||||
|
|
||||||
if errs:
|
if isinstance(res, dict):
|
||||||
out_headers['ResErrors'] = json.dumps(errs)
|
res = self.json_encode(res, out_headers)
|
||||||
|
|
||||||
start_response('200 OK', list(out_headers.items()))
|
if errs:
|
||||||
return res
|
out_headers['ResErrors'] = json.dumps(errs)
|
||||||
|
|
||||||
else:
|
start_response('200 OK', list(out_headers.items()))
|
||||||
return self.send_error(out_headers, errs, start_response)
|
return res
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
message = 'Internal Error: ' + str(e)
|
message = 'Internal Error: ' + str(e)
|
||||||
status = 500
|
status = 500
|
||||||
return self.send_error({}, {}, start_response,
|
return self.send_error({}, start_response,
|
||||||
message=message,
|
message=message,
|
||||||
status=status)
|
status=status)
|
||||||
|
|
||||||
|
def json_encode(self, res, out_headers):
|
||||||
|
res = json.dumps(res).encode('utf-8')
|
||||||
|
out_headers['Content-Type'] = JSON_CT
|
||||||
|
out_headers['Content-Length'] = str(len(res))
|
||||||
|
return [res]
|
||||||
|
|
||||||
def send_error(self, out_headers, errs, start_response,
|
def send_error(self, errs, start_response,
|
||||||
message='No Resource Found', status=404):
|
message='No Resource Found', status=404):
|
||||||
last_exc = errs.pop('last_exc', None)
|
last_exc = errs.pop('last_exc', None)
|
||||||
if last_exc:
|
if last_exc:
|
||||||
@ -104,12 +106,15 @@ class ResAggApp(object):
|
|||||||
if errs:
|
if errs:
|
||||||
res['errors'] = errs
|
res['errors'] = errs
|
||||||
|
|
||||||
err_msg = json.dumps(res)
|
out_headers = {}
|
||||||
|
res = self.json_encode(res, out_headers)
|
||||||
|
|
||||||
headers = [('Content-Type', JSON_CT),
|
if six.PY3:
|
||||||
('Content-Length', str(len(err_msg))),
|
out_headers['ResErrors'] = res[0].decode('utf-8')
|
||||||
('ResErrors', err_msg)
|
else:
|
||||||
]
|
out_headers['ResErrors'] = res[0]
|
||||||
|
message = message.encode('utf-8')
|
||||||
|
|
||||||
start_response(str(status) + ' ' + message, headers)
|
message = str(status) + ' ' + message
|
||||||
return [err_msg.encode('utf-8')]
|
start_response(message, list(out_headers.items()))
|
||||||
|
return res
|
||||||
|
@ -6,6 +6,8 @@ from pywb.utils.wbexception import NotFoundException
|
|||||||
from pywb.cdx.query import CDXQuery
|
from pywb.cdx.query import CDXQuery
|
||||||
from pywb.cdx.cdxdomainspecific import load_domain_specific_cdx_rules
|
from pywb.cdx.cdxdomainspecific import load_domain_specific_cdx_rules
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
#=============================================================================
|
#=============================================================================
|
||||||
def to_cdxj(cdx_iter, fields):
|
def to_cdxj(cdx_iter, fields):
|
||||||
@ -112,11 +114,11 @@ class IndexHandler(object):
|
|||||||
content_type, res = handler(cdx_iter, fields)
|
content_type, res = handler(cdx_iter, fields)
|
||||||
out_headers = {'Content-Type': content_type}
|
out_headers = {'Content-Type': content_type}
|
||||||
|
|
||||||
def check_str(res):
|
def check_str(lines):
|
||||||
for x in res:
|
for line in lines:
|
||||||
if isinstance(x, str):
|
if isinstance(line, six.text_type):
|
||||||
x = x.encode('utf-8')
|
line = line.encode('utf-8')
|
||||||
yield x
|
yield line
|
||||||
|
|
||||||
return out_headers, check_str(res), errs
|
return out_headers, check_str(res), errs
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user