1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Implementation utilities
19 ========================
20 """
21 __author__ = u"Andr\xe9 Malo"
22 __docformat__ = "restructuredtext en"
23
24 from wtf import stream as _stream
25
26
28 """
29 Emulate EOF condition after Content length is reached
30
31 :IVariables:
32 - `_stream`: Stream to read from
33 - `_left`: Octets that still can be read
34
35 :Types:
36 - `_stream`: ``file``
37 - `_left`: ``int``
38 """
39
40 - def __init__(self, stream, clen):
41 """
42 Initialization
43
44 :Parameters:
45 - `stream`: The stream to read from
46 - `clen`: The length of the stream
47
48 :Types:
49 - `stream`: ``file``
50 - `clen`: ``int``
51 """
52 self._stream, self._left = stream, int(clen)
53
54 - def read(self, size):
55 """
56 Read (at max) `size` bytes
57
58 :Parameters:
59 - `size`: Maximum number of octets to read
60
61 :Types:
62 - `size`: ``int``
63
64 :return: The bytes read (empty on EOF)
65 :rtype: ``str``
66 """
67 if self._left > 0:
68 size = min(size, self._left)
69 result = _stream.read_exact(self._stream, size)
70 self._left -= len(result)
71 return result
72 return ""
73