iterio.py 11 KB

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