コード例 #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 TestMp3TaggerManager(TestCase):
    folder_manager = Folders(Folders.OS_MODULE)
    tagger_manager = Mp3TaggerManager()

    def test_cant_open_file_as_mp3(self):
        if not self.folder_manager.exists('filename.txt'):
            file = open('filename.txt', 'w+')
            file.close()

        not_mp3_file_path = Path('./filename.txt')

        with self.assertRaises(Exception):
            self.tagger_manager.open_mp3_file(not_mp3_file_path)

    def test_can_open_file_as_mp3(self):
        mp3_file_source_path = Path('./files/jax-jones-years-years-play.mp3')

        assert self.tagger_manager.open_mp3_file(str(mp3_file_source_path))

    def test_read_version_1_tags(self):
        mock_file = Mock(artist='artist', song='song')

        tags = self.tagger_manager.read_version_tags(1, mock_file)

        self.assertEqual('artist', tags['artist'])
        self.assertEqual('song', tags['song'])

    def test_read_version_2_tags(self):
        mock_file = Mock(artist='artist', song='song')

        tags = self.tagger_manager.read_version_tags(2, mock_file)

        self.assertEqual('artist', tags['artist'])
        self.assertEqual('song', tags['song'])

    def tearDown(self):
        if self.folder_manager.exists('filename.txt'):
            self.folder_manager.remove_dir('filename.txt')
コード例 #3
0
class Tags():
    MP3_TAGGER_MODULE = 'mp3_tagger'

    folder_manager = Folders(Folders.OS_MODULE)

    def __init__(self, module):
        if module == self.MP3_TAGGER_MODULE:
            self.manager = Mp3TaggerManager()
        else:
            raise NotDefinedLibraryManagerError

    def open_mp3_file(self, path):
        return self.manager.open_mp3_file(path)

    def read_tags(self, mp3_file):
        tags = self.manager.read_version_tags(2, mp3_file)

        if not tags.get('artist', False) or not tags.get('song', False):
            tags = self.manager.read_version_tags(1, mp3_file)

        return tags

    def get_artist_and_song_from_file_name(self, file):
        file_name = self.folder_manager.splitext(file.name)[0]
        clear_file_name = file_name.replace('_', ' ')
        tags = clear_file_name.split('-')

        return {
            'artist': tags[0],
            'song': tags[1],
        } if len(tags) == 2 else None

    def write_artist_and_song_tags(self, file, tags):
        self.manager.write_artist_and_song_tags(file, tags)

        return file
コード例 #4
0
 def __init__(self):
     self.folders = Folders(Folders.OS_MODULE)
     self.tags = tags.Tags()
     self.utils = utils.Utils()
コード例 #5
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')

    folders = Folders(Folders.OS_MODULE)
    manager = os_manager.OsManager()

    def test_builder_returns_right_class(self):
        manager = Folders(Folders.OS_MODULE)

        assert type(manager.folder_manager) == os_manager.OsManager

    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 self.manager.exists(self.empty_folder):
            self.manager.create_dir(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 self.manager.exists(self.destination_folder):
            self.manager.create_dir(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 self.manager.exists(self.empty_folder):
            shutil.rmtree(self.empty_folder, ignore_errors=True)
コード例 #6
0
    def test_builder_returns_right_class(self):
        manager = Folders(Folders.OS_MODULE)

        assert type(manager.folder_manager) == os_manager.OsManager
コード例 #7
0
parser.add_argument(
    'name',
    type=str,
    help=
    'the name of the email marketing, something like \'MVX-2013-ProjectName\'')
parser.add_argument(
    '--langs',
    type=str,
    nargs='+',
    metavar='L',
    help='the languages to be used, supported languages are \'[pt-BR, en, es]\''
)
args = parser.parse_args()

files = Files()
folders = Folders()


def createProject(projectName, langs):
    folders.createProjectFolders(projectName=projectName, langs=langs)
    files.createProjectFiles(projectName=projectName, langs=langs)


def main():
    langs = ['pt-BR'] if not args.langs else args.langs

    if folders.folderExists(args.name):
        opt = raw_input(
            'Folder \'%s\' alredy exists, do you want to overwrite it\'s contents? '
            % args.name)
        opt = opt.lower()
コード例 #8
0
 def setUp(self):
     self.folders = Folders()