本文整理汇总了Python中pyramid.util.text_函数的典型用法代码示例。如果您正苦于以下问题:Python text_函数的具体用法?Python text_怎么用?Python text_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了text_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pattern_with_high_order_literal
def test_pattern_with_high_order_literal(self):
pattern = text_(b'/La Pe\xc3\xb1a/{x}', 'utf-8')
matcher, generator = self._callFUT(pattern)
self.assertEqual(
matcher(text_(b'/La Pe\xc3\xb1a/x', 'utf-8')), {'x': 'x'}
)
self.assertEqual(generator({'x': '1'}), '/La%20Pe%C3%B1a/1')
示例2: test_matcher_functional_oldstyle
def test_matcher_functional_oldstyle(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'
# pattern, path, expected
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', 'utf-8'),
{'traverse': (text_(b'La Pe\xc3\xb1a', 'utf-8'), 'x')},
)
self.matches('/foo/:id.html', '/foo/bar.html', {'id': 'bar'})
self.matches('/foo/:id_html', '/foo/bar_html', {'id_html': 'bar_html'})
self.matches('zzz/:_', '/zzz/abc', {'_': 'abc'})
self.matches('zzz/:_abc', '/zzz/abc', {'_abc': 'abc'})
self.matches('zzz/:abc_def', '/zzz/abc', {'abc_def': 'abc'})
示例3: test_absolute_unicode_found
def test_absolute_unicode_found(self):
# test for bug wiggy found in wild, traceback stack:
# root = '/%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)
示例4: test_generator_functional_oldstyle
def test_generator_functional_oldstyle(self):
self.generates('/:x', {'x': ''}, '/')
self.generates('/:x', {'x': 'a'}, '/a')
self.generates('zzz/:x', {'x': 'abc'}, '/zzz/abc')
self.generates(
'zzz/:x*traverse', {'x': 'abc', 'traverse': ''}, '/zzz/abc'
)
self.generates(
'zzz/:x*traverse',
{'x': 'abc', 'traverse': '/def/g'},
'/zzz/abc/def/g',
)
self.generates(
'/:x',
{'x': text_(b'/La Pe\xc3\xb1a', 'utf-8')},
'//La%20Pe%C3%B1a',
)
self.generates(
'/:x*y',
{'x': text_(b'/La Pe\xc3\xb1a', 'utf-8'), 'y': '/rest/of/path'},
'//La%20Pe%C3%B1a/rest/of/path',
)
self.generates(
'*traverse',
{'traverse': ('a', text_(b'La Pe\xf1a'))},
'/a/La%20Pe%C3%B1a',
)
self.generates('/foo/:id.html', {'id': 'bar'}, '/foo/bar.html')
self.generates('/foo/:_', {'_': '20'}, '/foo/20')
self.generates('/foo/:_abc', {'_abc': '20'}, '/foo/20')
self.generates('/foo/:abc_def', {'abc_def': '20'}, '/foo/20')
示例5: 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',
)
示例6: 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)
示例7: call_app_with_subpath_as_path_info
def call_app_with_subpath_as_path_info(request, app):
# Copy the request. Use the source request's subpath (if it exists) as
# the new request's PATH_INFO. Set the request copy's SCRIPT_NAME to the
# prefix before the subpath. Call the application with the new request
# and return a response.
#
# Postconditions:
# - SCRIPT_NAME and PATH_INFO are empty or start with /
# - At least one of SCRIPT_NAME or PATH_INFO are set.
# - SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should
# be '/').
environ = request.environ
script_name = environ.get('SCRIPT_NAME', '')
path_info = environ.get('PATH_INFO', '/')
subpath = list(getattr(request, 'subpath', ()))
new_script_name = ''
# compute new_path_info
new_path_info = '/' + '/'.join(
[text_(x.encode('utf-8'), 'latin-1') for x in subpath]
)
if new_path_info != '/': # don't want a sole double-slash
if path_info != '/': # if orig path_info is '/', we're already done
if path_info.endswith('/'):
# readd trailing slash stripped by subpath (traversal)
# conversion
new_path_info += '/'
# compute new_script_name
workback = (script_name + path_info).split('/')
tmp = []
while workback:
if tmp == subpath:
break
el = workback.pop()
if el:
tmp.insert(0, text_(bytes_(el, 'latin-1'), 'utf-8'))
# strip all trailing slashes from workback to avoid appending undue slashes
# to end of script_name
while workback and (workback[-1] == ''):
workback = workback[:-1]
new_script_name = '/'.join(workback)
new_request = request.copy()
new_request.environ['SCRIPT_NAME'] = new_script_name
new_request.environ['PATH_INFO'] = new_path_info
return new_request.get_response(app)
示例8: test_subpath_path_info_and_script_name_have_utf8
def test_subpath_path_info_and_script_name_have_utf8(self):
encoded = text_(b'La Pe\xc3\xb1a')
decoded = text_(bytes_(encoded), 'utf-8')
request = DummyRequest(
{'PATH_INFO': '/' + encoded, 'SCRIPT_NAME': '/' + encoded}
)
request.subpath = (decoded,)
response = self._callFUT(request, 'app')
self.assertTrue(request.copied)
self.assertEqual(response, 'app')
self.assertEqual(request.environ['SCRIPT_NAME'], '/' + encoded)
self.assertEqual(request.environ['PATH_INFO'], '/' + encoded)
示例9: test_add_route_with_path_info_regex
def test_add_route_with_path_info_regex(self):
config = self._makeOne(autocommit=True)
config.add_route(
'name', 'path', path_info=text_(br'/La Pe\w*', 'utf-8')
)
route = self._assertRoute(config, 'name', 'path', 1)
predicate = route.predicates[0]
request = self._makeRequest(config)
request.upath_info = text_(b'/La Pe\xc3\xb1a', 'utf-8')
self.assertEqual(predicate(None, request), True)
request = self._makeRequest(config)
request.upath_info = text_('/')
self.assertEqual(predicate(None, request), False)
示例10: test_call_withconn_getitem_withpath_withsubpath
def test_call_withconn_getitem_withpath_withsubpath(self):
foo = DummyContext()
root = DummyContext(foo)
policy = self._makeOne(root)
environ = self._getEnviron()
request = DummyRequest(environ, path_info=text_('/foo/bar/baz/buz'))
result = policy(request)
self.assertEqual(result['context'], foo)
self.assertEqual(result['view_name'], 'bar')
self.assertEqual(result['subpath'], ('baz', 'buz'))
self.assertEqual(result['traversed'], (text_('foo'),))
self.assertEqual(result['root'], root)
self.assertEqual(result['virtual_root'], root)
self.assertEqual(result['virtual_root_path'], ())
示例11: test_utf16
def test_utf16(self):
from pyramid.exceptions import URLDecodeError
la = text_(b'La Pe\xc3\xb1a', 'utf-8').encode('utf-16')
encoded = quote(la)
path = '/'.join([encoded, encoded])
self.assertRaises(URLDecodeError, self._callFUT, path)
示例12: test_utf8
def test_utf8(self):
la = b'La Pe\xc3\xb1a'
encoded = quote(la)
decoded = text_(la, 'utf-8')
path = '/'.join([encoded, encoded])
result = self._callFUT(path)
self.assertEqual(result, (decoded, decoded))
示例13: test_unicode
def test_unicode(self):
class DummyUnicodeObject(object):
def __unicode__(self):
return text_('42')
duo = DummyUnicodeObject()
self.assertEqual(self._callFUT(duo), text_('42'))
示例14: test_route_url
def test_route_url(self):
environ = {
'PATH_INFO': '/',
'SERVER_NAME': 'example.com',
'SERVER_PORT': '5432',
'QUERY_STRING': 'la=La%20Pe%C3%B1a',
'wsgi.url_scheme': 'http',
}
from pyramid.interfaces import IRoutesMapper
inst = self._makeOne(environ)
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3'))
self.config.registry.registerUtility(mapper, IRoutesMapper)
result = inst.route_url(
'flub',
'extra1',
'extra2',
a=1,
b=2,
c=3,
_query={'a': 1},
_anchor=text_("foo"),
)
self.assertEqual(
result, 'http://example.com:5432/1/2/3/extra1/extra2?a=1#foo'
)
示例15: _set_cookie
def _set_cookie(self, response):
if not self._cookie_on_exception:
exception = getattr(self.request, 'exception', None)
if (
exception is not None
): # dont set a cookie during exceptions
return False
cookieval = text_(
serializer.dumps((self.accessed, self.created, dict(self)))
)
if len(cookieval) > 4064:
raise ValueError(
'Cookie value is too long to store (%s bytes)'
% len(cookieval)
)
response.set_cookie(
self._cookie_name,
value=cookieval,
max_age=self._cookie_max_age,
path=self._cookie_path,
domain=self._cookie_domain,
secure=self._cookie_secure,
httponly=self._cookie_httponly,
samesite=self._cookie_samesite,
)
return True