request.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # -*- coding: utf-8 -*-
  2. # imageio is distributed under the terms of the (new) BSD License.
  3. """
  4. Definition of the Request object, which acts as a kind of bridge between
  5. what the user wants and what the plugins can.
  6. """
  7. import os
  8. from io import BytesIO
  9. import zipfile
  10. import tempfile
  11. import shutil
  12. import enum
  13. import warnings
  14. from ..core import urlopen, get_remote_file
  15. from pathlib import Path
  16. from urllib.parse import urlparse
  17. from typing import Optional
  18. # URI types
  19. URI_BYTES = 1
  20. URI_FILE = 2
  21. URI_FILENAME = 3
  22. URI_ZIPPED = 4
  23. URI_HTTP = 5
  24. URI_FTP = 6
  25. class IOMode(str, enum.Enum):
  26. """Available Image modes
  27. This is a helper enum for ``Request.Mode`` which is a composite of a
  28. ``Request.ImageMode`` and ``Request.IOMode``. The IOMode that tells the
  29. plugin if the resource should be read from or written to. Available values are
  30. - read ("r"): Read from the specified resource
  31. - write ("w"): Write to the specified resource
  32. """
  33. read = "r"
  34. write = "w"
  35. class ImageMode(str, enum.Enum):
  36. """Available Image modes
  37. This is a helper enum for ``Request.Mode`` which is a composite of a
  38. ``Request.ImageMode`` and ``Request.IOMode``. The image mode that tells the
  39. plugin the desired (and expected) image shape. Available values are
  40. - single_image ("i"): Return a single image extending in two spacial
  41. dimensions
  42. - multi_image ("I"): Return a list of images extending in two spacial
  43. dimensions
  44. - single_volume ("v"): Return an image extending into multiple dimensions.
  45. E.g. three spacial dimensions for image stacks, or two spatial and one
  46. time dimension for videos
  47. - multi_volume ("V"): Return a list of images extending into multiple
  48. dimensions.
  49. - any_mode ("?"): Return an image in any format (the plugin decides the
  50. appropriate action).
  51. """
  52. single_image = "i"
  53. multi_image = "I"
  54. single_volume = "v"
  55. multi_volume = "V"
  56. any_mode = "?"
  57. @enum.unique
  58. class Mode(str, enum.Enum):
  59. """The mode to use when interacting with the resource
  60. ``Request.Mode`` is a composite of ``Request.ImageMode`` and
  61. ``Request.IOMode``. The image mode that tells the plugin the desired (and
  62. expected) image shape and the ``Request.IOMode`` tells the plugin the way
  63. the resource should be interacted with. For a detailed description of the
  64. available modes, see the documentation for ``Request.ImageMode`` and
  65. ``Request.IOMode`` respectively.
  66. Available modes are all combinations of ``Request.IOMode`` and ``Request.ImageMode``:
  67. - read_single_image ("ri")
  68. - read_multi_image ("rI")
  69. - read_single_volume ("rv")
  70. - read_multi_volume ("rV")
  71. - read_any ("r?")
  72. - write_single_image ("wi")
  73. - write_multi_image ("wI")
  74. - write_single_volume ("wv")
  75. - write_multi_volume ("wV")
  76. - write_any ("w?")
  77. Examples
  78. --------
  79. >>> Request.Mode("rI") # a list of simple images should be read from the resource
  80. >>> Request.Mode("wv") # a single volume should be written to the resource
  81. """
  82. read_single_image = "ri"
  83. read_multi_image = "rI"
  84. read_single_volume = "rv"
  85. read_multi_volume = "rV"
  86. read_any = "r?"
  87. write_single_image = "wi"
  88. write_multi_image = "wI"
  89. write_single_volume = "wv"
  90. write_multi_volume = "wV"
  91. write_any = "w?"
  92. @classmethod
  93. def _missing_(cls, value):
  94. """Enable Mode("r") and Mode("w")
  95. The sunder method ``_missing_`` is called whenever the constructor fails
  96. to directly look up the corresponding enum value from the given input.
  97. In our case, we use it to convert the modes "r" and "w" (from the v3
  98. API) into their legacy versions "r?" and "w?".
  99. More info on _missing_:
  100. https://docs.python.org/3/library/enum.html#supported-sunder-names
  101. """
  102. if value == "r":
  103. return cls("r?")
  104. elif value == "w":
  105. return cls("w?")
  106. else:
  107. raise ValueError(f"{value} is no valid Mode.")
  108. @property
  109. def io_mode(self) -> IOMode:
  110. return IOMode(self.value[0])
  111. @property
  112. def image_mode(self) -> ImageMode:
  113. return ImageMode(self.value[1])
  114. def __getitem__(self, key):
  115. """For backwards compatibility with the old non-enum modes"""
  116. if key == 0:
  117. return self.io_mode
  118. elif key == 1:
  119. return self.image_mode
  120. else:
  121. raise IndexError(f"Mode has no item {key}")
  122. SPECIAL_READ_URIS = "<video", "<screen>", "<clipboard>"
  123. # The user can use this string in a write call to get the data back as bytes.
  124. RETURN_BYTES = "<bytes>"
  125. # Example images that will be auto-downloaded
  126. EXAMPLE_IMAGES = {
  127. "astronaut.png": "Image of the astronaut Eileen Collins",
  128. "camera.png": "A grayscale image of a photographer",
  129. "checkerboard.png": "Black and white image of a chekerboard",
  130. "wood.jpg": "A (repeatable) texture of wooden planks",
  131. "bricks.jpg": "A (repeatable) texture of stone bricks",
  132. "clock.png": "Photo of a clock with motion blur (Stefan van der Walt)",
  133. "coffee.png": "Image of a cup of coffee (Rachel Michetti)",
  134. "chelsea.png": "Image of Stefan's cat",
  135. "wikkie.png": "Image of Almar's cat",
  136. "coins.png": "Image showing greek coins from Pompeii",
  137. "horse.png": "Image showing the silhouette of a horse (Andreas Preuss)",
  138. "hubble_deep_field.png": "Photograph taken by Hubble telescope (NASA)",
  139. "immunohistochemistry.png": "Immunohistochemical (IHC) staining",
  140. "moon.png": "Image showing a portion of the surface of the moon",
  141. "page.png": "A scanned page of text",
  142. "text.png": "A photograph of handdrawn text",
  143. "bacterial_colony.tif": "Multi-page TIFF image of a bacterial colony",
  144. "calcium_imaging.tif": "Neuronal calcium imaging video",
  145. "chelsea.zip": "The chelsea.png in a zipfile (for testing)",
  146. "chelsea.bsdf": "The chelsea.png in a BSDF file(for testing)",
  147. "newtonscradle.gif": "Animated GIF of a newton's cradle",
  148. "cockatoo.mp4": "Video file of a cockatoo",
  149. "cockatoo_yuv420.mp4": "Video file of a cockatoo with yuv420 pixel format",
  150. "stent.npz": "Volumetric image showing a stented abdominal aorta",
  151. "meadow_cube.jpg": "A cubemap image of a meadow, e.g. to render a skybox.",
  152. }
  153. class Request(object):
  154. """ImageResource handling utility.
  155. Represents a request for reading or saving an image resource. This
  156. object wraps information to that request and acts as an interface
  157. for the plugins to several resources; it allows the user to read
  158. from filenames, files, http, zipfiles, raw bytes, etc., but offer
  159. a simple interface to the plugins via ``get_file()`` and
  160. ``get_local_filename()``.
  161. For each read/write operation a single Request instance is used and passed
  162. to the can_read/can_write method of a format, and subsequently to
  163. the Reader/Writer class. This allows rudimentary passing of
  164. information between different formats and between a format and
  165. associated reader/writer.
  166. Parameters
  167. ----------
  168. uri : {str, bytes, file}
  169. The resource to load the image from.
  170. mode : str
  171. The first character is "r" or "w", indicating a read or write
  172. request. The second character is used to indicate the kind of data:
  173. "i" for an image, "I" for multiple images, "v" for a volume,
  174. "V" for multiple volumes, "?" for don't care.
  175. """
  176. def __init__(self, uri, mode, *, extension=None, format_hint: str = None, **kwargs):
  177. # General
  178. self.raw_uri = uri
  179. self._uri_type = None
  180. self._filename = None
  181. self._extension = None
  182. self._format_hint = None
  183. self._kwargs = kwargs
  184. self._result = None # Some write actions may have a result
  185. # To handle the user-side
  186. self._filename_zip = None # not None if a zipfile is used
  187. self._bytes = None # Incoming bytes
  188. self._zipfile = None # To store a zipfile instance (if used)
  189. # To handle the plugin side
  190. self._file = None # To store the file instance
  191. self._file_is_local = False # whether the data needs to be copied at end
  192. self._filename_local = None # not None if using tempfile on this FS
  193. self._firstbytes = None # For easy header parsing
  194. # To store formats that may be able to fulfil this request
  195. # self._potential_formats = []
  196. # Check mode
  197. try:
  198. self._mode = Mode(mode)
  199. except ValueError:
  200. raise ValueError(f"Invalid Request.Mode: {mode}")
  201. # Parse what was given
  202. self._parse_uri(uri)
  203. # Set extension
  204. if extension is not None:
  205. if extension[0] != ".":
  206. raise ValueError(
  207. "`extension` should be a file extension starting with a `.`,"
  208. f" but is `{extension}`."
  209. )
  210. self._extension = extension
  211. elif self._filename is not None:
  212. if self._uri_type in (URI_FILENAME, URI_ZIPPED):
  213. path = self._filename
  214. else:
  215. path = urlparse(self._filename).path
  216. ext = Path(path).suffix.lower()
  217. self._extension = ext if ext != "" else None
  218. if format_hint is not None:
  219. warnings.warn(
  220. "The usage of `format_hint` is deprecated and will be removed "
  221. "in ImageIO v3. Use `extension` instead.",
  222. DeprecationWarning,
  223. )
  224. if format_hint is not None and format_hint[0] != ".":
  225. raise ValueError(
  226. "`format_hint` should be a file extension starting with a `.`,"
  227. f" but is `{format_hint}`."
  228. )
  229. self.format_hint = format_hint
  230. def _parse_uri(self, uri):
  231. """Try to figure our what we were given"""
  232. is_read_request = self.mode.io_mode is IOMode.read
  233. is_write_request = self.mode.io_mode is IOMode.write
  234. if isinstance(uri, str):
  235. # Explicit
  236. if uri.startswith("imageio:"):
  237. if is_write_request:
  238. raise RuntimeError("Cannot write to the standard images.")
  239. fn = uri.split(":", 1)[-1].lower()
  240. fn, _, zip_part = fn.partition(".zip/")
  241. if zip_part:
  242. fn += ".zip"
  243. if fn not in EXAMPLE_IMAGES:
  244. raise ValueError("Unknown standard image %r." % fn)
  245. self._uri_type = URI_FILENAME
  246. self._filename = get_remote_file("images/" + fn, auto=True)
  247. if zip_part:
  248. self._filename += "/" + zip_part
  249. elif uri.startswith("http://") or uri.startswith("https://"):
  250. self._uri_type = URI_HTTP
  251. self._filename = uri
  252. elif uri.startswith("ftp://") or uri.startswith("ftps://"):
  253. self._uri_type = URI_FTP
  254. self._filename = uri
  255. elif uri.startswith("file://"):
  256. self._uri_type = URI_FILENAME
  257. self._filename = uri[7:]
  258. elif uri.startswith(SPECIAL_READ_URIS) and is_read_request:
  259. self._uri_type = URI_BYTES
  260. self._filename = uri
  261. elif uri.startswith(RETURN_BYTES) and is_write_request:
  262. self._uri_type = URI_BYTES
  263. self._filename = uri
  264. else:
  265. self._uri_type = URI_FILENAME
  266. self._filename = uri
  267. elif isinstance(uri, memoryview) and is_read_request:
  268. self._uri_type = URI_BYTES
  269. self._filename = "<bytes>"
  270. self._bytes = uri.tobytes()
  271. elif isinstance(uri, bytes) and is_read_request:
  272. self._uri_type = URI_BYTES
  273. self._filename = "<bytes>"
  274. self._bytes = uri
  275. elif isinstance(uri, Path):
  276. self._uri_type = URI_FILENAME
  277. self._filename = str(uri)
  278. # Files
  279. elif is_read_request:
  280. if hasattr(uri, "read") and hasattr(uri, "close"):
  281. self._uri_type = URI_FILE
  282. self._filename = "<file>"
  283. self._file = uri # Data must be read from here
  284. elif is_write_request:
  285. if hasattr(uri, "write") and hasattr(uri, "close"):
  286. self._uri_type = URI_FILE
  287. self._filename = "<file>"
  288. self._file = uri # Data must be written here
  289. # Expand user dir
  290. if self._uri_type == URI_FILENAME and self._filename.startswith("~"):
  291. self._filename = os.path.expanduser(self._filename)
  292. # Check if a zipfile
  293. if self._uri_type == URI_FILENAME:
  294. # Search for zip extension followed by a path separator
  295. for needle in [".zip/", ".zip\\"]:
  296. zip_i = self._filename.lower().find(needle)
  297. if zip_i > 0:
  298. zip_i += 4
  299. zip_path = self._filename[:zip_i]
  300. if os.path.isdir(zip_path):
  301. pass # is an existing dir (see #548)
  302. elif is_write_request or os.path.isfile(zip_path):
  303. self._uri_type = URI_ZIPPED
  304. self._filename_zip = (
  305. zip_path,
  306. self._filename[zip_i:].lstrip("/\\"),
  307. )
  308. break
  309. # Check if we could read it
  310. if self._uri_type is None:
  311. uri_r = repr(uri)
  312. if len(uri_r) > 60:
  313. uri_r = uri_r[:57] + "..."
  314. raise IOError("Cannot understand given URI: %s." % uri_r)
  315. # Check if this is supported
  316. noWriting = [URI_HTTP, URI_FTP]
  317. if is_write_request and self._uri_type in noWriting:
  318. raise IOError("imageio does not support writing to http/ftp.")
  319. # Deprecated way to load standard images, give a sensible error message
  320. if is_read_request and self._uri_type in [URI_FILENAME, URI_ZIPPED]:
  321. fn = self._filename
  322. if self._filename_zip:
  323. fn = self._filename_zip[0]
  324. if (not os.path.exists(fn)) and (fn in EXAMPLE_IMAGES):
  325. raise IOError(
  326. "No such file: %r. This file looks like one of "
  327. "the standard images, but from imageio 2.1, "
  328. "standard images have to be specified using "
  329. '"imageio:%s".' % (fn, fn)
  330. )
  331. # Make filename absolute
  332. if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
  333. if self._filename_zip:
  334. self._filename_zip = (
  335. os.path.abspath(self._filename_zip[0]),
  336. self._filename_zip[1],
  337. )
  338. else:
  339. self._filename = os.path.abspath(self._filename)
  340. # Check whether file name is valid
  341. if self._uri_type in [URI_FILENAME, URI_ZIPPED]:
  342. fn = self._filename
  343. if self._filename_zip:
  344. fn = self._filename_zip[0]
  345. if is_read_request:
  346. # Reading: check that the file exists (but is allowed a dir)
  347. if not os.path.exists(fn):
  348. raise FileNotFoundError("No such file: '%s'" % fn)
  349. else:
  350. # Writing: check that the directory to write to does exist
  351. dn = os.path.dirname(fn)
  352. if not os.path.exists(dn):
  353. raise FileNotFoundError("The directory %r does not exist" % dn)
  354. @property
  355. def filename(self):
  356. """Name of the ImageResource.
  357. The uri for which reading/saving was requested. This
  358. can be a filename, an http address, or other resource
  359. identifier. Do not rely on the filename to obtain the data,
  360. but use ``get_file()`` or ``get_local_filename()`` instead.
  361. """
  362. return self._filename
  363. @property
  364. def extension(self) -> str:
  365. """The (lowercase) extension of the requested filename.
  366. Suffixes in url's are stripped. Can be None if the request is
  367. not based on a filename.
  368. """
  369. return self._extension
  370. @property
  371. def format_hint(self) -> Optional[str]:
  372. return self._format_hint
  373. @format_hint.setter
  374. def format_hint(self, format: str) -> None:
  375. self._format_hint = format
  376. if self._extension is None:
  377. self._extension = format
  378. @property
  379. def mode(self):
  380. """The mode of the request. The first character is "r" or "w",
  381. indicating a read or write request. The second character is
  382. used to indicate the kind of data:
  383. "i" for an image, "I" for multiple images, "v" for a volume,
  384. "V" for multiple volumes, "?" for don't care.
  385. """
  386. return self._mode
  387. @property
  388. def kwargs(self):
  389. """The dict of keyword arguments supplied by the user."""
  390. return self._kwargs
  391. # For obtaining data
  392. def get_file(self):
  393. """get_file()
  394. Get a file object for the resource associated with this request.
  395. If this is a reading request, the file is in read mode,
  396. otherwise in write mode. This method is not thread safe. Plugins
  397. should not close the file when done.
  398. This is the preferred way to read/write the data. But if a
  399. format cannot handle file-like objects, they should use
  400. ``get_local_filename()``.
  401. """
  402. want_to_write = self.mode.io_mode is IOMode.write
  403. # Is there already a file?
  404. # Either _uri_type == URI_FILE, or we already opened the file,
  405. # e.g. by using firstbytes
  406. if self._file is not None:
  407. return self._file
  408. if self._uri_type == URI_BYTES:
  409. if want_to_write:
  410. # Create new file object, we catch the bytes in finish()
  411. self._file = BytesIO()
  412. self._file_is_local = True
  413. else:
  414. self._file = BytesIO(self._bytes)
  415. elif self._uri_type == URI_FILENAME:
  416. if want_to_write:
  417. self._file = open(self.filename, "wb")
  418. else:
  419. self._file = open(self.filename, "rb")
  420. elif self._uri_type == URI_ZIPPED:
  421. # Get the correct filename
  422. filename, name = self._filename_zip
  423. if want_to_write:
  424. # Create new file object, we catch the bytes in finish()
  425. self._file = BytesIO()
  426. self._file_is_local = True
  427. else:
  428. # Open zipfile and open new file object for specific file
  429. self._zipfile = zipfile.ZipFile(filename, "r")
  430. self._file = self._zipfile.open(name, "r")
  431. self._file = SeekableFileObject(self._file)
  432. elif self._uri_type in [URI_HTTP or URI_FTP]:
  433. assert not want_to_write # This should have been tested in init
  434. timeout = os.getenv("IMAGEIO_REQUEST_TIMEOUT")
  435. if timeout is None or not timeout.isdigit():
  436. timeout = 5
  437. self._file = urlopen(self.filename, timeout=float(timeout))
  438. self._file = SeekableFileObject(self._file)
  439. return self._file
  440. def get_local_filename(self):
  441. """get_local_filename()
  442. If the filename is an existing file on this filesystem, return
  443. that. Otherwise a temporary file is created on the local file
  444. system which can be used by the format to read from or write to.
  445. """
  446. if self._uri_type == URI_FILENAME:
  447. return self._filename
  448. else:
  449. # Get filename
  450. if self.extension is not None:
  451. ext = self.extension
  452. else:
  453. ext = os.path.splitext(self._filename)[1]
  454. fd, self._filename_local = tempfile.mkstemp(ext, "imageio_")
  455. os.close(fd)
  456. # Write stuff to it?
  457. if self.mode.io_mode == IOMode.read:
  458. with open(self._filename_local, "wb") as file:
  459. shutil.copyfileobj(self.get_file(), file)
  460. return self._filename_local
  461. def finish(self) -> None:
  462. """Wrap up this request.
  463. Finishes any pending reads or writes, closes any open files and frees
  464. any resources allocated by this request.
  465. """
  466. if self.mode.io_mode == IOMode.write:
  467. # See if we "own" the data and must put it somewhere
  468. bytes = None
  469. if self._filename_local:
  470. bytes = Path(self._filename_local).read_bytes()
  471. elif self._file_is_local:
  472. self._file_is_local = False
  473. bytes = self._file.getvalue()
  474. # Put the data in the right place
  475. if bytes is not None:
  476. if self._uri_type == URI_BYTES:
  477. self._result = bytes # Picked up by imread function
  478. elif self._uri_type == URI_FILE:
  479. self._file.write(bytes)
  480. elif self._uri_type == URI_ZIPPED:
  481. zf = zipfile.ZipFile(self._filename_zip[0], "a")
  482. zf.writestr(self._filename_zip[1], bytes)
  483. zf.close()
  484. # elif self._uri_type == URI_FILENAME: -> is always direct
  485. # elif self._uri_type == URI_FTP/HTTP: -> write not supported
  486. # Close open files that we know of (and are responsible for)
  487. if self._file and self._uri_type != URI_FILE:
  488. self._file.close()
  489. self._file = None
  490. if self._zipfile:
  491. self._zipfile.close()
  492. self._zipfile = None
  493. # Remove temp file
  494. if self._filename_local:
  495. try:
  496. os.remove(self._filename_local)
  497. except Exception: # pragma: no cover
  498. warnings.warn(
  499. "Failed to delete the temporary file at "
  500. f"`{self._filename_local}`. Please report this issue."
  501. )
  502. self._filename_local = None
  503. # Detach so gc can clean even if a reference of self lingers
  504. self._bytes = None
  505. def get_result(self):
  506. """For internal use. In some situations a write action can have
  507. a result (bytes data). That is obtained with this function.
  508. """
  509. # Is there a reason to disallow reading multiple times?
  510. self._result, res = None, self._result
  511. return res
  512. @property
  513. def firstbytes(self):
  514. """The first 256 bytes of the file. These can be used to
  515. parse the header to determine the file-format.
  516. """
  517. if self._firstbytes is None:
  518. self._read_first_bytes()
  519. return self._firstbytes
  520. def _read_first_bytes(self, N=256):
  521. if self._bytes is not None:
  522. self._firstbytes = self._bytes[:N]
  523. else:
  524. # Prepare
  525. try:
  526. f = self.get_file()
  527. except IOError:
  528. if os.path.isdir(self.filename): # A directory, e.g. for DICOM
  529. self._firstbytes = bytes()
  530. return
  531. raise
  532. try:
  533. i = f.tell()
  534. except Exception:
  535. i = None
  536. # Read
  537. self._firstbytes = read_n_bytes(f, N)
  538. # Set back
  539. try:
  540. if i is None:
  541. raise Exception("cannot seek with None")
  542. f.seek(i)
  543. except Exception:
  544. # Prevent get_file() from reusing the file
  545. self._file = None
  546. # If the given URI was a file object, we have a problem,
  547. if self._uri_type == URI_FILE:
  548. raise IOError("Cannot seek back after getting firstbytes!")
  549. def read_n_bytes(f, N):
  550. """read_n_bytes(file, n)
  551. Read n bytes from the given file, or less if the file has less
  552. bytes. Returns zero bytes if the file is closed.
  553. """
  554. bb = bytes()
  555. while len(bb) < N:
  556. extra_bytes = f.read(N - len(bb))
  557. if not extra_bytes:
  558. break
  559. bb += extra_bytes
  560. return bb
  561. class SeekableFileObject:
  562. """A readonly wrapper file object that add support for seeking, even if
  563. the wrapped file object does not. The allows us to stream from http and
  564. still use Pillow.
  565. """
  566. def __init__(self, f):
  567. self.f = f
  568. self._i = 0 # >=0 but can exceed buffer
  569. self._buffer = b""
  570. self._have_all = False
  571. self.closed = False
  572. def read(self, n=None):
  573. # Fix up n
  574. if n is None:
  575. pass
  576. else:
  577. n = int(n)
  578. if n < 0:
  579. n = None
  580. # Can and must we read more?
  581. if not self._have_all:
  582. more = b""
  583. if n is None:
  584. more = self.f.read()
  585. self._have_all = True
  586. else:
  587. want_i = self._i + n
  588. want_more = want_i - len(self._buffer)
  589. if want_more > 0:
  590. more = self.f.read(want_more)
  591. if len(more) < want_more:
  592. self._have_all = True
  593. self._buffer += more
  594. # Read data from buffer and update pointer
  595. if n is None:
  596. res = self._buffer[self._i :]
  597. else:
  598. res = self._buffer[self._i : self._i + n]
  599. self._i += len(res)
  600. return res
  601. def readline(self):
  602. yield from self._file.readline()
  603. def tell(self):
  604. return self._i
  605. def seek(self, i, mode=0):
  606. # Mimic BytesIO behavior
  607. # Get the absolute new position
  608. i = int(i)
  609. if mode == 0:
  610. if i < 0:
  611. raise ValueError("negative seek value " + str(i))
  612. real_i = i
  613. elif mode == 1:
  614. real_i = max(0, self._i + i) # negative ok here
  615. elif mode == 2:
  616. if not self._have_all:
  617. self.read()
  618. real_i = max(0, len(self._buffer) + i)
  619. else:
  620. raise ValueError("invalid whence (%s, should be 0, 1 or 2)" % i)
  621. # Read some?
  622. if real_i <= len(self._buffer):
  623. pass # no need to read
  624. elif not self._have_all:
  625. assert real_i > self._i # if we don't have all, _i cannot be > _buffer
  626. self.read(real_i - self._i) # sets self._i
  627. self._i = real_i
  628. return self._i
  629. def close(self):
  630. self.closed = True
  631. self.f.close()
  632. def isatty(self):
  633. return False
  634. def seekable(self):
  635. return True
  636. class InitializationError(Exception):
  637. """The plugin could not initialize from the given request.
  638. This is a _internal_ error that is raised by plugins that fail to handle
  639. a given request. We use this to differentiate incompatibility between
  640. a plugin and a request from an actual error/bug inside a plugin.
  641. """
  642. pass