本文整理汇总了Python中pyramid.compat.text_函数的典型用法代码示例。如果您正苦于以下问题:Python text_函数的具体用法?Python text_怎么用?Python text_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了text_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matcher_functional_newstyle
def test_matcher_functional_newstyle(self):
self.matches('/{x}', '', None)
self.matches('/{x}', '/', None)
self.matches('/abc/{def}', '/abc/', None)
self.matches('/{x}', '/a', {'x':'a'})
self.matches('zzz/{x}', '/zzz/abc', {'x':'abc'})
self.matches('zzz/{x}*traverse', '/zzz/abc', {'x':'abc', 'traverse':()})
self.matches('zzz/{x}*traverse', '/zzz/abc/def/g',
{'x':'abc', 'traverse':('def', 'g')})
self.matches('*traverse', '/zzz/abc', {'traverse':('zzz', 'abc')})
self.matches('*traverse', '/zzz/ abc', {'traverse':('zzz', ' abc')})
#'/La%20Pe%C3%B1a'
self.matches('{x}', text_(b'/La Pe\xc3\xb1a', 'utf-8'),
{'x':text_(b'La Pe\xc3\xb1a', 'utf-8')})
# '/La%20Pe%C3%B1a/x'
self.matches('*traverse', text_(b'/La Pe\xc3\xb1a/x'),
{'traverse':(text_(b'La Pe\xc3\xb1a'), 'x')})
self.matches('/foo/{id}.html', '/foo/bar.html', {'id':'bar'})
self.matches('/{num:[0-9]+}/*traverse', '/555/abc/def',
{'num':'555', 'traverse':('abc', 'def')})
self.matches('/{num:[0-9]*}/*traverse', '/555/abc/def',
{'num':'555', 'traverse':('abc', 'def')})
self.matches('zzz/{_}', '/zzz/abc', {'_':'abc'})
self.matches('zzz/{_abc}', '/zzz/abc', {'_abc':'abc'})
self.matches('zzz/{abc_def}', '/zzz/abc', {'abc_def':'abc'})
示例2: test_testing_resources
def test_testing_resources(self):
from pyramid.traversal import find_resource
from pyramid.interfaces import ITraverser
ob1 = object()
ob2 = object()
resources = {'/ob1':ob1, '/ob2':ob2}
config = self._makeOne(autocommit=True)
config.testing_resources(resources)
adapter = config.registry.getAdapter(None, ITraverser)
result = adapter(DummyRequest({'PATH_INFO':'/ob1'}))
self.assertEqual(result['context'], ob1)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(result['traversed'], (text_('ob1'),))
self.assertEqual(result['virtual_root'], ob1)
self.assertEqual(result['virtual_root_path'], ())
result = adapter(DummyRequest({'PATH_INFO':'/ob2'}))
self.assertEqual(result['context'], ob2)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(result['traversed'], (text_('ob2'),))
self.assertEqual(result['virtual_root'], ob2)
self.assertEqual(result['virtual_root_path'], ())
self.assertRaises(KeyError, adapter, DummyRequest({'PATH_INFO':'/ob3'}))
try:
config.begin()
self.assertEqual(find_resource(None, '/ob1'), ob1)
finally:
config.end()
示例3: test_resource_url_anchor_is_encoded_utf8_if_unicode
def test_resource_url_anchor_is_encoded_utf8_if_unicode(self):
request = self._makeOne()
self._registerResourceURL(request.registry)
context = DummyContext()
uc = text_(b"La Pe\xc3\xb1a", "utf-8")
result = request.resource_url(context, anchor=uc)
self.assertEqual(result, native_(text_(b"http://example.com:5432/context/#La Pe\xc3\xb1a", "utf-8"), "utf-8"))
示例4: test_absolute_unicode_found
def test_absolute_unicode_found(self):
# test for bug wiggy found in wild, traceback stack:
# root = u'/%E6%B5%81%E8%A1%8C%E8%B6%8B%E5%8A%BF'
# wiggy's code: section=find_resource(page, root)
# find_resource L76: D = traverse(resource, path)
# traverse L291: return traverser(request)
# __call__ line 568: vpath_tuple = traversal_path(vpath)
# lru_cached line 91: f(*arg)
# traversal_path line 443: path.encode('ascii')
# UnicodeEncodeError: 'ascii' codec can't encode characters in
# position 1-12: ordinal not in range(128)
#
# solution: encode string to ascii in pyramid.traversal.traverse
# before passing it along to webob as path_info
from pyramid.traversal import ResourceTreeTraverser
unprintable = DummyContext()
root = DummyContext(unprintable)
unprintable.__parent__ = root
unprintable.__name__ = text_(
b'/\xe6\xb5\x81\xe8\xa1\x8c\xe8\xb6\x8b\xe5\x8a\xbf', 'utf-8')
root.__parent__ = None
root.__name__ = None
traverser = ResourceTreeTraverser
self._registerTraverser(traverser)
result = self._callFUT(
root,
text_(b'/%E6%B5%81%E8%A1%8C%E8%B6%8B%E5%8A%BF')
)
self.assertEqual(result, unprintable)
示例5: test_it
def test_it(self):
class Dummy:
pass
ob1 = Dummy()
ob2 = Dummy()
resources = {'/ob1':ob1, '/ob2':ob2}
from pyramid import testing
testing.registerResources(resources)
from pyramid.interfaces import ITraverser
adapter = self.registry.getAdapter(None, ITraverser)
result = adapter(DummyRequest({'PATH_INFO':'/ob1'}))
self.assertEqual(result['context'], ob1)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(result['traversed'], (text_('ob1'),))
self.assertEqual(result['virtual_root'], ob1)
self.assertEqual(result['virtual_root_path'], ())
result = adapter(DummyRequest({'PATH_INFO':'/ob2'}))
self.assertEqual(result['context'], ob2)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(result['traversed'], (text_('ob2'),))
self.assertEqual(result['virtual_root'], ob2)
self.assertEqual(result['virtual_root_path'], ())
self.assertRaises(KeyError, adapter, DummyRequest({'PATH_INFO':'/ob3'}))
from pyramid.traversal import find_resource
self.assertEqual(find_resource(None, '/ob1'), ob1)
示例6: test_call_with_vh_root_highorder
def test_call_with_vh_root_highorder(self):
bar = DummyContext(None, 'bar')
foo = DummyContext(bar, text_(b'Qu\xc3\xa9bec', 'utf-8'))
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
if PY3: # pragma: no cover
vhm_root = b'/Qu\xc3\xa9bec'.decode('latin-1')
else:
vhm_root = b'/Qu\xc3\xa9bec'
environ = self._getEnviron(HTTP_X_VHM_ROOT=vhm_root,
PATH_INFO='/bar')
request = DummyRequest(environ)
result = policy(request)
self.assertEqual(result['context'], bar)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(
result['traversed'],
(text_(b'Qu\xc3\xa9bec', 'utf-8'), text_('bar'))
)
self.assertEqual(result['root'], policy.root)
self.assertEqual(result['virtual_root'], foo)
self.assertEqual(
result['virtual_root_path'],
(text_(b'Qu\xc3\xa9bec', 'utf-8'),)
)
示例7: test_docs_sample_generate
def test_docs_sample_generate(self):
# sample from urldispatch.rst
pattern = text_(b'/La Pe\xc3\xb1a/{city}', 'utf-8')
_, generator = self._callFUT(pattern)
self.assertEqual(
generator({'city':text_(b'Qu\xc3\xa9bec', 'utf-8')}),
'/La%20Pe%C3%B1a/Qu%C3%A9bec')
示例8: response
def response(self):
return Response(
body=self.gf.read(),
content_disposition='attachment; filename={}'.format(
text_(self.gf.filename, encoding='utf8')),
content_type=text_(self.gf.content_type, encoding='utf8') if \
self.gf.content_type else 'binary/octet-stream',
content_length=self.gf.length
)
示例9: test___call__traverse_matches_with_highorder_chars
def test___call__traverse_matches_with_highorder_chars(self):
inst = self._makeOne(text_(b"/La Pe\xc3\xb1a/{x}", "utf-8"))
info = {"match": {"x": text_(b"Qu\xc3\xa9bec", "utf-8")}}
request = Dummy()
result = inst(info, request)
self.assertEqual(result, True)
self.assertEqual(
info["match"]["traverse"], (text_(b"La Pe\xc3\xb1a", "utf-8"), text_(b"Qu\xc3\xa9bec", "utf-8"))
)
示例10: test_generate_with_string_remainder_and_unicode_replacement
def test_generate_with_string_remainder_and_unicode_replacement(self):
pattern = text_(b'/abc*remainder', 'utf-8')
_, generator = self._callFUT(pattern)
result = generator(
{'remainder': text_(b'/Qu\xc3\xa9bec/La Pe\xc3\xb1a', 'utf-8')}
)
self.assertEqual(result, '/abc/Qu%C3%A9bec/La%20Pe%C3%B1a')
# should be a native string
self.assertEqual(type(result), str)
示例11: test_route_url_with_anchor_unicode
def test_route_url_with_anchor_unicode(self):
from pyramid.interfaces import IRoutesMapper
request = self._makeOne()
mapper = DummyRoutesMapper(route=DummyRoute("/1/2/3"))
request.registry.registerUtility(mapper, IRoutesMapper)
anchor = text_(b"La Pe\xc3\xb1a", "utf-8")
result = request.route_url("flub", _anchor=anchor)
self.assertEqual(result, native_(text_(b"http://example.com:5432/1/2/3#La Pe\xc3\xb1a", "utf-8"), "utf-8"))
示例12: object_description
def object_description(object):
""" Produce a human-consumable text description of ``object``,
usually involving a Python dotted name. For example:
>>> object_description(None)
u'None'
>>> from xml.dom import minidom
>>> object_description(minidom)
u'module xml.dom.minidom'
>>> object_description(minidom.Attr)
u'class xml.dom.minidom.Attr'
>>> object_description(minidom.Attr.appendChild)
u'method appendChild of class xml.dom.minidom.Attr'
If this method cannot identify the type of the object, a generic
description ala ``object <object.__name__>`` will be returned.
If the object passed is already a string, it is simply returned. If it
is a boolean, an integer, a list, a tuple, a set, or ``None``, a
(possibly shortened) string representation is returned.
"""
if isinstance(object, string_types):
return text_(object)
if isinstance(object, integer_types):
return text_(str(object))
if isinstance(object, (bool, float, type(None))):
return text_(str(object))
if isinstance(object, set):
if PY3: # pragma: no cover
return shortrepr(object, '}')
else:
return shortrepr(object, ')')
if isinstance(object, tuple):
return shortrepr(object, ')')
if isinstance(object, list):
return shortrepr(object, ']')
if isinstance(object, dict):
return shortrepr(object, '}')
module = inspect.getmodule(object)
if module is None:
return text_('object %s' % str(object))
modulename = module.__name__
if inspect.ismodule(object):
return text_('module %s' % modulename)
if inspect.ismethod(object):
oself = getattr(object, '__self__', None)
if oself is None: # pragma: no cover
oself = getattr(object, 'im_self', None)
return text_('method %s of class %s.%s' %
(object.__name__, modulename,
oself.__class__.__name__))
if inspect.isclass(object):
dottedname = '%s.%s' % (modulename, object.__name__)
return text_('class %s' % dottedname)
if inspect.isfunction(object):
dottedname = '%s.%s' % (modulename, object.__name__)
return text_('function %s' % dottedname)
return text_('object %s' % str(object))
示例13: test___call__traverse_matches_with_highorder_chars
def test___call__traverse_matches_with_highorder_chars(self):
inst = self._makeOne(text_(b'/La Pe\xc3\xb1a/{x}', 'utf-8'))
info = {'match':{'x':text_(b'Qu\xc3\xa9bec', 'utf-8')}}
request = Dummy()
result = inst(info, request)
self.assertEqual(result, True)
self.assertEqual(
info['match']['traverse'],
(text_(b'La Pe\xc3\xb1a', 'utf-8'),
text_(b'Qu\xc3\xa9bec', 'utf-8'))
)
示例14: test_add_mako_renderer
def test_add_mako_renderer(self):
from pyramid.renderers import render
self.config.add_settings({'foo.directories':
'pyramid_mako.tests:fixtures'})
self.config.add_mako_renderer('.foo', settings_prefix='foo.')
result = render('nonminimal.foo',
{'name': '<b>fred</b>'}).replace('\r', '')
self.assertEqual(result, text_('Hello, <b>fred</b>!\n'))
result = render('fixtures/nonminimal.mak',
{'name': '<b>fred</b>'}).replace('\r', '')
self.assertEqual(result, text_('Hello, <b>fred</b>!\n'))
示例15: test_messages_error_msg
def test_messages_error_msg(self):
add_message(self.request, 'error', 'error')
self.assertEqual(
render_messages(self.request).strip(),
text_('<div class="alert alert-error alert-danger">\n <a class="close" data-dismiss="alert">×</a>\n error\n</div>','utf-8'))
add_message(self.request, ValueError('Error'), 'error')
self.assertEqual(
render_messages(self.request).strip(),
text_('<div class="alert alert-error alert-danger">\n <a class="close" data-dismiss="alert">×</a>\n ValueError: Error\n</div>','utf-8'))