def __init__(self, storage_writer, path): """Initializes a storage merge reader. Args: storage_writer (StorageWriter): storage writer. path (str): path to the input file. Raises: IOError: if the input file cannot be opened. """ # On Windows the file can sometimes be in use and we have to wait. gzip_file = None for attempt in range(1, self._MAXIMUM_NUMBER_OF_LOCKED_FILE_ATTEMPTS): try: gzip_file = gzip.open(path, 'rb') break except IOError: if attempt == (self._MAXIMUM_NUMBER_OF_LOCKED_FILE_ATTEMPTS - 1): raise time.sleep(self._LOCKED_FILE_SLEEP_TIME) if platform_specific.PlatformIsWindows(): file_handle = gzip_file.fileno() platform_specific.DisableWindowsFileHandleInheritance(file_handle) super(GZIPStorageMergeReader, self).__init__(storage_writer) self._data_buffer = None self._gzip_file = gzip_file self._path = path
def Open(self, path=None, read_only=True, **unused_kwargs): """Opens the storage. Args: path (Optional[str]): path of the storage file. read_only (Optional[bool]): True if the file should be opened in read-only mode. Raises: IOError: if the storage file is already opened. ValueError: if path is missing. """ if self._is_open: raise IOError(u'Storage file already opened.') if not path: raise ValueError(u'Missing path.') if read_only: access_mode = 'rb' else: access_mode = 'wb' self._gzip_file = gzip.open(path, access_mode, self._COMPRESSION_LEVEL) if platform_specific.PlatformIsWindows(): file_handle = self._gzip_file.fileno() platform_specific.DisableWindowsFileHandleInheritance(file_handle) if read_only: self._OpenRead() self._is_open = True self._read_only = read_only