You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. class FileProxyMixin:
  2. """
  3. A mixin class used to forward file methods to an underlaying file
  4. object. The internal file object has to be called "file"::
  5. class FileProxy(FileProxyMixin):
  6. def __init__(self, file):
  7. self.file = file
  8. """
  9. encoding = property(lambda self: self.file.encoding)
  10. fileno = property(lambda self: self.file.fileno)
  11. flush = property(lambda self: self.file.flush)
  12. isatty = property(lambda self: self.file.isatty)
  13. newlines = property(lambda self: self.file.newlines)
  14. read = property(lambda self: self.file.read)
  15. readinto = property(lambda self: self.file.readinto)
  16. readline = property(lambda self: self.file.readline)
  17. readlines = property(lambda self: self.file.readlines)
  18. seek = property(lambda self: self.file.seek)
  19. tell = property(lambda self: self.file.tell)
  20. truncate = property(lambda self: self.file.truncate)
  21. write = property(lambda self: self.file.write)
  22. writelines = property(lambda self: self.file.writelines)
  23. @property
  24. def closed(self):
  25. return not self.file or self.file.closed
  26. def readable(self):
  27. if self.closed:
  28. return False
  29. if hasattr(self.file, 'readable'):
  30. return self.file.readable()
  31. return True
  32. def writable(self):
  33. if self.closed:
  34. return False
  35. if hasattr(self.file, 'writable'):
  36. return self.file.writable()
  37. return 'w' in getattr(self.file, 'mode', '')
  38. def seekable(self):
  39. if self.closed:
  40. return False
  41. if hasattr(self.file, 'seekable'):
  42. return self.file.seekable()
  43. return True
  44. def __iter__(self):
  45. return iter(self.file)