iterio.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # -*- coding: utf-8 -*-
  2. r"""
  3. werkzeug.contrib.iterio
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. This module implements a :class:`IterIO` that converts an iterator into
  6. a stream object and the other way round. Converting streams into
  7. iterators requires the `greenlet`_ module.
  8. To convert an iterator into a stream all you have to do is to pass it
  9. directly to the :class:`IterIO` constructor. In this example we pass it
  10. a newly created generator::
  11. def foo():
  12. yield "something\n"
  13. yield "otherthings"
  14. stream = IterIO(foo())
  15. print stream.read() # read the whole iterator
  16. The other way round works a bit different because we have to ensure that
  17. the code execution doesn't take place yet. An :class:`IterIO` call with a
  18. callable as first argument does two things. The function itself is passed
  19. an :class:`IterIO` stream it can feed. The object returned by the
  20. :class:`IterIO` constructor on the other hand is not an stream object but
  21. an iterator::
  22. def foo(stream):
  23. stream.write("some")
  24. stream.write("thing")
  25. stream.flush()
  26. stream.write("otherthing")
  27. iterator = IterIO(foo)
  28. print iterator.next() # prints something
  29. print iterator.next() # prints otherthing
  30. iterator.next() # raises StopIteration
  31. .. _greenlet: https://github.com/python-greenlet/greenlet
  32. :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
  33. :license: BSD, see LICENSE for more details.
  34. """
  35. try:
  36. import greenlet
  37. except ImportError:
  38. greenlet = None
  39. from werkzeug._compat import implements_iterator
  40. def _mixed_join(iterable, sentinel):
  41. """concatenate any string type in an intelligent way."""
  42. iterator = iter(iterable)
  43. first_item = next(iterator, sentinel)
  44. if isinstance(first_item, bytes):
  45. return first_item + b''.join(iterator)
  46. return first_item + u''.join(iterator)
  47. def _newline(reference_string):
  48. if isinstance(reference_string, bytes):
  49. return b'\n'
  50. return u'\n'
  51. @implements_iterator
  52. class IterIO(object):
  53. """Instances of this object implement an interface compatible with the
  54. standard Python :class:`file` object. Streams are either read-only or
  55. write-only depending on how the object is created.
  56. If the first argument is an iterable a file like object is returned that
  57. returns the contents of the iterable. In case the iterable is empty
  58. read operations will return the sentinel value.
  59. If the first argument is a callable then the stream object will be
  60. created and passed to that function. The caller itself however will
  61. not receive a stream but an iterable. The function will be be executed
  62. step by step as something iterates over the returned iterable. Each
  63. call to :meth:`flush` will create an item for the iterable. If
  64. :meth:`flush` is called without any writes in-between the sentinel
  65. value will be yielded.
  66. Note for Python 3: due to the incompatible interface of bytes and
  67. streams you should set the sentinel value explicitly to an empty
  68. bytestring (``b''``) if you are expecting to deal with bytes as
  69. otherwise the end of the stream is marked with the wrong sentinel
  70. value.
  71. .. versionadded:: 0.9
  72. `sentinel` parameter was added.
  73. """
  74. def __new__(cls, obj, sentinel=''):
  75. try:
  76. iterator = iter(obj)
  77. except TypeError:
  78. return IterI(obj, sentinel)
  79. return IterO(iterator, sentinel)
  80. def __iter__(self):
  81. return self
  82. def tell(self):
  83. if self.closed:
  84. raise ValueError('I/O operation on closed file')
  85. return self.pos
  86. def isatty(self):
  87. if self.closed:
  88. raise ValueError('I/O operation on closed file')
  89. return False
  90. def seek(self, pos, mode=0):
  91. if self.closed:
  92. raise ValueError('I/O operation on closed file')
  93. raise IOError(9, 'Bad file descriptor')
  94. def truncate(self, size=None):
  95. if self.closed:
  96. raise ValueError('I/O operation on closed file')
  97. raise IOError(9, 'Bad file descriptor')
  98. def write(self, s):
  99. if self.closed:
  100. raise ValueError('I/O operation on closed file')
  101. raise IOError(9, 'Bad file descriptor')
  102. def writelines(self, list):
  103. if self.closed:
  104. raise ValueError('I/O operation on closed file')
  105. raise IOError(9, 'Bad file descriptor')
  106. def read(self, n=-1):
  107. if self.closed:
  108. raise ValueError('I/O operation on closed file')
  109. raise IOError(9, 'Bad file descriptor')
  110. def readlines(self, sizehint=0):
  111. if self.closed:
  112. raise ValueError('I/O operation on closed file')
  113. raise IOError(9, 'Bad file descriptor')
  114. def readline(self, length=None):
  115. if self.closed:
  116. raise ValueError('I/O operation on closed file')
  117. raise IOError(9, 'Bad file descriptor')
  118. def flush(self):
  119. if self.closed:
  120. raise ValueError('I/O operation on closed file')
  121. raise IOError(9, 'Bad file descriptor')
  122. def __next__(self):
  123. if self.closed:
  124. raise StopIteration()
  125. line = self.readline()
  126. if not line:
  127. raise StopIteration()
  128. return line
  129. class IterI(IterIO):
  130. """Convert an stream into an iterator."""
  131. def __new__(cls, func, sentinel=''):
  132. if greenlet is None:
  133. raise RuntimeError('IterI requires greenlet support')
  134. stream = object.__new__(cls)
  135. stream._parent = greenlet.getcurrent()
  136. stream._buffer = []
  137. stream.closed = False
  138. stream.sentinel = sentinel
  139. stream.pos = 0
  140. def run():
  141. func(stream)
  142. stream.close()
  143. g = greenlet.greenlet(run, stream._parent)
  144. while 1:
  145. rv = g.switch()
  146. if not rv:
  147. return
  148. yield rv[0]
  149. def close(self):
  150. if not self.closed:
  151. self.closed = True
  152. self._flush_impl()
  153. def write(self, s):
  154. if self.closed:
  155. raise ValueError('I/O operation on closed file')
  156. if s:
  157. self.pos += len(s)
  158. self._buffer.append(s)
  159. def writelines(self, list):
  160. for item in list:
  161. self.write(item)
  162. def flush(self):
  163. if self.closed:
  164. raise ValueError('I/O operation on closed file')
  165. self._flush_impl()
  166. def _flush_impl(self):
  167. data = _mixed_join(self._buffer, self.sentinel)
  168. self._buffer = []
  169. if not data and self.closed:
  170. self._parent.switch()
  171. else:
  172. self._parent.switch((data,))
  173. class IterO(IterIO):
  174. """Iter output. Wrap an iterator and give it a stream like interface."""
  175. def __new__(cls, gen, sentinel=''):
  176. self = object.__new__(cls)
  177. self._gen = gen
  178. self._buf = None
  179. self.sentinel = sentinel
  180. self.closed = False
  181. self.pos = 0
  182. return self
  183. def __iter__(self):
  184. return self
  185. def _buf_append(self, string):
  186. '''Replace string directly without appending to an empty string,
  187. avoiding type issues.'''
  188. if not self._buf:
  189. self._buf = string
  190. else:
  191. self._buf += string
  192. def close(self):
  193. if not self.closed:
  194. self.closed = True
  195. if hasattr(self._gen, 'close'):
  196. self._gen.close()
  197. def seek(self, pos, mode=0):
  198. if self.closed:
  199. raise ValueError('I/O operation on closed file')
  200. if mode == 1:
  201. pos += self.pos
  202. elif mode == 2:
  203. self.read()
  204. self.pos = min(self.pos, self.pos + pos)
  205. return
  206. elif mode != 0:
  207. raise IOError('Invalid argument')
  208. buf = []
  209. try:
  210. tmp_end_pos = len(self._buf)
  211. while pos > tmp_end_pos:
  212. item = next(self._gen)
  213. tmp_end_pos += len(item)
  214. buf.append(item)
  215. except StopIteration:
  216. pass
  217. if buf:
  218. self._buf_append(_mixed_join(buf, self.sentinel))
  219. self.pos = max(0, pos)
  220. def read(self, n=-1):
  221. if self.closed:
  222. raise ValueError('I/O operation on closed file')
  223. if n < 0:
  224. self._buf_append(_mixed_join(self._gen, self.sentinel))
  225. result = self._buf[self.pos:]
  226. self.pos += len(result)
  227. return result
  228. new_pos = self.pos + n
  229. buf = []
  230. try:
  231. tmp_end_pos = 0 if self._buf is None else len(self._buf)
  232. while new_pos > tmp_end_pos or (self._buf is None and not buf):
  233. item = next(self._gen)
  234. tmp_end_pos += len(item)
  235. buf.append(item)
  236. except StopIteration:
  237. pass
  238. if buf:
  239. self._buf_append(_mixed_join(buf, self.sentinel))
  240. if self._buf is None:
  241. return self.sentinel
  242. new_pos = max(0, new_pos)
  243. try:
  244. return self._buf[self.pos:new_pos]
  245. finally:
  246. self.pos = min(new_pos, len(self._buf))
  247. def readline(self, length=None):
  248. if self.closed:
  249. raise ValueError('I/O operation on closed file')
  250. nl_pos = -1
  251. if self._buf:
  252. nl_pos = self._buf.find(_newline(self._buf), self.pos)
  253. buf = []
  254. try:
  255. if self._buf is None:
  256. pos = self.pos
  257. else:
  258. pos = len(self._buf)
  259. while nl_pos < 0:
  260. item = next(self._gen)
  261. local_pos = item.find(_newline(item))
  262. buf.append(item)
  263. if local_pos >= 0:
  264. nl_pos = pos + local_pos
  265. break
  266. pos += len(item)
  267. except StopIteration:
  268. pass
  269. if buf:
  270. self._buf_append(_mixed_join(buf, self.sentinel))
  271. if self._buf is None:
  272. return self.sentinel
  273. if nl_pos < 0:
  274. new_pos = len(self._buf)
  275. else:
  276. new_pos = nl_pos + 1
  277. if length is not None and self.pos + length < new_pos:
  278. new_pos = self.pos + length
  279. try:
  280. return self._buf[self.pos:new_pos]
  281. finally:
  282. self.pos = min(new_pos, len(self._buf))
  283. def readlines(self, sizehint=0):
  284. total = 0
  285. lines = []
  286. line = self.readline()
  287. while line:
  288. lines.append(line)
  289. total += len(line)
  290. if 0 < sizehint <= total:
  291. break
  292. line = self.readline()
  293. return lines