def notebook_proxy(request, username):
"""Renders a IPython Notebook frame wrapper.
Starts or reattachs ot an existing Notebook session.
"""
security_check(request, username)
manager = get_notebook_manager(request)
notebook_info = manager.get_context(username)
if not notebook_info:
raise HTTPInternalServerError("Apparently IPython Notebook daemon process is not running for {}".format(username))
if not "http_port" in notebook_info:
raise RuntimeError("Notebook terminated prematurely before managed to tell us its HTTP port")
return proxy_it(request, notebook_info["http_port"])
python类HTTPInternalServerError()的实例源码
def __call__(self, info):
"""
If a schema is present, replace value with output from schema.dump(..).
"""
original_render = super().__call__(info)
def schema_render(value, system):
request = system.get('request')
if (request is not None and isinstance(getattr(request, 'render_schema', None), Schema)):
try:
value, errors = request.render_schema.dump(value)
except Exception:
errors = True
if errors:
raise HTTPInternalServerError(body="Serialization failed.")
return original_render(value, system)
return schema_render
def get_app(config):
""" return a pyramid wsgi app with various urls. """
def index(request):
return Response('idx')
def error(request):
raise HTTPInternalServerError("oh no")
def exception(request):
1 / 0
def json(request):
return {'a': 1}
def renderer(request):
return render_to_response('template.pt', {'foo': 'bar'}, request=request)
config.add_route('index', '/')
config.add_route('error', '/error')
config.add_route('exception', '/exception')
config.add_route('json', '/json')
config.add_route('renderer', '/renderer')
config.add_view(index, route_name='index')
config.add_view(error, route_name='error')
config.add_view(exception, route_name='exception')
config.add_view(json, route_name='json', renderer='json')
config.add_view(renderer, route_name='renderer', renderer='template.pt')
return config.make_wsgi_app()
def topic_matching_service(request):
parsed = urlparse(request.path_qs)
question_words = parse_qsl(parsed.query)
domain = request.matchdict.get('domain').replace(' ', '_')
log.info('Topic matching on domain {}'.format(domain))
tm = TopicMatcher(domain, request.registry.settings['MODELERLOCATION'], logger=log)
data = tm.topic_matching(question_words)
log.info('Topic matching completed')
if not data:
raise exc.HTTPInternalServerError(explanation="Topic matching failed. " +
"A model has not been generated for the given taxonomy.")
return data
def data_set_ranking_service(request):
domain = request.matchdict.get('domain')
analytic_type = request.matchdict.get('analytic_type')
interpretation = request.matchdict.get('interpretation')
log.info('Ranking data sets with\ndomain: {}\nanalytic type: {}\ninterpretation: {}'.format(domain, analytic_type, interpretation))
dm = DataMatcher(request.registry.settings['MODELERLOCATION'])
ranking = dm.dataSetRanking(domain, analytic_type, interpretation)
log.info('Data set ranking completed')
log.info(ranking)
if not ranking:
raise exc.HTTPInternalServerError(explanation="There were no data sets found in the database.")
return ranking
def viz_ranking_service(request):
log.info('Ranking vizes')
parsed = urlparse(request.path_qs)
question_analysis = parse_qs(parsed.query)
dm = DataMatcher(request.registry.settings['MODELERLOCATION'])
ranking = dm.vizRanking(question_analysis)
log.info('Viz ranking completed')
log.info(ranking)
if not ranking[0]['viz_ranks']:
raise exc.HTTPInternalServerError(explanation="There were no visualizations found in the database.")
return ranking
def __getitem__(self, name):
try:
resource = self.get(name)
except KeyError:
# Just in case we get an unexpected KeyError
# FIXME: exception logging.
raise HTTPInternalServerError('Traversal raised KeyError')
if resource is None:
raise KeyError(name)
return resource
def __getitem__(self, name):
try:
item = self.get(name)
except KeyError:
# Just in case we get an unexpected KeyError
# FIXME: exception logging.
raise HTTPInternalServerError('Traversal raised KeyError')
if item is None:
raise KeyError(name)
return item