def test_download_multiple_packs(self): progress = Progress(2) packs = [XDCCPack(IrcServer("irc.namibsun.net"), "xdcc_servbot", 2), XDCCPack(IrcServer("irc.namibsun.net"), "xdcc_servbot", 3)] results = self.downloader.download(packs, progress) for pack in results: self.assertTrue(os.path.isfile(pack.get_filepath())) self.assertEqual(results[pack], "OK") self.assertEqual(progress.get_single_progress_percentage(), 100.0) self.assertEqual(progress.get_total_percentage(), 100.0)
def test_download_multiple_packs_different_servers(self): progress = Progress(2) downloader = MultipleServerDownloader("random") downloader.download([ XDCCPack(IrcServer("irc.namibsun.net"), "xdcc_servbot", 2), XDCCPack(IrcServer("namibsun.net"), "xdcc_servbot", 3) ], progress) self.assertTrue(os.path.isfile("2_test.txt")) self.assertTrue(os.path.isfile("3_test.txt")) self.assertEqual(progress.get_single_progress_percentage(), 100.0) self.assertEqual(progress.get_total_percentage(), 100.0)
def do_download(): progress = Progress( len(self.download_queue), callback=lambda a, b, sin, d, e, tot, g, h: self.progress_update_signal.emit(sin, tot) ) self.spinner_start_signal.emit("download") self.downloader = MultipleServerDownloader("random") results = self.downloader.download( self.download_queue, progress ) self.download_queue = [] self.refresh_download_queue_signal.emit("") self.progress_update_signal.emit(0.0, 0.0) self.downloader.quit() self.downloading = False list_of_downloaded_packs = "" for result in results: list_of_downloaded_packs += \ result.get_filepath() + "\n" self.show_download_complete_message_signal.emit( list_of_downloaded_packs )
def download(self, packs: List[XDCCPack], progress: Progress = None)\ -> Dict[XDCCPack, str]: """ Downloads all XDCC packs specified. Optionally shares state with other threads using a Progress object. All packs need to connect to the same server :param packs: The packs to download :param progress: Optional Progress object :return: Dictionary of packs mapped to status codes: "OK": Download was successful "BOTNOTFOUND": Bot was not found "CHANNELJOINFAIL": Channel join failed, most likely due to missing whois information "NETWORKERROR": Download failed due to network error "INCORRECT": Sent file was not the correct file "EXISTED": File already existed and was completely downloaded "OTHERSERVER": If a pack was found that is hosted on a different server """ self.progress = progress if progress is not None else\ Progress(len(packs)) self.pack_queue = packs self.pack_states = {} while len(self.pack_queue) > 0: self.current_pack = self.pack_queue.pop(0) if self.current_pack.get_server().get_address() != \ self.server.get_address(): self.pack_states[self.current_pack] = "OTHERSERVER" continue status_code = "OK" try: self.start() except BotNotFoundException: status_code = "BOTNOTFOUND" except NoValidWhoisQueryException: status_code = "CHANNELJOINFAIL" except AlreadyDownloaded: status_code = "EXISTED" except IncorrectFileSentException: status_code = "INCORRECT" except NetworkError: status_code = "NETWORKERROR" self.pack_states[self.current_pack] = status_code self.reset_connection_state() self.progress.next_file() self.quit() return self.pack_states
def on_welcome(self, conn, event): self.current_pack = XDCCPack(self.server, "xdcc_servbot", 2) self.pack_queue = [] self.progress = Progress(1) with open("2_test.txt", 'w') as f: f.write("Test") self.dcc_resume_requested = True self.connection.whois(self.current_pack.get_bot())
def on_welcome(self, conn, event): self.current_pack = XDCCPack(self.server, "xdcc_servbot", 3) self.pack_queue = [] self.progress = Progress(1) event.arguments = [] self.on_privnotice(conn, event) event.arguments = [ "You will have to re-send that," "to the bot that transferred the file." ] self.on_privnotice(conn, event)
def on_welcome(self, conn, event): self.progress = Progress(1) self.file = open("testfile.mkv", 'wb') self.file.write(b"test") self.filesize = 10000000 self.current_pack = XDCCPack(self.server, "xdcc_servbot", 1) self.pack_queue = [] self.logger.log = self.logging_handler self.on_dcc_disconnect(conn, event)
def do_download(): progress = Progress( len(self.download_queue), callback=lambda a, b, single_percentage, d, e, total_percentage, g, h: self.update_progress( single_percentage, total_percentage)) self.spin_buttons(download=True) self.downloader = MultipleServerDownloader("random") self.downloader.download(self.download_queue, progress) self.download_queue = [] self.update_progress(0.0, 0.0) self.refresh_ui() self.downloader.quit() self.downloading = False
def test_callback_handler(self): def callback_tester(single_progress, single_total, single_progress_percentage, total_progress, total_total, total_percentage, current_speed, average_speed): self.callback_called = True self.assertEqual(single_progress, 300) self.assertEqual(single_total, 1000) self.assertEqual(single_progress_percentage, 30.0) self.assertEqual(total_progress, 0) self.assertEqual(total_total, 2) self.assertLess(total_percentage, 50.0) self.assertLess(0.0, total_percentage) self.assertAlmostEqual(current_speed, 100, delta=5) self.assertAlmostEqual(average_speed, 100, delta=5) progress_with_callback = Progress(2, callback=callback_tester) progress_with_callback.set_single_progress_total(1000) time.sleep(3) progress_with_callback.add_single_progress(300) self.assertTrue(self.callback_called)
def test_for_zero_division_errors(self): progress = Progress(0) self.assertEqual(progress.get_single_progress_percentage(), 0.0) self.assertEqual(progress.get_total_percentage(), 0.0)
def setUp(self): self.progress = Progress(10) self.callback_called = False
class UnitTests(unittest.TestCase): def setUp(self): self.progress = Progress(10) self.callback_called = False def tearDown(self): pass def test_single_progress(self): self.progress.set_single_progress(100) self.progress.set_single_progress_total(1000) self.assertEqual(self.progress.get_single_progress(), 100) self.assertEqual(self.progress.get_single_progress_total(), 1000) self.assertEqual(self.progress.get_single_progress_percentage(), 10.0) self.progress.add_single_progress(100) self.assertEqual(self.progress.get_single_progress(), 200) self.assertEqual(self.progress.get_single_progress_percentage(), 20.0) self.assertLess(self.progress.get_total_percentage(), 10.0) self.assertLess(0.0, self.progress.get_total_percentage()) def test_next_file(self): self.assertEqual(self.progress.get_total_percentage(), 0.0) self.progress.next_file() self.assertEqual(self.progress.get_total_percentage(), 10.0) def test_callback_handler(self): def callback_tester(single_progress, single_total, single_progress_percentage, total_progress, total_total, total_percentage, current_speed, average_speed): self.callback_called = True self.assertEqual(single_progress, 300) self.assertEqual(single_total, 1000) self.assertEqual(single_progress_percentage, 30.0) self.assertEqual(total_progress, 0) self.assertEqual(total_total, 2) self.assertLess(total_percentage, 50.0) self.assertLess(0.0, total_percentage) self.assertAlmostEqual(current_speed, 100, delta=5) self.assertAlmostEqual(average_speed, 100, delta=5) progress_with_callback = Progress(2, callback=callback_tester) progress_with_callback.set_single_progress_total(1000) time.sleep(3) progress_with_callback.add_single_progress(300) self.assertTrue(self.callback_called) def test_current_download_speed(self): self.assertEqual(self.progress.calculate_current_download_speed(), 0) self.progress.add_single_progress(50) time.sleep(0.25) self.assertEqual(self.progress.calculate_current_download_speed(), 0) self.progress.add_single_progress(50) time.sleep(0.75) self.assertAlmostEqual( self.progress.calculate_current_download_speed(), 100, delta=5) def test_for_zero_division_errors(self): progress = Progress(0) self.assertEqual(progress.get_single_progress_percentage(), 0.0) self.assertEqual(progress.get_total_percentage(), 0.0) def test_finished_numbers(self): while self.progress.get_total_percentage() < 100.0: self.progress.set_single_progress_total(100) while self.progress.get_single_progress_percentage() < 100.0: self.progress.add_single_progress(1) self.progress.next_file() self.assertEqual(self.progress.get_total_percentage(), 100.0) self.assertEqual(self.progress.get_single_progress_percentage(), 100.0) self.assertEqual(self.progress.get_single_progress(), 100) self.assertEqual(self.progress.get_single_progress(), self.progress.get_single_progress_total()) def test_going_past_total_limit(self): while self.progress.get_total_percentage() < 100.0: self.progress.next_file() self.assertEqual(self.progress.get_total_percentage(), 100.0) self.assertEqual(self.progress.total_progress, 10) self.progress.next_file() self.assertEqual(self.progress.get_total_percentage(), 100.0) self.assertEqual(self.progress.total_progress, 10) self.progress.next_file() self.assertEqual(self.progress.get_total_percentage(), 100.0) self.assertEqual(self.progress.total_progress, 10)