File: streams.py

package info (click to toggle)
python-fixtures 4.2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 2,547; makefile: 29; sh: 9
file content (89 lines) | stat: -rw-r--r-- 2,712 bytes parent folder | download
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
#  fixtures: Fixtures with cleanups for testing and convenience.
#
# Copyright (c) 2012, Robert Collins <robertc@robertcollins.net>
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# license you chose for the specific language governing permissions and
# limitations under that license.

__all__ = [
    'ByteStream',
    'DetailStream',
    'StringStream',
]

import io

from fixtures import Fixture


class Stream(Fixture):
    """Expose a file-like object as a detail.

    :attr stream: The file-like object.
    """

    def __init__(self, detail_name, stream_factory):
        """Create a ByteStream.

        :param detail_name: Use this as the name of the stream.
        :param stream_factory: Called to construct a pair of streams:
            (write_stream, content_stream).
        """
        self._detail_name = detail_name
        self._stream_factory = stream_factory

    def _setUp(self):
        # Available with the fixtures[streams] extra.
        from testtools.content import content_from_stream

        write_stream, read_stream = self._stream_factory()
        self.stream = write_stream
        self.addDetail(
            self._detail_name, content_from_stream(read_stream, seek_offset=0)
        )


def _byte_stream_factory():
    result = io.BytesIO()
    return (result, result)


def ByteStream(detail_name):
    """Provide a file-like object that accepts bytes and expose as a detail.

    :param detail_name: The name of the detail.
    :return: A fixture which has an attribute `stream` containing the file-like
        object.
    """
    return Stream(detail_name, _byte_stream_factory)


def _string_stream_factory():
    lower = io.BytesIO()
    upper = io.TextIOWrapper(lower, encoding="utf8")
    # See http://bugs.python.org/issue7955
    upper._CHUNK_SIZE = 1
    return upper, lower


def StringStream(detail_name):
    """Provide a file-like object that accepts strings and expose as a detail.

    :param detail_name: The name of the detail.
    :return: A fixture which has an attribute `stream` containing the file-like
        object.
    """
    return Stream(detail_name, _string_stream_factory)


def DetailStream(detail_name):
    """Deprecated alias for ByteStream."""
    return ByteStream(detail_name)