def test_seek_or_fail_os_end(self): """ Test the :func:`seek_or_fail` function when called with an unsupported seek operation. """ # GIVEN: A Mocked object # WHEN: Attempting to seek relative to the end # THEN: An NotImplementedError should have been raised with self.assertRaises(NotImplementedError): seek_or_fail(MagicMock(), 1, os.SEEK_END)
def test_seek_or_fail_invalid_seek_cur(self): """ Test that :func:`seek_or_fail` raises an exception when seeking past the end. """ # GIVEN: A mocked file_like object mocked_file_like_object = MagicMock(**{ 'tell.return_value': 3, 'seek.return_value': 10 }) # WHEN: Attempting to seek from the current position pas the end. # THEN: An OSError should have been raised with self.assertRaises(OSError): seek_or_fail(mocked_file_like_object, 15, os.SEEK_CUR)
def test_seek_or_fail_default_method(self): """ Test the :func:`seek_or_fail` function when using the default value for the :arg:`how` """ # GIVEN: A mocked_file_like_object mocked_file_like_object = MagicMock(**{ 'seek.return_value': 5, 'tell.return_value': 0 }) # WHEN: Calling seek_or_fail with out the how arg set seek_or_fail(mocked_file_like_object, 5) # THEN: seek should be called using the os.SEEK_SET constant mocked_file_like_object.seek.assert_called_once_with(5, os.SEEK_SET)
def vaildate(self, file_path, song_data): seek_or_fail(song_data, 0x00) err_text = b'' data = read_or_fail(song_data, 20) if data != b'WoW File\nSong Words\n': err_text = data seek_or_fail(song_data, 0x42) data = read_or_fail(song_data, 16) if data != b'CSongDoc::CBlock': err_text = data if err_text: self.log_error( file_path, translate( 'SongsPlugin.WordsofWorshipSongImport', 'Invalid Words of Worship song file. Missing {text!r} header.' ).format(text=err_text)) return False return True
def test_seek_or_fail_valid_seek_cur(self): """ Test that :func:`seek_or_fail` successfully seeks to the correct position. """ # GIVEN: A mocked file_like object mocked_file_like_object = MagicMock(**{ 'tell.return_value': 3, 'seek.return_value': 8 }) # WHEN: Attempting to seek from the current position result = seek_or_fail(mocked_file_like_object, 5, os.SEEK_CUR) # THEN: The new position should be 8 (5 from its starting position) assert result == 8
def test_seek_or_fail_valid_seek_set(self): """ Test that :func:`seek_or_fail` successfully seeks to the correct position. """ # GIVEN: A mocked file-like object mocked_file_like_object = MagicMock(**{ 'tell.return_value': 3, 'seek.return_value': 5 }) # WHEN: Attempting to seek from the beginning result = seek_or_fail(mocked_file_like_object, 5, os.SEEK_SET) # THEN: The new position should be 5 from the beginning assert result == 5
def do_import(self): """ Receive a single file or a list of files to import. """ if isinstance(self.import_source, list): self.import_wizard.progress_bar.setMaximum(len(self.import_source)) for file_path in self.import_source: if self.stop_import_flag: return log.debug('Importing %s', file_path) try: self.set_defaults() # Get the song title self.title = file_path.stem with file_path.open('rb') as song_data: if not self.vaildate(file_path, song_data): continue seek_or_fail(song_data, 20) self.read_version = self.parse_version(song_data) # Seek to byte which stores number of blocks in the song seek_or_fail(song_data, 56) no_of_blocks = read_int(song_data, DataType.U8) # Seek to the beginning of the first block seek_or_fail(song_data, 82) for block_no in range(no_of_blocks): # Blocks are separated by 2 bytes, skip them, but not if this is the last block! if block_no != 0: seek_or_fail(song_data, 2, os.SEEK_CUR) text = self.parse_lines(song_data) block_type = BLOCK_TYPES[read_int( song_data, DataType.U32, 'little')] self.add_verse(text, block_type) # Now to extract the author self.parse_author(self.parse_string(song_data)) # Finally the copyright self.add_copyright(self.parse_string(song_data)) if not self.finish(): self.log_error(file_path) except IndexError: self.log_error(file_path, UiStrings().FileCorrupt) except Exception as e: self.log_error(file_path, e)