コード例 #1
0
class MainProgram():
    SOURCE_PATH = Path('./files')
    DESTINATION_PATH = Path('./result/')

    def __init__(self):
        self.folders = Folders(Folders.OS_MODULE)
        self.tags = tags.Tags()
        self.utils = utils.Utils()

    def retag_files(self):
        mp3_files = self.folders.retrieve_mp3_files(self.SOURCE_PATH)

        for mp3_file in mp3_files:
            complete_file_path = self.folders.join(self.SOURCE_PATH, mp3_file.name)

            mp3 = self.tags.open_mp3_file(complete_file_path)

            tags = self.tags.read_tags(mp3)
            if len(tags) != 2 or not tags.get('song', False):
                tags = self.tags.get_artist_and_song_from_file_name(mp3_file)
            tags = self.utils.capitalize(tags)

            source_file_path = self.folders.join(
                self.SOURCE_PATH,
                mp3_file.name,
            )

            new_path = self.folders.copy_file(
                source_file_path,
                self.DESTINATION_PATH,
                '{}.mp3'.format(tags.get('song', mp3_file.name)),
            )

            new_mp3_file = self.tags.open_mp3_file(new_path)
            self.tags.write_artist_and_song_tags(new_mp3_file, tags)
コード例 #2
0
class TestFolders(TestCase):
    destination_folder = Path('./result/')
    empty_folder = Path('./foo')
    mp3_file_destination_path = Path('./result/Play.mp3')
    mp3_file_source_path = Path('./files/jax-jones-years-years-play.mp3')

    def setUp(self):
        self.folders = Folders()

    def test_access_to_not_existing_path(self):
        with self.assertRaises(FileNotFoundError):
            self.folders.retrieve_mp3_files('./path')

    def test_not_retrieve_files_from_empty_directory(self):
        if not os.path.exists(self.empty_folder):
            os.mkdir(self.empty_folder)

        entries = self.folders.retrieve_mp3_files(str(self.empty_folder))

        self.assertTrue(len(list(entries)) == 0)

    def test_retrieve_only_mp3_files_from_path(self):
        source_folder = Path('./files')
        entries = self.folders.retrieve_mp3_files(str(source_folder))

        self.assertTrue(len(list(entries)) > 0)

    def test_try_to_copy_non_existing_file(self):
        assert not self.folders.copy_file(
            './foo',
            self.mp3_file_destination_path,
            'bar.mp3',
        )

    def test_try_to_copy_to_non_existing_directory(self):
        newPath = self.folders.copy_file(
            self.mp3_file_source_path,
            self.destination_folder,
            'Play.mp3',
        )

        assert  newPath == str(self.mp3_file_destination_path)

    def test_copy_file_to_other_directory(self):
        if not os.path.exists(self.destination_folder):
            os.mkdir(self.destination_folder)

        newPath = self.folders.copy_file(
            self.mp3_file_source_path,
            self.destination_folder,
            'Play.mp3',
        )

        assert newPath
        assert newPath == str(self.mp3_file_destination_path)

    def test_join_path_with_file_name(self):
        file_path = Path('./file')
        file_name = 'file_name.mp3'

        file_path = self.folders.join(file_path, file_name)

        assert file_path == str(Path('./file/file_name.mp3'))

    def tearDown(self):
        if os.path.exists(self.empty_folder):
            shutil.rmtree(self.empty_folder, ignore_errors=True)