本文整理匯總了Python中webob.acceptparse.Accept.best_match方法的典型用法代碼示例。如果您正苦於以下問題:Python Accept.best_match方法的具體用法?Python Accept.best_match怎麽用?Python Accept.best_match使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類webob.acceptparse.Accept
的用法示例。
在下文中一共展示了Accept.best_match方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_best_match
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match():
accept = Accept("text/html, foo/bar")
assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
assert accept.best_match(["foo/bar", "text/html"]) == "foo/bar"
assert accept.best_match([("foo/bar", 0.5), "text/html"]) == "text/html"
assert accept.best_match([("foo/bar", 0.5), ("text/html", 0.4)]) == "foo/bar"
assert_raises(ValueError, accept.best_match, ["text/*"])
示例2: test_best_match_with_complex_q
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match_with_complex_q():
accept = Accept('text/html, foo/bar;q=0.55, baz/gort;q=0.59')
assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
accept = Accept('text/html;q=0.5, foo/bar;q=0.586, baz/gort;q=0.5966')
assert "baz/gort;q=0.597" in str(accept)
assert "foo/bar;q=0.586" in str(accept)
assert "text/html;q=0.5" in str(accept)
assert accept.best_match(['text/html', 'baz/gort']) == 'baz/gort'
示例3: test_best_match
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match():
accept = Accept('Content-Type', 'text/html, foo/bar')
assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
assert accept.best_match([('foo/bar', 0.5),
'text/html']) == 'text/html'
assert accept.best_match([('foo/bar', 0.5),
('text/html', 0.4)]) == 'foo/bar'
assert_raises(ValueError, accept.best_match, ['text/*'])
示例4: test_best_match
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match():
accept = Accept('text/html, foo/bar')
assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
assert accept.best_match([('foo/bar', 0.5),
'text/html']) == 'text/html'
assert accept.best_match([('foo/bar', 0.5),
('text/html', 0.4)]) == 'foo/bar'
with pytest.raises(ValueError):
accept.best_match(['text/*'])
示例5: generate_response
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def generate_response(self, environ, start_response):
if self.content_length is not None:
del self.content_length
headerlist = list(self.headerlist)
accept_value = environ.get('HTTP_ACCEPT', '')
accept = Accept(accept_value)
match = accept.best_match(['application/json', 'text/html',
'text/plain'], default_match='text/plain')
if match == 'text/html':
content_type = 'text/html'
body = self.html_body(environ)
elif match == 'application/json':
content_type = 'application/json'
body = self.json_body(environ)
else:
content_type = 'text/plain'
body = self.plain_body(environ)
extra_kw = {}
if isinstance(body, text_type):
extra_kw.update(charset='utf-8')
resp = Response(body,
status=self.status,
headerlist=headerlist,
content_type=content_type,
**extra_kw
)
resp.content_type = content_type
return resp(environ, start_response)
示例6: concept
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def concept(request, lccn):
concept = get_object_or_404(m.Concept, lccn=lccn)
host = request.get_host()
accept = Accept('Accept', request.META.get('HTTP_ACCEPT', 'text/html'))
best_match = accept.best_match(['text/html', 'application/rdf+xml'])
if best_match == 'application/rdf+xml':
r = concept_rdf(request, lccn)
else:
alt_labels = concept.alternate_labels.all().order_by('text')
broader = concept.broader.all().order_by('pref_label')
extra_broader = concept.extra_broader()
narrower = concept.narrower.all().order_by('pref_label')
related = concept.related.all().order_by('pref_label')
r = render_to_response('concept.html', dictionary=locals(),
context_instance=RequestContext(request))
# let downstream caches know that the resonse can vary depending
# on the Accept header that the client sent
r['Vary'] = 'Accept'
return r
示例7: checkMimetype
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def checkMimetype(acceptHeader):
accept = Accept('Accept', acceptHeader)
best = accept.best_match(['application/rdf', 'application/rdf+xml', 'text/n3', 'application/xml', 'application/json', 'text/xml', 'application/xhtml+xml'])
if best is None:
best = "text/html"
return best
示例8: test_best_match_with_one_lower_q
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match_with_one_lower_q():
accept = Accept('Content-Type', 'text/html, foo/bar;q=0.5')
assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
accept = Accept('Content-Type', 'text/html;q=0.5, foo/bar')
assert accept.best_match(['text/html', 'foo/bar']) == 'foo/bar'
示例9: WebController
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
#.........這裏部分代碼省略.........
return self._session_key
def delete_cookie(self, key):
self.__response.delete_cookie(key)
def set_cookie(self, key):
value = self.new_session
self.__response.set_cookie(key, bytes(value))
return value
def process(self, environ, start_response):
self._begin = datetime.datetime.now()
if self._debug:
open("/tmp/cog_sql", "w")
try:
self.clear()
self._environ = environ
# open("/tmp/cog_tmp_xxx", "w").write("{}".format(environ))
self._script_name = self._environ.get('SCRIPT_NAME', '')
self._server_name = self._environ['HTTP_HOST']
self._url_scheme = self._environ['wsgi.url_scheme']
self._url = "{}://{}{}".format(
self._url_scheme, self._server_name, self._script_name)
self._path_info = self._environ['PATH_INFO'].lower()
alnum = "[a-z0-9]{4}"
oid_pattern = '^/{}{}-{}-{}-{}-{}{}{}$'.format(*(alnum,)*8)
if re.match(oid_pattern, self._path_info):
obj = self.db.get_elt_by_oid(self._path_info[1:])
self.add_json_res({'#page_ref':self.get_page_ref()})
self.direct_obj_ref = obj.w3display
self.load_site()
self.__request = Request(environ)
self.__lang = Accept(str(self.__request.accept_language))
self.i18n = gettext.translation(
'messages', '/usr/share/collorg/locale',
[self.__lang.best_match(('en', 'fr', 'fr-FR')) or 'en'])
if WebController.__static_body is None:
WebController.__tags = self.__get_tags()
WebController.__static_body = self.__get_static_body()
self.__response = Response()
self.__reset()
if not(self._cog_method is None and '#page_ref' in self._json_res):
self.add_json_res({'#page_ref':self.get_page_ref()})
self.__response.unicode_body = self._unicode(self.__exec())
if(self.__response.content_type != 'text/html' or
self.__response.status_int != 200):
# Not an HTML response, we don't want to
# do anything to it
return self.__response(environ, start_response)
# Make sure the content isn't gzipped:
self.__response.decode_content()
return self.__response(environ, start_response)
except Exception as err:
if self.db._cog_params['debug']:
response = Response()
response.unicode_body = self._unicode(err)
return response(environ, start_response)
if raise_error:
raise err
def debug(self, error = None):
output = []
css_style = 'debug hidden toggle'
css_error = ''
if error:
css_style += ' debug_error'
css_error = 'debug_error'
title = error and "Error" or "debug"
link = ( '<span class="link toggle %s" '
'to_toggle="#cog_debug">%s</span>' % (css_error, title))
if error:
output.append('<h2>Error</h2>')
output.append('<pre>%s</pre>' % (self.__pfce(error)))
output.append("<h2>Traceback</h2>")
output.append("<pre>%s</pre>"%(
traceback.format_exc()))
output.append('<h2>Main variables</h2>')
output.append('<pre>')
output.append('cog_method = %s\n' % (self._cog_method))
output.append('cog_fqtn_ = %s\n' % (self._cog_fqtn_))
output.append('cog_oid_ = %s\n' % (self._cog_oid_))
output.append('cog_ref_oid = %s\n' % (self._cog_ref_oid))
output.append('</pre>')
if error:
post_error = self.db.table(
'collorg.application.communication.error')
try:
post_error.hit(
self._cog_fqtn_, self._cog_method, traceback.format_exc())
except Exception, err:
sys.stderr.write("Warning! {}\n".format(err))
# open("/tmp/cog_error_log", "a+").write("{}\n".format(err))
output.append('<h2>Query string</h2>')
output.append('<pre>qs = %s</pre>' % (self.__pfce(self.__get_request)))
output.append('<h2>Command executed</h2>')
output.append('<pre>cmd = %s</pre>' %(self.__pfce(self._cog_cmd)))
output.append('<h2>Environ</h2>')
output.append('<pre>%s</pre>' % (self.__pfce(self._environ)))
u_output = [self._unicode(elt) for elt in output]
return link, "\n".join(u_output)
示例10: test_best_match_with_one_lower_q
# 需要導入模塊: from webob.acceptparse import Accept [as 別名]
# 或者: from webob.acceptparse.Accept import best_match [as 別名]
def test_best_match_with_one_lower_q():
accept = Accept("text/html, foo/bar;q=0.5")
assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
accept = Accept("text/html;q=0.5, foo/bar")
assert accept.best_match(["text/html", "foo/bar"]) == "foo/bar"