本文整理汇总了Python中werkzeug._compat.to_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python to_unicode函数的具体用法?Python to_unicode怎么用?Python to_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了to_unicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parse_pairs
def _parse_pairs():
for key, val in _cookie_parse_impl(header):
key = to_unicode(key, charset, errors, allow_none_charset=True)
if not key:
continue
val = to_unicode(val, charset, errors, allow_none_charset=True)
yield try_coerce_native(key), val
示例2: peek_path_info
def peek_path_info(environ, charset='utf-8', errors='replace'):
"""Returns the next segment on the `PATH_INFO` or `None` if there
is none. Works like :func:`pop_path_info` without modifying the
environment:
>>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
>>> peek_path_info(env)
'a'
>>> peek_path_info(env)
'a'
If the `charset` is set to `None` a bytestring is returned.
.. versionadded:: 0.5
.. versionchanged:: 0.9
The path is now decoded and a charset and encoding
parameter can be provided.
:param environ: the WSGI environment that is checked.
"""
segments = environ.get('PATH_INFO', '').lstrip('/').split('/', 1)
if segments:
return to_unicode(wsgi_get_bytes(segments[0]),
charset, errors, allow_none_charset=True)
示例3: iri_to_uri
def iri_to_uri(iri, charset='utf-8', errors='strict'):
r"""
Converts any unicode based IRI to an acceptable ASCII URI. Werkzeug always
uses utf-8 URLs internally because this is what browsers and HTTP do as
well. In some places where it accepts an URL it also accepts a unicode IRI
and converts it into a URI.
Examples for IRI versus URI:
>>> iri_to_uri(u'http://☃.net/')
'http://xn--n3h.net/'
>>> iri_to_uri(u'http://üser:pä[email protected]☃.net/påth')
'http://%C3%BCser:p%C3%[email protected]/p%C3%A5th'
.. versionadded:: 0.6
:param iri: The IRI to convert.
:param charset: The charset for the URI.
"""
if isinstance(iri, tuple):
iri = url_unparse(iri)
iri = url_parse(to_unicode(iri, charset, errors))
netloc = iri.encode_netloc().decode('ascii')
path = url_quote(iri.path, charset, errors, '/:~+%')
query = url_quote(iri.query, charset, errors, '%&[]:;$*()+,!?*/=')
fragment = url_quote(iri.fragment, charset, errors, '=%&[]:;$()+,!?*/')
return to_native(url_unparse((iri.scheme, netloc,
path, query, fragment)))
示例4: url_fix
def url_fix(s, charset='utf-8'):
r"""Sometimes you get an URL by a user that just isn't a real URL because
it contains unsafe characters like ' ' and so on. This function can fix
some of the problems in a similar way browsers handle data entered by the
user:
>>> url_fix(u'http://de.wikipedia.org/wiki/Elf (Begriffskl\xe4rung)')
'http://de.wikipedia.org/wiki/Elf%20(Begriffskl%C3%A4rung)'
:param s: the string with the URL to fix.
:param charset: The target charset for the URL if the url was given as
unicode string.
"""
# First step is to switch to unicode processing and to convert
# backslashes (which are invalid in URLs anyways) to slashes. This is
# consistent with what Chrome does.
s = to_unicode(s, charset, 'replace').replace('\\', '/')
# For the specific case that we look like a malformed windows URL
# we want to fix this up manually:
if s.startswith('file://') and s[7:8].isalpha() and s[8:10] in (':/', '|/'):
s = 'file:///' + s[7:]
url = url_parse(s)
path = url_quote(url.path, charset, safe='/%+$!*\'(),')
qs = url_quote_plus(url.query, charset, safe=':&%=+$!*\'(),')
anchor = url_quote_plus(url.fragment, charset, safe=':&%=+$!*\'(),')
return to_native(url_unparse((url.scheme, url.encode_netloc(),
path, qs, anchor)))
示例5: uri_to_iri
def uri_to_iri(uri, charset='utf-8', errors='replace'):
r"""
Converts a URI in a given charset to a IRI.
Examples for URI versus IRI:
>>> uri_to_iri(b'http://xn--n3h.net/')
u'http://\u2603.net/'
>>> uri_to_iri(b'http://%C3%BCser:p%C3%[email protected]/p%C3%A5th')
u'http://\xfcser:p\[email protected]\u2603.net/p\xe5th'
Query strings are left unchanged:
>>> uri_to_iri('/?foo=24&x=%26%2f')
u'/?foo=24&x=%26%2f'
.. versionadded:: 0.6
:param uri: The URI to convert.
:param charset: The charset of the URI.
:param errors: The error handling on decode.
"""
if isinstance(uri, tuple):
uri = url_unparse(uri)
uri = url_parse(to_unicode(uri, charset))
path = url_unquote(uri.path, charset, errors, '/;?')
query = url_unquote(uri.query, charset, errors, ';/?:@&=+,$')
fragment = url_unquote(uri.fragment, charset, errors, ';/?:@&=+,$')
return url_unparse((uri.scheme, uri.decode_netloc(),
path, query, fragment))
示例6: __init__
def __init__(self, exc_type, exc_value, tb):
self.lineno = tb.tb_lineno
self.function_name = tb.tb_frame.f_code.co_name
self.locals = tb.tb_frame.f_locals
self.globals = tb.tb_frame.f_globals
fn = inspect.getsourcefile(tb) or inspect.getfile(tb)
if fn[-4:] in ('.pyo', '.pyc'):
fn = fn[:-1]
# if it's a file on the file system resolve the real filename.
if os.path.isfile(fn):
fn = os.path.realpath(fn)
self.filename = to_unicode(fn, get_filesystem_encoding())
self.module = self.globals.get('__name__')
self.loader = self.globals.get('__loader__')
self.code = tb.tb_frame.f_code
# support for paste's traceback extensions
self.hide = self.locals.get('__traceback_hide__', False)
info = self.locals.get('__traceback_info__')
if info is not None:
try:
info = text_type(info)
except UnicodeError:
info = str(info).decode('utf-8', 'replace')
self.info = info
示例7: serve_file
def serve_file(self, environ, start_response, endpoint, file_name=None):
if endpoint == 'root_file':
if not file_name:
file_name = 'index.html'
environ['PATH_INFO'] = environ['PATH_INFO'] + '/index.html'
elif file_name == 'admin.html':
file_name = 'builder.html'
if file_name == 'index.html':
self.check_modified(file_name, environ)
self.check_project_modified()
elif file_name == 'builder.html':
self.check_modified(os.path.join(to_unicode(self.jam_dir, 'utf-8'), file_name), environ)
environ['PATH_INFO'] = os.path.join('jam', file_name)
if file_name:
base, ext = os.path.splitext(file_name)
init_path_info = None
if common.SETTINGS['COMPRESSED_JS'] and ext and ext in ['.js', '.css']:
init_path_info = environ['PATH_INFO']
min_file_name = base + '.min' + ext
environ['PATH_INFO'] = environ['PATH_INFO'].replace(file_name, min_file_name)
try:
try:
return self.fileserver(environ, start_response)
except Exception as e:
if init_path_info:
environ['PATH_INFO'] = init_path_info
return self.fileserver(environ, start_response)
else:
raise
except Exception as e:
return Response('')(environ, start_response)
示例8: __call__
def __call__(self, *path, **query):
if path and isinstance(path[-1], dict):
if query:
raise TypeError("keyword arguments and query-dicts " "can't be combined")
query, path = path[-1], path[:-1]
elif query:
query = dict([(k.endswith("_") and k[:-1] or k, v) for k, v in query.items()])
path = "/".join([to_unicode(url_quote(x, self.charset), "ascii") for x in path if x is not None]).lstrip("/")
rv = self.base
if path:
if not rv.endswith("/"):
rv += "/"
rv = url_join(rv, "./" + path)
if query:
rv += "?" + to_unicode(url_encode(query, self.charset, sort=self.sort, key=self.key), "ascii")
return to_native(rv)
示例9: dump_cookie
def dump_cookie(key, value='', max_age=None, expires=None, path='/',
domain=None, secure=None, httponly=False, sync_expires=True):
"""Creates a new Set-Cookie header without the ``Set-Cookie`` prefix
The parameters are the same as in the cookie Morsel object in the
Python standard library but it accepts unicode data, too.
:param max_age: should be a number of seconds, or `None` (default) if
the cookie should last only as long as the client's
browser session. Additionally `timedelta` objects
are accepted, too.
:param expires: should be a `datetime` object or unix timestamp.
:param path: limits the cookie to a given path, per default it will
span the whole domain.
:param domain: Use this if you want to set a cross-domain cookie. For
example, ``domain=".example.com"`` will set a cookie
that is readable by the domain ``www.example.com``,
``foo.example.com`` etc. Otherwise, a cookie will only
be readable by the domain that set it.
:param secure: The cookie will only be available via HTTPS
:param httponly: disallow JavaScript to access the cookie. This is an
extension to the cookie standard and probably not
supported by all browsers.
:param charset: the encoding for unicode values.
:param sync_expires: automatically set expires if max_age is defined
but expires not.
"""
if not isinstance(key, (bytes, text_type)):
raise TypeError('invalid key %r' % key)
if not isinstance(value, (bytes, text_type)):
raise TypeError('invalid value %r' % value)
key, value = to_native(key, _cookie_charset), to_native(value, _cookie_charset)
value = quote_header_value(value)
morsel = _ExtendedMorsel(key, value)
if isinstance(max_age, timedelta):
max_age = (max_age.days * 60 * 60 * 24) + max_age.seconds
if expires is not None:
if not isinstance(expires, string_types):
expires = cookie_date(expires)
morsel['expires'] = expires
elif max_age is not None and sync_expires:
morsel['expires'] = cookie_date(time() + max_age)
if domain and ':' in domain:
# The port part of the domain should NOT be used. Strip it
domain = domain.split(':', 1)[0]
if domain:
assert '.' in domain, (
"Setting \"domain\" for a cookie on a server running localy (ex: "
"localhost) is not supportted by complying browsers. You should "
"have something like: \"127.0.0.1 localhost dev.localhost\" on "
"your hosts file and then point your server to run on "
"\"dev.localhost\" and also set \"domain\" for \"dev.localhost\""
)
for k, v in (('path', path), ('domain', domain), ('secure', secure),
('max-age', max_age), ('httponly', httponly)):
if v is not None and v is not False:
morsel[k] = str(v)
return to_unicode(morsel.output(header='').lstrip(), _cookie_charset)
示例10: full_path
def full_path(self):
"""
Werzueg's full_path implementation always appends '?', even when the
query string is empty. Let's fix that.
"""
if not self.query_string:
return self.path
return self.path + "?" + to_unicode(self.query_string, self.url_charset)
示例11: unquote
def unquote(cls, value):
if cls.quote_base64:
value = base64.b64decode(value)
### Added line
value = to_unicode(value, 'utf-8')
if cls.serialization_method is not None:
value = cls.serialization_method.loads(value)
return value
示例12: process_sql_result
def process_sql_result(rows):
result = []
for row in rows:
new_row = []
for r in row:
if isinstance(r, fdb.fbcore.BlobReader):
r = to_unicode(r.read(), 'utf-8')
new_row.append(r)
result.append(new_row)
return result
示例13: process_sql_result
def process_sql_result(rows):
result = []
for row in rows:
new_row = []
for r in row:
if isinstance(r, bytes):
r = to_unicode(r, 'utf-8')
new_row.append(r)
result.append(new_row)
return result
示例14: __call__
def __call__(self, *path, **query):
if path and isinstance(path[-1], dict):
if query:
raise TypeError('keyword arguments and query-dicts '
'can\'t be combined')
query, path = path[-1], path[:-1]
elif query:
query = dict([(k.endswith('_') and k[:-1] or k, v)
for k, v in query.items()])
path = '/'.join([to_unicode(url_quote(x, self.charset), 'ascii')
for x in path if x is not None]).lstrip('/')
rv = self.base
if path:
if not rv.endswith('/'):
rv += '/'
rv = url_join(rv, './' + path)
if query:
rv += '?' + to_unicode(url_encode(query, self.charset, sort=self.sort,
key=self.key), 'ascii')
return to_native(rv)
示例15: process_sql_result
def process_sql_result(rows):
result = []
for row in rows:
fields = []
for field in row:
if isinstance(field, cx_Oracle.LOB):
field = field.read()
field = to_unicode(field, 'utf-8')
fields.append(field)
result.append(fields)
return result