/usr/lib/python2.7/dist-packages/azure/storage/table/_serialization.py is in python-azure-storage 0.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | #-------------------------------------------------------------------------
# Copyright (c) Microsoft. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#--------------------------------------------------------------------------
import sys
import types
import uuid
from datetime import datetime
from json import (
dumps,
)
from math import(
isnan,
)
from .._common_conversion import (
_encode_base64,
_to_str,
)
from .._serialization import (
_to_utc_datetime,
)
from ._error import (
_ERROR_CANNOT_SERIALIZE_VALUE_TO_ENTITY,
_ERROR_TYPE_NOT_SUPPORTED,
_ERROR_VALUE_TOO_LARGE,
)
from .models import (
EntityProperty,
TablePayloadFormat,
EdmType,
)
if sys.version_info < (3,):
def _new_boundary():
return str(uuid.uuid1())
else:
def _new_boundary():
return str(uuid.uuid1()).encode('utf-8')
_DEFAULT_ACCEPT_HEADER = ('Accept', TablePayloadFormat.JSON_MINIMAL_METADATA)
_DEFAULT_CONTENT_TYPE_HEADER = ('Content-Type', 'application/json')
_DEFAULT_PREFER_HEADER = ('Prefer', 'return-no-content')
_SUB_HEADERS = ['If-Match', 'Prefer', 'Accept', 'Content-Type', 'DataServiceVersion']
def _get_entity_path(table_name, partition_key, row_key):
return '/{0}(PartitionKey=\'{1}\',RowKey=\'{2}\')'.format(
_to_str(table_name),
_to_str(partition_key),
_to_str(row_key))
def _update_storage_table_header(request):
''' add additional headers for storage table request. '''
# set service version
request.headers['DataServiceVersion'] = '3.0;NetFx'
request.headers['MaxDataServiceVersion'] = '3.0'
def _to_entity_binary(value):
return EdmType.BINARY, _encode_base64(value)
def _to_entity_bool(value):
return None, value
def _to_entity_datetime(value):
return EdmType.DATETIME, _to_utc_datetime(value)
def _to_entity_float(value):
if isnan(value):
return EdmType.DOUBLE, 'NaN'
if value == float('inf'):
return EdmType.DOUBLE, 'Infinity'
if value == float('-inf'):
return EdmType.DOUBLE, '-Infinity'
return None, value
def _to_entity_guid(value):
return EdmType.GUID, str(value)
def _to_entity_int32(value):
if sys.version_info < (3,):
value = long(value)
else:
value = int(value)
if value >= 2**31 or value < -(2**31):
raise TypeError(_ERROR_VALUE_TOO_LARGE.format(str(value), EdmType.INT32))
return None, value
def _to_entity_int64(value):
if sys.version_info < (3,):
ivalue = long(value)
else:
ivalue = int(value)
if ivalue >= 2**63 or ivalue < -(2**63):
raise TypeError(_ERROR_VALUE_TOO_LARGE.format(str(value), EdmType.INT64))
return EdmType.INT64, str(value)
def _to_entity_str(value):
return None, value
def _to_entity_none(value):
return None, None
# Conversion from Python type to a function which returns a tuple of the
# type string and content string.
_PYTHON_TO_ENTITY_CONVERSIONS = {
int: _to_entity_int64,
bool: _to_entity_bool,
datetime: _to_entity_datetime,
float: _to_entity_float,
str: _to_entity_str,
}
# Conversion from Edm type to a function which returns a tuple of the
# type string and content string.
_EDM_TO_ENTITY_CONVERSIONS = {
EdmType.BINARY: _to_entity_binary,
EdmType.BOOLEAN: _to_entity_bool,
EdmType.DATETIME: _to_entity_datetime,
EdmType.DOUBLE: _to_entity_float,
EdmType.GUID: _to_entity_guid,
EdmType.INT32: _to_entity_int32,
EdmType.INT64: _to_entity_int64,
EdmType.STRING: _to_entity_str,
}
if sys.version_info < (3,):
_PYTHON_TO_ENTITY_CONVERSIONS.update({
long: _to_entity_int64,
types.NoneType: _to_entity_none,
unicode: _to_entity_str,
})
def _convert_entity_to_json(source):
''' Converts an entity object to json to send.
The entity format is:
{
"Address":"Mountain View",
"Age":23,
"AmountDue":200.23,
"CustomerCode@odata.type":"Edm.Guid",
"CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833",
"CustomerSince@odata.type":"Edm.DateTime",
"CustomerSince":"2008-07-10T00:00:00",
"IsActive":true,
"NumberOfOrders@odata.type":"Edm.Int64",
"NumberOfOrders":"255",
"PartitionKey":"mypartitionkey",
"RowKey":"myrowkey"
}
'''
properties = {}
# set properties type for types we know if value has no type info.
# if value has type info, then set the type to value.type
for name, value in source.items():
mtype = ''
if isinstance(value, EntityProperty):
conv = _EDM_TO_ENTITY_CONVERSIONS.get(value.type)
if conv is None:
raise TypeError(
_ERROR_TYPE_NOT_SUPPORTED.format(value.type))
mtype, value = conv(value.value)
else:
conv = _PYTHON_TO_ENTITY_CONVERSIONS.get(type(value))
if conv is None and sys.version_info >= (3,) and value is None:
conv = _to_entity_none
if conv is None:
raise TypeError(
_ERROR_CANNOT_SERIALIZE_VALUE_TO_ENTITY.format(
type(value).__name__))
mtype, value = conv(value)
# form the property node
properties[name] = value
if mtype:
properties[name + '@odata.type'] = mtype
# generate the entity_body
return dumps(properties)
def _convert_table_to_json(table_name):
'''
Create json to send for a given table name. Since json format for table is
the same as entity and the only difference is that table has only one
property 'TableName', so we just call _convert_entity_to_json.
table_name:
the name of the table
'''
return _convert_entity_to_json({'TableName': table_name})
def _convert_batch_to_json(batch_requests):
'''
Create json to send for an array of batch requests.
batch_requests:
an array of requests
'''
batch_boundary = b'batch_' + _new_boundary()
changeset_boundary = b'changeset_' + _new_boundary()
body = []
body.append(b'--' + batch_boundary + b'\n')
body.append(b'Content-Type: multipart/mixed; boundary=')
body.append(changeset_boundary + b'\n\n')
content_id = 1
# Adds each request body to the POST data.
for _, request in batch_requests:
body.append(b'--' + changeset_boundary + b'\n')
body.append(b'Content-Type: application/http\n')
body.append(b'Content-Transfer-Encoding: binary\n\n')
body.append(request.method.encode('utf-8'))
body.append(b' ')
body.append(request.path.encode('utf-8'))
body.append(b' HTTP/1.1\n')
body.append(b'Content-ID: ')
body.append(str(content_id).encode('utf-8') + b'\n')
content_id += 1
for name, value in request.headers.items():
if name in _SUB_HEADERS:
body.append(name.encode('utf-8') + b': ')
body.append(value.encode('utf-8') + b'\n')
# Add different headers for different request types.
if not request.method == 'DELETE':
body.append(b'Content-Length: ')
body.append(str(len(request.body)).encode('utf-8'))
body.append(b'\n\n')
body.append(request.body + b'\n')
body.append(b'\n')
body.append(b'--' + changeset_boundary + b'--' + b'\n')
body.append(b'--' + batch_boundary + b'--')
return b''.join(body), 'multipart/mixed; boundary=' + batch_boundary.decode('utf-8')
|