File: _serialization.py

package info (click to toggle)
azure-multiapi-storage-python 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 18,260 kB
  • sloc: python: 196,859; sh: 60; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,595 bytes parent folder | download | duplicates (3)
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
# -------------------------------------------------------------------------
# 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

if sys.version_info >= (3,):
    from io import BytesIO
else:
    try:
        from cStringIO import StringIO as BytesIO
    except:
        from StringIO import StringIO as BytesIO

try:
    from xml.etree import cElementTree as ETree
except ImportError:
    from xml.etree import ElementTree as ETree

from ..common._common_conversion import (
    _str,
)
from ._encryption import (
    _encrypt_queue_message,
)


def _get_path(queue_name=None, include_messages=None, message_id=None):
    '''
    Creates the path to access a queue resource.

    queue_name:
        Name of queue.
    include_messages:
        Whether or not to include messages.
    message_id:
        Message id.
    '''
    if queue_name and include_messages and message_id:
        return '/{0}/messages/{1}'.format(_str(queue_name), message_id)
    if queue_name and include_messages:
        return '/{0}/messages'.format(_str(queue_name))
    elif queue_name:
        return '/{0}'.format(_str(queue_name))
    else:
        return '/'


def _convert_queue_message_xml(message_text, encode_function, key_encryption_key):
    '''
    <?xml version="1.0" encoding="utf-8"?>
    <QueueMessage>
        <MessageText></MessageText>
    </QueueMessage>
    '''
    queue_message_element = ETree.Element('QueueMessage')

    # Enabled
    message_text = encode_function(message_text)
    if key_encryption_key is not None:
        message_text = _encrypt_queue_message(message_text, key_encryption_key)
    ETree.SubElement(queue_message_element, 'MessageText').text = message_text

    # Add xml declaration and serialize
    try:
        stream = BytesIO()
        ETree.ElementTree(queue_message_element).write(stream, xml_declaration=True, encoding='utf-8', method='xml')
        output = stream.getvalue()
    finally:
        stream.close()

    return output