| 1 | 1 |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | import mutagen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | from music_album_creation.audio_segmentation import AudioSegmenter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from music_album_creation.audio_segmentation.data import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     SegmentationInformation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     TrackTimestampsSequenceError, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 1 |  |     WrongTimestampFormat, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  | this_dir = os.path.dirname(os.path.realpath(__file__)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | @pytest.fixture(scope='module') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | def test_audio_file_path(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |     return os.path.join(this_dir, 'know_your_enemy.mp3') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | @pytest.fixture(scope='module') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | def segmenter(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |     return AudioSegmenter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class TestSegmenting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     # def test_input_durations_to_segment(self, tmpdir, test_audio_file_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     #     durations_data = [['t1', '0:10'], ['t2', '0:40'], ['t3', '0:10']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     #     with pytest.raises(NotStartingFromZeroTimestampError, match=r"First track \({}\) is supposed to have a 0:00 timestamp. Instead {} found".format(durations_data[0][0], durations_data[0][1])): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     #         segmenter.target_directory = str(tmpdir.mkdir('album')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |     #         segmenter.segment_from_list(test_audio_file_path, durations_data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |     def test_illogical_timetamp_sequence(self, tmpdir, segmenter, test_audio_file_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         with pytest.raises(TrackTimestampsSequenceError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             segmenter.target_directory = str(tmpdir.mkdir('album')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             c = '\n'.join( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 "{} - {}".format(x[0], x[1]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |                 for x in [['t1', '0:00'], ['t2', '1:00'], ['t3', '0:35']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             _ = SegmentationInformation.from_multiline(c, 'timestamps') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             # segmenter.segment_from_list(test_audio_file_path, [['t1', '0:00'], ['t2', '1:00'], ['t3', '0:35']], supress_stdout=True, verbose=False, sleep_seconds=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     def test_wrong_timestamp_input(self, tmpdir, segmenter, test_audio_file_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         with pytest.raises(WrongTimestampFormat): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             segmenter.target_directory = str(tmpdir.mkdir('album')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             segmenter.segment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 test_audio_file_path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 SegmentationInformation.from_tracks_information( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     [['t1', '0:00'], ['t2', '1:a0'], ['t3', '1:35']], 'timestamps' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 supress_stderr=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             # segmenter.segment(test_audio_file_path, [['t1', '0:00'], ['t2', '1:a0'], ['t3', '1:35']], supress_stdout=True, verbose=False, sleep_seconds=0) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 | 1 |  |  | 
            
                                                        
            
                                    
            
            
                | 57 | 1 |  |     @pytest.mark.parametrize( | 
            
                                                        
            
                                    
            
            
                | 58 | 1 |  |         "tracks_info, names, durations", | 
            
                                                        
            
                                    
            
            
                | 59 | 1 |  |         [ | 
            
                                                        
            
                                    
            
            
                | 60 | 1 |  |             ( | 
            
                                                        
            
                                    
            
            
                | 61 | 1 |  |                 "1. tr1 - 0:00\n2. tr2 - 1:12\n3. tr3 - 2:00\n", | 
            
                                                        
            
                                    
            
            
                | 62 | 1 |  |                 ['01 - tr1.mp3', '02 - tr2.mp3', '03 - tr3.mp3'], | 
            
                                                        
            
                                    
            
            
                | 63 | 1 |  |                 [72, 48, 236], | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             ), | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             pytest.param( | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                 "1. tr1 - 0:00\n2. tr2 - 1:12\n3. tr3 - 1:00\n", | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                 ['01 - tr1.mp4', '02 - tr2.mp4', '03 - tr3.mp4'], | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                 [72, 48, 236], | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |                 marks=pytest.mark.xfail, | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             ), | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |             pytest.param( | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |                 "1. tr1 - 0:00\n2. tr2 - 1:72\n3. tr3 - 3:00\n", | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |                 ['01 - tr1.mp4', '02 - tr2.mp4', '03 - tr3.mp4'], | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |                 [72, 48, 236], | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |                 marks=pytest.mark.xfail, | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             ), | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         ], | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     ) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     def test_valid_segmentation( | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         self, tracks_info, names, durations, tmpdir, test_audio_file_path, segmenter | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     ): | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         from music_album_creation.audio_segmentation.data import SegmentationInformation | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         segmenter.target_directory = str(tmpdir.mkdir('album')) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |         tracks_file = tmpdir.join('tracks_info.txt') | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         tracks_info = {2: u'{}'.format(tracks_info)}.get(sys.version_info[0], tracks_info) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         tracks_file.write_text(tracks_info, 'utf-8') | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         with open(tracks_file, 'r') as f: | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |             segmentation_info = SegmentationInformation.from_multiline( | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |                 f.read().strip(), | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |                 'timestamps', | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         segmenter.segment( | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             test_audio_file_path, | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             segmentation_info, | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             sleep_seconds=0, | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         # segmenter.segment_from_file( | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         #     test_audio_file_path, | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         #     str(tracks_file), | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         #     'timestamps', | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         #     supress_stdout=False, | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         #     supress_stderr=False, | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         #     sleep_seconds=0, | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         # ) | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |         file_names = sorted(os.listdir(segmenter.target_directory)) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         assert file_names == names | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |         assert [ | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |             abs( | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |                 getattr( | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |                     mutagen.File(os.path.join(segmenter.target_directory, x[0])).info, | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |                     'length', | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |                     0, | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |                 ) | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |                 - x[1] | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |             < 1 | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |             for x in zip(file_names, durations) | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         ] | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |