Package wtf :: Package impl :: Module _util
[hide private]
[frames] | no frames]

Source Code for Module wtf.impl._util

 1  # -*- coding: ascii -*- 
 2  # 
 3  # Copyright 2006-2012 
 4  # Andr\xe9 Malo or his licensors, as applicable 
 5  # 
 6  # Licensed under the Apache License, Version 2.0 (the "License"); 
 7  # you may not use this file except in compliance with the License. 
 8  # You may obtain a copy of the License at 
 9  # 
10  #     http://www.apache.org/licenses/LICENSE-2.0 
11  # 
12  # Unless required by applicable law or agreed to in writing, software 
13  # distributed under the License is distributed on an "AS IS" BASIS, 
14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  # See the License for the specific language governing permissions and 
16  # limitations under the License. 
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   
27 -class ContentLengthReader(object):
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