| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 6 # | 7 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 9 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 14 # limitations under the License. |
| 15 """FilePart implementation for representing part of a file.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 14 | 18 |
| 15 import os | 19 import os |
| 16 | 20 |
| 21 |
| 17 class FilePart(file): | 22 class FilePart(file): |
| 18 """ | 23 """Subclass of the file API for representing part of a file. |
| 19 Subclass of the file API whose only purpose it to behave as a contiguous | 24 |
| 20 subset of a given file (e.g., this object will behave as though the desired | 25 This class behaves as a contiguous subset of a given file (e.g., this object |
| 21 part of the file was written to another file, and the second file was opened). | 26 will behave as though the desired part of the file was written to another |
| 27 file, and the second file was opened). |
| 22 """ | 28 """ |
| 23 | 29 |
| 30 # pylint: disable=super-init-not-called |
| 24 def __init__(self, filename, offset, length): | 31 def __init__(self, filename, offset, length): |
| 25 """ | 32 """Initializes the FilePart. |
| 33 |
| 26 Args: | 34 Args: |
| 27 filename: The name of the existing file, of which this object represents | 35 filename: The name of the existing file, of which this object represents |
| 28 a part. | 36 a part. |
| 29 offset: The position (in bytes) in the original file that corresponds to | 37 offset: The position (in bytes) in the original file that corresponds to |
| 30 the first byte of the FilePart. | 38 the first byte of the FilePart. |
| 31 length: The total number of bytes in the FilePart. | 39 length: The total number of bytes in the FilePart. |
| 32 """ | 40 """ |
| 33 self._fp = open(filename, 'rb') | 41 self._fp = open(filename, 'rb') |
| 34 self.length = length | 42 self.length = length |
| 35 self._start = offset | 43 self._start = offset |
| 36 self._end = self._start + self.length | 44 self._end = self._start + self.length |
| 37 self._fp.seek(self._start) | 45 self._fp.seek(self._start) |
| 38 | 46 |
| 39 def __enter__(self): | 47 def __enter__(self): |
| 40 pass | 48 pass |
| 41 | 49 |
| 50 # pylint: disable=redefined-builtin |
| 42 def __exit__(self, type, value, traceback): | 51 def __exit__(self, type, value, traceback): |
| 43 self.close() | 52 self.close() |
| 44 | 53 |
| 45 def tell(self): | 54 def tell(self): |
| 46 return self._fp.tell() - self._start | 55 return self._fp.tell() - self._start |
| 47 | 56 |
| 48 def read(self, size=-1): | 57 def read(self, size=-1): |
| 49 if size < 0: | 58 if size < 0: |
| 50 size = self.length | 59 size = self.length |
| 51 size = min(size, self._end - self._fp.tell()) # Only read to our EOF | 60 size = min(size, self._end - self._fp.tell()) # Only read to our EOF |
| 52 return self._fp.read(max(0, size)) | 61 return self._fp.read(max(0, size)) |
| 53 | 62 |
| 54 def seek(self, offset, whence=os.SEEK_SET): | 63 def seek(self, offset, whence=os.SEEK_SET): |
| 55 if whence == os.SEEK_END: | 64 if whence == os.SEEK_END: |
| 56 return self._fp.seek(offset + self._end) | 65 return self._fp.seek(offset + self._end) |
| 57 elif whence == os.SEEK_CUR: | 66 elif whence == os.SEEK_CUR: |
| 58 return self._fp.seek(offset, whence) | 67 return self._fp.seek(offset, whence) |
| 59 else: | 68 else: |
| 60 return self._fp.seek(self._start + offset) | 69 return self._fp.seek(self._start + offset) |
| 61 | 70 |
| 62 def close(self): | 71 def close(self): |
| 63 self._fp.close() | 72 self._fp.close() |
| 64 | 73 |
| 65 def flush(self, size=None): | 74 def flush(self, size=None): |
| 66 raise NotImplementedError('flush is not implemented in FilePart.') | 75 raise NotImplementedError('flush is not implemented in FilePart.') |
| 67 | 76 |
| 68 def fileno(self, size=None): | 77 def fileno(self, size=None): |
| 69 raise NotImplementedError('fileno is not implemented in FilePart.') | 78 raise NotImplementedError('fileno is not implemented in FilePart.') |
| 70 | 79 |
| 71 def isatty(self, size=None): | 80 def isatty(self, size=None): |
| 72 raise NotImplementedError('isatty is not implemented in FilePart.') | 81 raise NotImplementedError('isatty is not implemented in FilePart.') |
| 73 | 82 |
| 74 def next(self, size=None): | 83 def next(self, size=None): |
| 75 raise NotImplementedError('next is not implemented in FilePart.') | 84 raise NotImplementedError('next is not implemented in FilePart.') |
| 76 | 85 |
| 77 def readline(self, size=None): | 86 def readline(self, size=None): |
| 78 raise NotImplementedError('readline is not implemented in FilePart.') | 87 raise NotImplementedError('readline is not implemented in FilePart.') |
| 79 | 88 |
| 80 def readlines(self, size=None): | 89 def readlines(self, size=None): |
| 81 raise NotImplementedError('readlines is not implemented in FilePart.') | 90 raise NotImplementedError('readlines is not implemented in FilePart.') |
| 82 | 91 |
| 83 def xreadlines(self, size=None): | 92 def xreadlines(self, size=None): |
| 84 raise NotImplementedError('xreadlines is not implemented in FilePart.') | 93 raise NotImplementedError('xreadlines is not implemented in FilePart.') |
| 85 | 94 |
| 86 def truncate(self, size=None): | 95 def truncate(self, size=None): |
| 87 raise NotImplementedError('truncate is not implemented in FilePart.') | 96 raise NotImplementedError('truncate is not implemented in FilePart.') |
| 88 | 97 |
| 89 def write(self, size=None): | 98 def write(self, size=None): |
| 90 raise NotImplementedError('write is not implemented in FilePart.') | 99 raise NotImplementedError('write is not implemented in FilePart.') |
| 91 | 100 |
| 92 def writelines(self, size=None): | 101 def writelines(self, size=None): |
| 93 raise NotImplementedError('writelines is not implemented in FilePart.') | 102 raise NotImplementedError('writelines is not implemented in FilePart.') |
| OLD | NEW |