comparison requests/_internal_utils.py @ 7:5eb2d5e3bf22

planemo upload for repository https://toolrepo.galaxytrakr.org/view/jpayne/bioproject_to_srr_2/556cac4fb538
author jpayne
date Sun, 05 May 2024 23:32:17 -0400
parents
children
comparison
equal deleted inserted replaced
6:b2745907b1eb 7:5eb2d5e3bf22
1 """
2 requests._internal_utils
3 ~~~~~~~~~~~~~~
4
5 Provides utility functions that are consumed internally by Requests
6 which depend on extremely few external helpers (such as compat)
7 """
8 import re
9
10 from .compat import builtin_str
11
12 _VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$")
13 _VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$")
14 _VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$")
15 _VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$")
16
17 _HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
18 _HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
19 HEADER_VALIDATORS = {
20 bytes: _HEADER_VALIDATORS_BYTE,
21 str: _HEADER_VALIDATORS_STR,
22 }
23
24
25 def to_native_string(string, encoding="ascii"):
26 """Given a string object, regardless of type, returns a representation of
27 that string in the native string type, encoding and decoding where
28 necessary. This assumes ASCII unless told otherwise.
29 """
30 if isinstance(string, builtin_str):
31 out = string
32 else:
33 out = string.decode(encoding)
34
35 return out
36
37
38 def unicode_is_ascii(u_string):
39 """Determine if unicode string only contains ASCII characters.
40
41 :param str u_string: unicode string to check. Must be unicode
42 and not Python 2 `str`.
43 :rtype: bool
44 """
45 assert isinstance(u_string, str)
46 try:
47 u_string.encode("ascii")
48 return True
49 except UnicodeEncodeError:
50 return False