Development of an internal social media platform with personalised dashboards for students
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.

ContainerIO.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # a class to read from a container file
  6. #
  7. # History:
  8. # 1995-06-18 fl Created
  9. # 1995-09-07 fl Added readline(), readlines()
  10. #
  11. # Copyright (c) 1997-2001 by Secret Labs AB
  12. # Copyright (c) 1995 by Fredrik Lundh
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. ##
  17. # A file object that provides read access to a part of an existing
  18. # file (for example a TAR file).
  19. class ContainerIO(object):
  20. def __init__(self, file, offset, length):
  21. """
  22. Create file object.
  23. :param file: Existing file.
  24. :param offset: Start of region, in bytes.
  25. :param length: Size of region, in bytes.
  26. """
  27. self.fh = file
  28. self.pos = 0
  29. self.offset = offset
  30. self.length = length
  31. self.fh.seek(offset)
  32. ##
  33. # Always false.
  34. def isatty(self):
  35. return 0
  36. def seek(self, offset, mode=0):
  37. """
  38. Move file pointer.
  39. :param offset: Offset in bytes.
  40. :param mode: Starting position. Use 0 for beginning of region, 1
  41. for current offset, and 2 for end of region. You cannot move
  42. the pointer outside the defined region.
  43. """
  44. if mode == 1:
  45. self.pos = self.pos + offset
  46. elif mode == 2:
  47. self.pos = self.length + offset
  48. else:
  49. self.pos = offset
  50. # clamp
  51. self.pos = max(0, min(self.pos, self.length))
  52. self.fh.seek(self.offset + self.pos)
  53. def tell(self):
  54. """
  55. Get current file pointer.
  56. :returns: Offset from start of region, in bytes.
  57. """
  58. return self.pos
  59. def read(self, n=0):
  60. """
  61. Read data.
  62. :param n: Number of bytes to read. If omitted or zero,
  63. read until end of region.
  64. :returns: An 8-bit string.
  65. """
  66. if n:
  67. n = min(n, self.length - self.pos)
  68. else:
  69. n = self.length - self.pos
  70. if not n: # EOF
  71. return ""
  72. self.pos = self.pos + n
  73. return self.fh.read(n)
  74. def readline(self):
  75. """
  76. Read a line of text.
  77. :returns: An 8-bit string.
  78. """
  79. s = ""
  80. while True:
  81. c = self.read(1)
  82. if not c:
  83. break
  84. s = s + c
  85. if c == "\n":
  86. break
  87. return s
  88. def readlines(self):
  89. """
  90. Read multiple lines of text.
  91. :returns: A list of 8-bit strings.
  92. """
  93. lines = []
  94. while True:
  95. s = self.readline()
  96. if not s:
  97. break
  98. lines.append(s)
  99. return lines