def encode_method_value(value):
if isinstance(value, basestring):
value = value.replace("'", "''")
# Same replacements as SQL Server
# https://web.archive.org/web/20150101222238/http://msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx
# https://stackoverflow.com/questions/4229054/how-are-special-characters-handled-in-an-odata-query#answer-45883747
value = value.replace('%', '%25')
value = value.replace('+', '%2B')
value = value.replace('/', '%2F')
value = value.replace('?', '%3F')
value = value.replace('#', '%23')
value = value.replace('&', '%26')
value = "'{0}'".format(value)
elif isinstance(value, bool):
value = str(value).lower()
return value
python类basestring()的实例源码
def test_callaction_param_mashal_out(self, mock_post):
"""
Values should be marshalled into the appropriate Python data types.
"""
ret = mock.Mock()
ret.text = """
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntryResponse xmlns:u="urn:schemas-upnp-org:service:Layer3Forwarding:1">
<NewInternalClient>10.0.0.1</NewInternalClient>
<NewExternalPort>51773</NewExternalPort>
<NewEnabled>true</NewEnabled>
</u:GetGenericPortMappingEntryResponse>
</s:Body>
</s:Envelope>
"""
mock_post.return_value = ret
action = self.server.find_action('GetGenericPortMappingEntry')
response = action(NewPortMappingIndex=0)
self.assertIsInstance(response['NewInternalClient'], basestring)
self.assertIsInstance(response['NewExternalPort'], int)
self.assertIsInstance(response['NewEnabled'], bool)
def timestamp_parameter(timestamp, allow_none=True):
if timestamp is None:
if allow_none:
return None
raise ValueError("Timestamp value cannot be None")
if isinstance(timestamp, datetime):
return timestamp.isoformat()
if isinstance(timestamp, basestring):
if not ISO_8601.match(timestamp):
raise ValueError(("Invalid timestamp: %s is not a valid ISO-8601"
" formatted date") % timestamp)
return timestamp
raise ValueError("Cannot accept type %s for timestamp" % type(timestamp))
def _dump_request_data(request, prefixes, bytearr, proxy_info=None):
if proxy_info is None:
proxy_info = {}
prefix = prefixes.request
method = _coerce_to_bytes(proxy_info.pop('method', request.method))
request_path, uri = _build_request_path(request.url, proxy_info)
# <prefix><METHOD> <request-path> HTTP/1.1
bytearr.extend(prefix + method + b' ' + request_path + b' HTTP/1.1\r\n')
# <prefix>Host: <request-host> OR host header specified by user
headers = request.headers.copy()
host_header = _coerce_to_bytes(headers.pop('Host', uri.netloc))
bytearr.extend(prefix + b'Host: ' + host_header + b'\r\n')
for name, value in headers.items():
bytearr.extend(prefix + _format_header(name, value))
bytearr.extend(prefix + b'\r\n')
if request.body:
if isinstance(request.body, compat.basestring):
bytearr.extend(prefix + _coerce_to_bytes(request.body))
else:
# In the event that the body is a file-like object, let's not try
# to read everything into memory.
bytearr.extend('<< Request body is not a string-like type >>')
bytearr.extend(b'\r\n')
def _dump_request_data(request, prefixes, bytearr, proxy_info=None):
if proxy_info is None:
proxy_info = {}
prefix = prefixes.request
method = _coerce_to_bytes(proxy_info.pop('method', request.method))
request_path, uri = _build_request_path(request.url, proxy_info)
# <prefix><METHOD> <request-path> HTTP/1.1
bytearr.extend(prefix + method + b' ' + request_path + b' HTTP/1.1\r\n')
# <prefix>Host: <request-host> OR host header specified by user
headers = request.headers.copy()
host_header = _coerce_to_bytes(headers.pop('Host', uri.netloc))
bytearr.extend(prefix + b'Host: ' + host_header + b'\r\n')
for name, value in headers.items():
bytearr.extend(prefix + _format_header(name, value))
bytearr.extend(prefix + b'\r\n')
if request.body:
if isinstance(request.body, compat.basestring):
bytearr.extend(prefix + _coerce_to_bytes(request.body))
else:
# In the event that the body is a file-like object, let's not try
# to read everything into memory.
bytearr.extend('<< Request body is not a string-like type >>')
bytearr.extend(b'\r\n')
def _dump_request_data(request, prefixes, bytearr, proxy_info=None):
if proxy_info is None:
proxy_info = {}
prefix = prefixes.request
method = _coerce_to_bytes(proxy_info.pop('method', request.method))
request_path, uri = _build_request_path(request.url, proxy_info)
# <prefix><METHOD> <request-path> HTTP/1.1
bytearr.extend(prefix + method + b' ' + request_path + b' HTTP/1.1\r\n')
# <prefix>Host: <request-host> OR host header specified by user
headers = request.headers.copy()
host_header = _coerce_to_bytes(headers.pop('Host', uri.netloc))
bytearr.extend(prefix + b'Host: ' + host_header + b'\r\n')
for name, value in headers.items():
bytearr.extend(prefix + _format_header(name, value))
bytearr.extend(prefix + b'\r\n')
if request.body:
if isinstance(request.body, compat.basestring):
bytearr.extend(prefix + _coerce_to_bytes(request.body))
else:
# In the event that the body is a file-like object, let's not try
# to read everything into memory.
bytearr.extend('<< Request body is not a string-like type >>')
bytearr.extend(b'\r\n')
def timestamp_parameter(timestamp, allow_none=True):
"""Function to check the conformance of timestamps passed by users.
This will check that a string is a valid format and allow users to pass a
datetime object which we will then convert to a proper ISO8601 date-time
string.
:param timestamp: string to be validated or datetime object to be
converted.
:param bool allow_none: whether or not to allow timestamp to be None.
Default: ``True``
:returns: valid ISO8601 string
:rtype: str
:raises: ValueError
"""
if timestamp is None:
if allow_none:
return None
raise ValueError("Timestamp value cannot be None")
if isinstance(timestamp, datetime.datetime):
return timestamp.isoformat() + 'Z'
if isinstance(timestamp, compat.basestring):
if not ISO_8601.match(timestamp):
raise ValueError(("Invalid timestamp: %s is not a valid ISO-8601"
" formatted date") % timestamp)
return timestamp
raise ValueError("Cannot accept type %s for timestamp" % type(timestamp))