/usr/lib/python2.7/dist-packages/azure/storage/table/_deserialization.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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | #-------------------------------------------------------------------------
# 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
from dateutil import parser
if sys.version_info < (3,):
from urllib2 import quote as url_quote
else:
from urllib.parse import quote as url_quote
from json import (
loads,
)
from .._http import HTTPResponse
from azure.common import (
AzureException,
)
from .._common_conversion import (
_decode_base64_to_bytes,
)
from .._error import (
_ERROR_DECRYPTION_FAILURE,
_validate_decryption_required,
)
from ._error import (
_ERROR_TYPE_NOT_SUPPORTED,
_ERROR_INVALID_PROPERTY_RESOLVER,
)
from .models import (
Entity,
EntityProperty,
Table,
EdmType,
AzureBatchOperationError,
)
from ..models import (
_list,
_HeaderDict,
)
from ._encryption import (
_decrypt_entity,
_extract_encryption_metadata,
)
def _get_continuation_from_response_headers(response):
marker = {}
for name, value in response.headers.items():
if name.startswith('x-ms-continuation'):
marker[name[len('x-ms-continuation') + 1:]] = value
return marker
# Tables of conversions to and from entity types. We support specific
# datatypes, and beyond that the user can use an EntityProperty to get
# custom data type support.
def _from_entity_binary(value):
return EntityProperty(EdmType.BINARY, _decode_base64_to_bytes(value))
def _from_entity_int32(value):
return EntityProperty(EdmType.INT32, int(value))
def _from_entity_datetime(value):
# Note that Azure always returns UTC datetime, and dateutil parser
# will set the tzinfo on the date it returns
return parser.parse(value)
_EDM_TYPES = [EdmType.BINARY, EdmType.INT64, EdmType.GUID, EdmType.DATETIME,
EdmType.STRING, EdmType.INT32, EdmType.DOUBLE, EdmType.BOOLEAN]
_ENTITY_TO_PYTHON_CONVERSIONS = {
EdmType.BINARY: _from_entity_binary,
EdmType.INT32: _from_entity_int32,
EdmType.INT64: int,
EdmType.DOUBLE: float,
EdmType.DATETIME: _from_entity_datetime,
}
def _convert_json_response_to_entity(response, property_resolver, require_encryption,
key_encryption_key, key_resolver):
'''
:param bool require_encryption:
If set, will enforce that the retrieved entity is encrypted and decrypt it.
:param object key_encryption_key:
The user-provided key-encryption-key. Must implement the following methods:
unwrap_key(key, algorithm)--returns the unwrapped form of the specified symmetric key using the
string-specified algorithm.
get_kid()--returns a string key id for this key-encryption-key.
:param function key_resolver(kid):
The user-provided key resolver. Uses the kid string to return a key-encryption-key implementing
the interface defined above.
'''
if response is None or response.body is None:
return None
root = loads(response.body.decode('utf-8'))
return _decrypt_and_deserialize_entity(root, property_resolver, require_encryption,
key_encryption_key, key_resolver)
def _convert_json_to_entity(entry_element, property_resolver, encrypted_properties):
''' Convert json response to entity.
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"
}
'''
entity = Entity()
properties = {}
edmtypes = {}
odata = {}
for name, value in entry_element.items():
if name.startswith('odata.'):
odata[name[6:]] = value
elif name.endswith('@odata.type'):
edmtypes[name[:-11]] = value
else:
properties[name] = value
# Partition key is a known property
partition_key = properties.pop('PartitionKey', None)
if partition_key:
entity['PartitionKey'] = partition_key
# Row key is a known property
row_key = properties.pop('RowKey', None)
if row_key:
entity['RowKey'] = row_key
# Timestamp is a known property
timestamp = properties.pop('Timestamp', None)
if timestamp:
entity['Timestamp'] = _from_entity_datetime(timestamp)
for name, value in properties.items():
mtype = edmtypes.get(name);
# use the property resolver if present
if property_resolver:
# Clients are not expected to resolve these interal fields.
# This check avoids unexpected behavior from the user-defined
# property resolver.
if not (name == '_ClientEncryptionMetadata1' or \
name == '_ClientEncryptionMetadata2'):
mtype = property_resolver(partition_key, row_key,
name, value, mtype)
# throw if the type returned is not a valid edm type
if mtype and mtype not in _EDM_TYPES:
raise AzureException(_ERROR_TYPE_NOT_SUPPORTED.format(mtype))
# If the property was encrypted, supercede the results of the resolver and set as binary
if encrypted_properties is not None and name in encrypted_properties:
mtype = EdmType.BINARY
# Add type for Int32
if type(value) is int:
mtype = EdmType.INT32
# no type info, property should parse automatically
if not mtype:
entity[name] = value
else: # need an object to hold the property
conv = _ENTITY_TO_PYTHON_CONVERSIONS.get(mtype)
if conv is not None:
try:
property = conv(value)
except Exception as e:
# throw if the type returned by the property resolver
# cannot be used in the conversion
if property_resolver:
raise AzureException(
_ERROR_INVALID_PROPERTY_RESOLVER.format(name, value, mtype))
else:
raise e
else:
property = EntityProperty(mtype, value)
entity[name] = property
# extract etag from entry
etag = odata.get('etag')
if timestamp:
etag = 'W/"datetime\'' + url_quote(timestamp) + '\'"'
entity['etag'] = etag
return entity
def _convert_json_response_to_tables(response):
''' Converts the response to tables class.
'''
if response is None or response.body is None:
return None
tables = _list()
continuation = _get_continuation_from_response_headers(response)
tables.next_marker = continuation.get('nexttablename')
root = loads(response.body.decode('utf-8'))
if 'TableName' in root:
table = Table()
table.name = root['TableName']
tables.append(table)
else:
for element in root['value']:
table = Table()
table.name = element['TableName']
tables.append(table)
return tables
def _convert_json_response_to_entities(response, property_resolver, require_encryption,
key_encryption_key, key_resolver):
''' Converts the response to tables class.
'''
if response is None or response.body is None:
return None
entities = _list()
entities.next_marker = _get_continuation_from_response_headers(response)
root = loads(response.body.decode('utf-8'))
if 'value' in root:
for entity in root['value']:
entity = _decrypt_and_deserialize_entity(entity, property_resolver, require_encryption,
key_encryption_key, key_resolver)
entities.append(entity)
else:
entities.append(_convert_json_to_entity(entity,
property_resolver))
return entities
def _decrypt_and_deserialize_entity(entity, property_resolver, require_encryption,
key_encryption_key, key_resolver):
try:
_validate_decryption_required(require_encryption, key_encryption_key,
key_resolver)
entity_iv, encrypted_properties, content_encryption_key, isJavaV1 = None, None, None, False
if (key_encryption_key is not None) or (key_resolver is not None):
entity_iv, encrypted_properties, content_encryption_key, isJavaV1 = \
_extract_encryption_metadata(entity, require_encryption, key_encryption_key, key_resolver)
except:
raise AzureException(_ERROR_DECRYPTION_FAILURE)
entity = _convert_json_to_entity(entity, property_resolver, encrypted_properties)
if entity_iv is not None and encrypted_properties is not None and \
content_encryption_key is not None:
try:
entity = _decrypt_entity(entity, encrypted_properties, content_encryption_key,
entity_iv, isJavaV1)
except:
raise AzureException(_ERROR_DECRYPTION_FAILURE)
return entity
def _extract_etag(response):
''' Extracts the etag from the response headers. '''
if response and response.headers:
return response.headers.get('etag')
return None
def _parse_batch_response(response):
if response is None or response.body is None:
return None
parts = response.body.split(b'--changesetresponse_')
responses = []
for part in parts:
httpLocation = part.find(b'HTTP/')
if httpLocation > 0:
response_part = _parse_batch_response_part(part[httpLocation:])
if response_part.status >= 300:
_parse_batch_error(response_part)
responses.append(_extract_etag(response_part))
return responses
def _parse_batch_response_part(part):
lines = part.splitlines();
# First line is the HTTP status/reason
status, _, reason = lines[0].partition(b' ')[2].partition(b' ')
# Followed by headers and body
headers = {}
body = b''
isBody = False
for line in lines[1:]:
if line == b'' and not isBody:
isBody = True
elif isBody:
body += line
else:
headerName, _, headerVal = line.partition(b': ')
headers[headerName.lower().decode("utf-8")] = headerVal.decode("utf-8")
return HTTPResponse(int(status), reason.strip(), headers, body)
def _parse_batch_error(part):
doc = loads(part.body.decode('utf-8'))
code = ''
message = ''
error = doc.get('odata.error')
if error:
code = error.get('code')
if error.get('message'):
message = error.get('message').get('value')
raise AzureBatchOperationError(message, part.status, code)
|