Esempio n. 1
0
 def _save_and_rename(self, old_filename, metadata, settings):
     """Includes an additional check for WavPack correction files"""
     wvc_filename = old_filename.replace(".wv", ".wvc")
     if isfile(wvc_filename):
         if settings["rename_files"] or settings["move_files"]:
             self._rename(wvc_filename, metadata, settings)
     File._save_and_rename(self, old_filename, metadata, settings)
Esempio n. 2
0
 def setUp(self):
     super().setUp()
     self.tagger.acoustidmanager = MagicMock()
     self.file = File('somepath/somefile.mp3')
     self.set_config_values({
         'save_acoustid_fingerprints': True,
     })
Esempio n. 3
0
 def test_guess_title_tracknumber(self):
     f = File('/somepath/01 somefile.mp3')
     metadata = Metadata({
         'album': 'somealbum',
     })
     f._guess_tracknumber_and_title(metadata)
     self.assertEqual(metadata['tracknumber'], '1')
     self.assertEqual(metadata['title'], 'somefile')
 def test_add_and_remove(self):
     file = File('foo.flac')
     file.acoustid_fingerprint = 'foo'
     file.acoustid_length = 120
     self.acoustidmanager.add(file, '00000000-0000-0000-0000-000000000001')
     self.tagger.window.enable_submit.assert_called_with(False)
     self.acoustidmanager.update(file,
                                 '00000000-0000-0000-0000-000000000002')
     self.tagger.window.enable_submit.assert_called_with(True)
     self.acoustidmanager.remove(file)
     self.tagger.window.enable_submit.assert_called_with(False)
Esempio n. 5
0
 def setUp(self):
     super().setUp()
     self.file = File('/somepath/somefile.mp3')
     self.INVALIDSIMVAL = 666
     self.file.similarity = self.INVALIDSIMVAL  # to check if changed or not
     self.file.supports_tag = lambda x: False if x.startswith('unsupported'
                                                              ) else True
     self.set_config_values({
         'clear_existing_tags': False,
         'compare_ignore_tags': [],
         'enabled_plugins': [],
     })
 def test_is_submitted(self):
     file = File('foo.flac')
     file.acoustid_fingerprint = 'foo'
     file.acoustid_length = 120
     self.assertTrue(self.acoustidmanager.is_submitted(file))
     self.acoustidmanager.add(file, '00000000-0000-0000-0000-000000000001')
     self.assertTrue(self.acoustidmanager.is_submitted(file))
     self.acoustidmanager.update(file,
                                 '00000000-0000-0000-0000-000000000002')
     self.assertFalse(self.acoustidmanager.is_submitted(file))
     self.acoustidmanager.update(file, '')
     self.assertTrue(self.acoustidmanager.is_submitted(file))
 def _add_unsubmitted_files(self, count):
     files = []
     for i in range(0, count):
         file = File('foo%d.flac' % i)
         files.append(file)
         file.acoustid_fingerprint = 'foo'
         file.acoustid_length = 120
         self.acoustidmanager.add(file, None)
         self.acoustidmanager.update(file,
                                     '00000000-0000-0000-0000-%012d' % i)
         self.assertFalse(self.acoustidmanager.is_submitted(file))
     return files
Esempio n. 8
0
class DataObjectTest(PicardTestCase):
    def setUp(self):
        super().setUp()
        self.tagger.acoustidmanager = MagicMock()
        self.file = File('somepath/somefile.mp3')

    def test_filename(self):
        self.assertEqual('somepath/somefile.mp3', self.file.filename)
        self.assertEqual('somefile.mp3', self.file.base_filename)

    def test_tracknumber(self):
        self.assertEqual(0, self.file.tracknumber)
        self.file.metadata['tracknumber'] = '42'
        self.assertEqual(42, self.file.tracknumber)
        self.file.metadata['tracknumber'] = 'FOURTYTWO'
        self.assertEqual(0, self.file.tracknumber)

    def test_discnumber(self):
        self.assertEqual(0, self.file.discnumber)
        self.file.metadata['discnumber'] = '42'
        self.assertEqual(42, self.file.discnumber)
        self.file.metadata['discnumber'] = 'FOURTYTWO'
        self.assertEqual(0, self.file.discnumber)

    def test_set_acoustid_fingerprint(self):
        fingerprint = 'foo'
        length = 36
        self.file.set_acoustid_fingerprint(fingerprint, length)
        self.assertEqual(fingerprint, self.file.acoustid_fingerprint)
        self.assertEqual(length, self.file.acoustid_length)
        self.tagger.acoustidmanager.add.assert_called_with(self.file, None)
        self.tagger.acoustidmanager.add.reset_mock()
        self.file.set_acoustid_fingerprint(fingerprint, length)
        self.tagger.acoustidmanager.add.assert_not_called()
        self.tagger.acoustidmanager.remove.assert_not_called()

    def test_set_acoustid_fingerprint_no_length(self):
        self.file.metadata.length = 42000
        fingerprint = 'foo'
        self.file.set_acoustid_fingerprint(fingerprint)
        self.assertEqual(fingerprint, self.file.acoustid_fingerprint)
        self.assertEqual(42, self.file.acoustid_length)

    def test_set_acoustid_fingerprint_unset(self):
        self.file.acoustid_fingerprint = 'foo'
        self.file.set_acoustid_fingerprint(None, 42)
        self.tagger.acoustidmanager.add.assert_not_called()
        self.tagger.acoustidmanager.remove.assert_called_with(self.file)
        self.assertEqual(None, self.file.acoustid_fingerprint)
        self.assertEqual(0, self.file.acoustid_length)

    def format_specific_metadata(self):
        values = ['foo', 'bar']
        self.file.metadata['test'] = values
        self.assertEqual(
            values,
            self.file.format_specific_metadata(self.file.metadata, 'test'))
Esempio n. 9
0
def create_test_files():
    test_images = [
        CoverArtImage(url='file://file1', data=create_fake_png(b'a')),
        CoverArtImage(url='file://file2', data=create_fake_png(b'b')),
    ]
    test_files = [File('test1.flac'), File('test2.flac'), File('test2.flac')]
    test_files[0].metadata.append_image(test_images[0])
    test_files[1].metadata.append_image(test_images[1])
    test_files[2].metadata.append_image(test_images[1])
    test_files[0].orig_metadata.append_image(test_images[0])
    test_files[1].orig_metadata.append_image(test_images[1])
    test_files[2].orig_metadata.append_image(test_images[1])
    return (test_images, test_files)
Esempio n. 10
0
 def test_single(self):
     file = File('foo')
     fc = FileCluster()
     fc.add('album 1', 'artist 1', file)
     self.assertEqual('album 1', fc.title)
     self.assertEqual('artist 1', fc.artist)
     self.assertEqual([file], list(fc.files))
Esempio n. 11
0
 def _save_and_rename(self, old_filename, metadata):
     """Includes an additional check for WavPack correction files"""
     wvc_filename = old_filename.replace(".wv", ".wvc")
     if isfile(wvc_filename):
         if config.setting["rename_files"] or config.setting["move_files"]:
             self._rename(wvc_filename, metadata)
     return File._save_and_rename(self, old_filename, metadata)
Esempio n. 12
0
 def test_remove_from_empty_cluster(self):
     cluster = Cluster('Test')
     cluster.files.append(File('test1.flac'))
     update_metadata_images(cluster)
     remove_metadata_images(cluster, [cluster.files[0]])
     self.assertEqual(set(), set(cluster.metadata.images))
     self.assertTrue(cluster.metadata.has_common_images)
Esempio n. 13
0
 def test_remove_from_empty_track(self):
     track = Track('00000000-0000-0000-0000-000000000000')
     track.linked_files.append(File('test1.flac'))
     update_metadata_images(track)
     remove_metadata_images(track, [track.linked_files[0]])
     self.assertEqual(set(), set(track.orig_metadata.images))
     self.assertTrue(track.orig_metadata.has_common_images)
Esempio n. 14
0
    def callback(self, objs):
        if len(objs) != 1 or not isinstance(objs[0], Cluster):
            return
        cluster = objs[0]

        # Temporary list for sorting.
        files = []

        # Create a placeholder file to keep the cluster from being deleted
        # when all the original files are removed.
        tmpFile = File("tmpFile")

        # Make a copy of all the file data in the cluster
        for file in cluster.files:
            files.append(file)

        # Add the paceholder.
        cluster.add_file(tmpFile)

        # Delete all the files from the cluster.  Use the temp list as the
        # file reference to avoid iterator related problems.
        for file in files:
            cluster.remove_file(file)

        # Sort the list.
        files.sort(
            key=lambda file: (file.metadata["artist"], file.metadata["title"]))

        # Copy all the data back to the cluster
        for file in files:
            cluster.add_file(file)

        # Remove the placeholder
        cluster.remove_file(tmpFile)
Esempio n. 15
0
 def setUp(self):
     super().setUp()
     self.file = File('/somepath/somefile.mp3')
     self.set_config_values({
         'ascii_filenames': False,
         'clear_existing_tags': False,
         'enabled_plugins': [],
         'file_naming_format': '%album%/%title%',
         'move_files_to': '/media/music',
         'move_files': False,
         'rename_files': False,
         'windows_compatibility': True,
     })
     self.metadata = Metadata({
         'album': 'somealbum',
         'title': 'sometitle',
     })
Esempio n. 16
0
 def test_whitespaces_patterns(self):
     pattern = "  a   \n b   "
     expected = {
         (re.compile('(?s:a)\\Z', re.IGNORECASE), False),
         (re.compile('(?s:b)\\Z', re.IGNORECASE), False),
     }
     self.assertEqual(File._compile_move_additional_files_pattern(pattern),
                      expected)
Esempio n. 17
0
 def test_multi(self):
     files = [
         File('foo1'),
         File('foo2'),
         File('foo3'),
         File('foo4'),
         File('foo5'),
     ]
     fc = FileCluster()
     fc.add('album 1', 'artist1', files[0])
     fc.add('Album 1', 'artist 2', files[1])
     fc.add('album\t1', 'Artist 1', files[2])
     fc.add('Album 1', 'Artist 2', files[3])
     fc.add('album 2', 'Artist 1', files[4])
     self.assertEqual('Album 1', fc.title)
     self.assertEqual('Artist 1', fc.artist)
     self.assertEqual(files, list(fc.files))
Esempio n. 18
0
 def test_remove_from_empty_album(self):
     album = Album('00000000-0000-0000-0000-000000000000')
     album.unmatched_files.files.append(File('test1.flac'))
     update_metadata_images(album)
     remove_metadata_images(album, [album.unmatched_files.files[0]])
     self.assertEqual(set(), set(album.metadata.images))
     self.assertEqual(set(), set(album.orig_metadata.images))
     self.assertTrue(album.metadata.has_common_images)
     self.assertTrue(album.orig_metadata.has_common_images)
Esempio n. 19
0
 def test_wildcard_patterns(self):
     pattern = 'c?ver.jpg .h?dden* *.jpg *.JPG'
     expected = {
         (re.compile('(?s:c.ver\\.jpg)\\Z', re.IGNORECASE), False),
         (re.compile('(?s:\\.h.dden.*)\\Z', re.IGNORECASE), True),
         (re.compile('(?s:.*\\.jpg)\\Z', re.IGNORECASE), False),
     }
     self.assertEqual(File._compile_move_additional_files_pattern(pattern),
                      expected)
Esempio n. 20
0
    def setUp(self):
        super().setUp()

        AcousticBrainzFeatureExtractionTest.singleton = self
        # Setup mock extractor
        settings['acousticbrainz_extractor'] = mock_extractor
        self.set_config_values(settings)

        self.tagger.ab_extractor = ABExtractor()
        self.assertTrue(self.tagger.ab_extractor.available())

        # Load an irrelevant test file
        self.file = File("./test/data/test.mp3")

        # Load the AB features sample and copy the recording ID
        with open(self.ab_features_file, "r", encoding="utf-8") as f:
            ab_features = json.load(f)

        # Copy the MB recordingID to the file, as we only work with already matched files
        self.file.metadata['musicbrainz_recordingid'] = ab_features['metadata']['tags']['musicbrainz_trackid']

        self.tagger.webservice = MagicMock()
Esempio n. 21
0
 def test_add_invalid(self):
     file = File('foo.flac')
     self.acoustidmanager.add(file, '00000000-0000-0000-0000-000000000001')
     self.tagger.window.enable_submit.assert_not_called()
Esempio n. 22
0
 def setUp(self):
     super().setUp()
     self.tmp_directory = mkdtemp()
     filepath = os.path.join(self.tmp_directory, 'a.mp3')
     self.file = File(filepath)
Esempio n. 23
0
 def example_1(self):
     file = File("ticket_to_ride.mp3")
     file.state = File.NORMAL
     file.metadata["album"] = "Help!"
     file.metadata["title"] = "Ticket to Ride"
     file.metadata["artist"] = "The Beatles"
     file.metadata["artistsort"] = "Beatles, The"
     file.metadata["albumartist"] = "The Beatles"
     file.metadata["albumartistsort"] = "Beatles, The"
     file.metadata["tracknumber"] = "7"
     file.metadata["totaltracks"] = "14"
     file.metadata["discnumber"] = "1"
     file.metadata["totaldiscs"] = "1"
     file.metadata["date"] = "1965-08-06"
     file.metadata["releasetype"] = ["album", "soundtrack"]
     file.metadata["~primaryreleasetype"] = ["album"]
     file.metadata["~secondaryreleasetype"] = ["soundtrack"]
     file.metadata["releasestatus"] = "official"
     file.metadata["releasecountry"] = "US"
     file.metadata["~extension"] = "mp3"
     file.metadata["musicbrainz_albumid"] = "2c053984-4645-4699-9474-d2c35c227028"
     file.metadata["musicbrainz_albumartistid"] = "b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d"
     file.metadata["musicbrainz_artistid"] = "b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d"
     file.metadata["musicbrainz_recordingid"] = "ed052ae1-c950-47f2-8d2b-46e1b58ab76c"
     file.metadata["musicbrainz_releasetrackid"] = "7668a62a-2fac-3151-a744-5707ac8c883c"
     return file
Esempio n. 24
0
 def setUp(self):
     super().setUp()
     self.tmp_directory = self.mktmpdir()
     filepath = os.path.join(self.tmp_directory, 'a.mp3')
     self.file = File(filepath)
Esempio n. 25
0
class TestPreserveTimes(PicardTestCase):

    def setUp(self):
        super().setUp()
        self.tmp_directory = self.mktmpdir()
        filepath = os.path.join(self.tmp_directory, 'a.mp3')
        self.file = File(filepath)

    def _create_testfile(self):
        # create a dummy file
        with open(self.file.filename, 'w') as f:
            f.write('xxx')
            f.flush()
            os.fsync(f.fileno())

    def _modify_testfile(self):
        # dummy file modification, append data to it
        with open(self.file.filename, 'a') as f:
            f.write('yyy')
            f.flush()
            os.fsync(f.fileno())

    def _read_testfile(self):
        with open(self.file.filename, 'r') as f:
            return f.read()

    def test_preserve_times(self):
        self._create_testfile()

        # test if times are preserved
        (before_atime_ns, before_mtime_ns) = self.file._preserve_times(self.file.filename, self._modify_testfile)

        # HERE an external access to the file is possible, modifying its access time

        # read times again and compare with original
        st = os.stat(self.file.filename)
        (after_atime_ns, after_mtime_ns) = (st.st_atime_ns, st.st_mtime_ns)

        # on macOS 10.14 and later os.utime only sets the times with second
        # precision see https://tickets.metabrainz.org/browse/PICARD-1516.
        # This also seems to depend on the Python build being used.
        if IS_MACOS:
            before_atime_ns //= 1000
            before_mtime_ns //= 1000
            after_atime_ns //= 1000
            after_mtime_ns //= 1000

        # modification times should be equal
        self.assertEqual(before_mtime_ns, after_mtime_ns)

        # access times may not be equal
        # time difference should be positive and reasonably low (if no access in between, it should be 0)
        delta = after_atime_ns - before_atime_ns
        tolerance = 10**7  #  0.01 seconds
        self.assertTrue(0 <= delta < tolerance, "0 <= %s < %s" % (delta, tolerance))

        # ensure written data can be read back
        # keep it at the end, we don't want to access file before time checks
        self.assertEqual(self._read_testfile(), 'xxxyyy')

    def test_preserve_times_nofile(self):

        with self.assertRaises(self.file.PreserveTimesStatError):
            self.file._preserve_times(self.file.filename,
                                      self._modify_testfile)
        with self.assertRaises(FileNotFoundError):
            self._read_testfile()

    def test_preserve_times_nofile_utime(self):
        self._create_testfile()

        def save():
            os.remove(self.file.filename)

        with self.assertRaises(self.file.PreserveTimesUtimeError):
            self.file._preserve_times(self.file.filename, save)
Esempio n. 26
0
 def example_1(self):
     file = File("ticket_to_ride.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = 'Help!'
     file.metadata['title'] = 'Ticket to Ride'
     file.metadata['artist'] = 'The Beatles'
     file.metadata['artistsort'] = 'Beatles, The'
     file.metadata['albumartist'] = 'The Beatles'
     file.metadata['albumartistsort'] = 'Beatles, The'
     file.metadata['tracknumber'] = '7'
     file.metadata['totaltracks'] = '14'
     file.metadata['discnumber'] = '1'
     file.metadata['totaldiscs'] = '1'
     file.metadata['date'] = '1965-08-06'
     file.metadata['releasetype'] = ['album', 'soundtrack']
     file.metadata['~primaryreleasetype'] = ['album']
     file.metadata['~secondaryreleasetype'] = ['soundtrack']
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'US'
     file.metadata['~extension'] = 'mp3'
     file.metadata[
         'musicbrainz_albumid'] = '2c053984-4645-4699-9474-d2c35c227028'
     file.metadata[
         'musicbrainz_albumartistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata[
         'musicbrainz_artistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata[
         'musicbrainz_recordingid'] = 'ed052ae1-c950-47f2-8d2b-46e1b58ab76c'
     file.metadata[
         'musicbrainz_releasetrackid'] = '7668a62a-2fac-3151-a744-5707ac8c883c'
     return file
Esempio n. 27
0
    def default_examples():
        """Generator for default example files.

        Yields:
            File: the next example File object
        """
        # example 1
        efile = File("ticket_to_ride.mp3")
        efile.state = File.NORMAL
        efile.metadata.update({
            'album':
            'Help!',
            'title':
            'Ticket to Ride',
            '~releasecomment':
            '2014 mono remaster',
            'artist':
            'The Beatles',
            'artistsort':
            'Beatles, The',
            'albumartist':
            'The Beatles',
            'albumartistsort':
            'Beatles, The',
            'tracknumber':
            '7',
            'totaltracks':
            '14',
            'discnumber':
            '1',
            'totaldiscs':
            '1',
            'originaldate':
            '1965-08-06',
            'originalyear':
            '1965',
            'date':
            '2014-09-08',
            'releasetype': ['album', 'soundtrack'],
            '~primaryreleasetype': ['album'],
            '~secondaryreleasetype': ['soundtrack'],
            'releasestatus':
            'official',
            'releasecountry':
            'US',
            '~extension':
            'mp3',
            'musicbrainz_albumid':
            'd7fbbb0a-1348-40ad-8eef-cd438d4cd203',
            'musicbrainz_albumartistid':
            'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d',
            'musicbrainz_artistid':
            'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d',
            'musicbrainz_recordingid':
            'ed052ae1-c950-47f2-8d2b-46e1b58ab76c',
            'musicbrainz_releasetrackid':
            '392639f5-5629-477e-b04b-93bffa703405',
        })
        yield efile

        # example 2
        config = get_config()
        efile = File("track05.mp3")
        efile.state = File.NORMAL
        efile.metadata.update({
            'album':
            "Coup d'État, Volume 1: Ku De Ta / Prologue",
            'title':
            "I've Got to Learn the Mambo",
            'artist':
            "Snowboy feat. James Hunter",
            'artistsort':
            "Snowboy feat. Hunter, James",
            'albumartist':
            config.setting['va_name'],
            'albumartistsort':
            config.setting['va_name'],
            'tracknumber':
            '5',
            'totaltracks':
            '13',
            'discnumber':
            '2',
            'totaldiscs':
            '2',
            'discsubtitle':
            "Beat Up",
            'originaldate':
            '2005-07-04',
            'originalyear':
            '2005',
            'date':
            '2005-07-04',
            'releasetype': ['album', 'compilation'],
            '~primaryreleasetype':
            'album',
            '~secondaryreleasetype':
            'compilation',
            'releasestatus':
            'official',
            'releasecountry':
            'AU',
            'compilation':
            '1',
            '~multiartist':
            '1',
            '~extension':
            'mp3',
            'musicbrainz_albumid':
            '4b50c71e-0a07-46ac-82e4-cb85dc0c9bdd',
            'musicbrainz_recordingid':
            'b3c487cb-0e55-477d-8df3-01ec6590f099',
            'musicbrainz_releasetrackid':
            'f8649a05-da39-39ba-957c-7abf8f9012be',
            'musicbrainz_albumartistid':
            '89ad4ac3-39f7-470e-963a-56509c546377',
            'musicbrainz_artistid': [
                '7b593455-d207-482c-8c6f-19ce22c94679',
                '9e082466-2390-40d1-891e-4803531f43fd'
            ],
        })
        yield efile
Esempio n. 28
0
 def setUp(self):
     super().setUp()
     self.file = File('somepath/somefile.mp3')
Esempio n. 29
0
class FileNamingTest(PicardTestCase):

    def setUp(self):
        super().setUp()
        self.file = File('/somepath/somefile.mp3')
        self.set_config_values({
            'ascii_filenames': False,
            'clear_existing_tags': False,
            'enabled_plugins': [],
            'file_naming_format': '%album%/%title%',
            'move_files_to': '/media/music',
            'move_files': False,
            'rename_files': False,
            'windows_compatibility': True,
        })
        self.metadata = Metadata({
            'album': 'somealbum',
            'title': 'sometitle',
        })

    def test_make_filename_no_move_and_rename(self):
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(os.path.realpath(self.file.filename), filename)

    def test_make_filename_rename_only(self):
        config.setting['rename_files'] = True
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(os.path.realpath('/somepath/sometitle.mp3'), filename)

    def test_make_filename_move_only(self):
        config.setting['move_files'] = True
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(
            os.path.realpath('/media/music/somealbum/somefile.mp3'),
            filename)

    def test_make_filename_move_and_rename(self):
        config.setting['rename_files'] = True
        config.setting['move_files'] = True
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(
            os.path.realpath('/media/music/somealbum/sometitle.mp3'),
            filename)

    def test_make_filename_move_relative_path(self):
        config.setting['move_files'] = True
        config.setting['move_files_to'] = 'subdir'
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(
            os.path.realpath('/somepath/subdir/somealbum/somefile.mp3'),
            filename)

    def test_make_filename_empty_script(self):
        config.setting['rename_files'] = True
        config.setting['file_naming_format'] = '$noop()'
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(os.path.realpath('/somepath/somefile.mp3'), filename)

    def test_make_filename_no_extension(self):
        config.setting['rename_files'] = True
        file_ = FakeMp3File('/somepath/_')
        filename = file_.make_filename(file_.filename, self.metadata)
        self.assertEqual(os.path.realpath('/somepath/sometitle.mp3'), filename)

    def test_make_filename_lowercase_extension(self):
        config.setting['rename_files'] = True
        file_ = FakeMp3File('/somepath/somefile.MP3')
        filename = file_.make_filename(file_.filename, self.metadata)
        self.assertEqual(os.path.realpath('/somepath/sometitle.mp3'), filename)

    def test_make_filename_scripted_extension(self):
        config.setting['rename_files'] = True
        config.setting['file_naming_format'] = '$set(_extension,.foo)%title%'
        filename = self.file.make_filename(self.file.filename, self.metadata)
        self.assertEqual(os.path.realpath('/somepath/sometitle.foo'), filename)

    def test_make_filename_replace_trailing_dots(self):
        config.setting['rename_files'] = True
        config.setting['move_files'] = True
        config.setting['windows_compatibility'] = True
        metadata = Metadata({
            'album': 'somealbum.',
            'title': 'sometitle',
        })
        filename = self.file.make_filename(self.file.filename, metadata)
        self.assertEqual(
            os.path.realpath('/media/music/somealbum_/sometitle.mp3'),
            filename)

    @unittest.skipUnless(not IS_WIN, "non-windows test")
    def test_make_filename_keep_trailing_dots(self):
        config.setting['rename_files'] = True
        config.setting['move_files'] = True
        config.setting['windows_compatibility'] = False
        metadata = Metadata({
            'album': 'somealbum.',
            'title': 'sometitle',
        })
        filename = self.file.make_filename(self.file.filename, metadata)
        self.assertEqual(
            os.path.realpath('/media/music/somealbum./sometitle.mp3'),
            filename)

    def test_make_filename_replace_leading_dots(self):
        config.setting['rename_files'] = True
        config.setting['move_files'] = True
        config.setting['windows_compatibility'] = True
        metadata = Metadata({
            'album': '.somealbum',
            'title': '.sometitle',
        })
        filename = self.file.make_filename(self.file.filename, metadata)
        self.assertEqual(
            os.path.realpath('/media/music/_somealbum/_sometitle.mp3'),
            filename)
Esempio n. 30
0
 def example_1(self):
     file = File("ticket_to_ride.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = 'Help!'
     file.metadata['title'] = 'Ticket to Ride'
     file.metadata['artist'] = 'The Beatles'
     file.metadata['artistsort'] = 'Beatles, The'
     file.metadata['albumartist'] = 'The Beatles'
     file.metadata['albumartistsort'] = 'Beatles, The'
     file.metadata['tracknumber'] = '7'
     file.metadata['totaltracks'] = '14'
     file.metadata['discnumber'] = '1'
     file.metadata['totaldiscs'] = '1'
     file.metadata['date'] = '1965-08-06'
     file.metadata['releasetype'] = 'album'
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'US'
     file.metadata['~extension'] = 'mp3'
     file.metadata['musicbrainz_albumid'] = '2c053984-4645-4699-9474-d2c35c227028'
     file.metadata['musicbrainz_albumartistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata['musicbrainz_artistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata['musicbrainz_trackid'] = '898a2916-f64d-48d3-ab1a-3446fb450448'
     return file
Esempio n. 31
0
 def example_2(self):
     file = File("track05.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = 'Explosive Doowops, Volume 4'
     file.metadata['title'] = 'Why? Oh Why?'
     file.metadata['artist'] = 'The Fantasys'
     file.metadata['artistsort'] = 'Fantasys, The'
     file.metadata['albumartist'] = self.config.setting['va_name']
     file.metadata['albumartistsort'] = self.config.setting['va_name']
     file.metadata['tracknumber'] = '5'
     file.metadata['totaltracks'] = '26'
     file.metadata['discnumber'] = '2'
     file.metadata['totaldiscs'] = '2'
     file.metadata['date'] = '1999-02-03'
     file.metadata['releasetype'] = 'compilation'
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'US'
     file.metadata['compilation'] = '1'
     file.metadata['~extension'] = 'mp3'
     file.metadata['musicbrainz_albumid'] = 'bcc97e8a-2055-400b-a6ed-83288285c6fc'
     file.metadata['musicbrainz_albumartistid'] = '89ad4ac3-39f7-470e-963a-56509c546377'
     file.metadata['musicbrainz_artistid'] = '06704773-aafe-4aca-8833-b449e0a6467f'
     file.metadata['musicbrainz_trackid'] = 'd92837ee-b1e4-4649-935f-e433c3e5e429'
     return file
Esempio n. 32
0
 def example_2(self):
     file = File("track05.mp3")
     file.state = File.NORMAL
     # The data for this example does not match the release on MusicBrainz,
     # but still works well enough as an example.
     file.metadata['album'] = 'Explosive Doowops, Volume 4'
     file.metadata['title'] = 'Why? Oh Why?'
     file.metadata['artist'] = 'The Fantasys'
     file.metadata['artistsort'] = 'Fantasys, The'
     file.metadata['albumartist'] = config.setting['va_name']
     file.metadata['albumartistsort'] = config.setting['va_name']
     file.metadata['tracknumber'] = '5'
     file.metadata['totaltracks'] = '26'
     file.metadata['discnumber'] = '2'
     file.metadata['totaldiscs'] = '2'
     file.metadata['date'] = '1999-02-03'
     file.metadata['releasetype'] = ['album', 'compilation']
     file.metadata['~primaryreleasetype'] = ['album']
     file.metadata['~secondaryreleasetype'] = ['compilation']
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'US'
     file.metadata['compilation'] = '1'
     file.metadata['~extension'] = 'mp3'
     file.metadata['musicbrainz_albumid'] = 'bcc97e8a-2055-400b-a6ed-83288285c6fc'
     file.metadata['musicbrainz_albumartistid'] = '89ad4ac3-39f7-470e-963a-56509c546377'
     file.metadata['musicbrainz_artistid'] = '06704773-aafe-4aca-8833-b449e0a6467f'
     file.metadata['musicbrainz_recordingid'] = 'd92837ee-b1e4-4649-935f-e433c3e5e429'
     file.metadata['musicbrainz_releasetrackid'] = 'eac99807-93d4-3668-9714-fa0c1b487ccf'
     return file
Esempio n. 33
0
class TestPreserveTimes(PicardTestCase):

    def setUp(self):
        super().setUp()
        self.tmp_directory = mkdtemp()
        filepath = os.path.join(self.tmp_directory, 'a.mp3')
        self.file = File(filepath)

    def tearDown(self):
        shutil.rmtree(self.tmp_directory)

    def _create_testfile(self):
        # create a dummy file
        with open(self.file.filename, 'w') as f:
            f.write('xxx')
            f.flush()
            os.fsync(f.fileno())

    def _modify_testfile(self):
        # dummy file modification, append data to it
        with open(self.file.filename, 'a') as f:
            f.write('yyy')
            f.flush()
            os.fsync(f.fileno())

    def _read_testfile(self):
        with open(self.file.filename, 'r') as f:
            return f.read()

    def test_preserve_times(self):
        self._create_testfile()

        # test if times are preserved
        (before_atime_ns, before_mtime_ns) = self.file._preserve_times(self.file.filename, self._modify_testfile)

        # HERE an external access to the file is possible, modifying its access time

        # read times again and compare with original
        st = os.stat(self.file.filename)
        (after_atimes_ns, after_mtimes_ns) = (st.st_atime_ns, st.st_mtime_ns)

        # modification times should be equal
        self.assertEqual(before_mtime_ns, after_mtimes_ns)

        # access times may not be equal
        # time difference should be positive and reasonably low (if no access in between, it should be 0)
        delta = after_atimes_ns - before_atime_ns
        tolerance = 10**7  #  0.01 seconds
        self.assertTrue(0 <= delta < tolerance)

        # ensure written data can be read back
        # keep it at the end, we don't want to access file before time checks
        self.assertEqual(self._read_testfile(), 'xxxyyy')

    def test_preserve_times_nofile(self):

        with self.assertRaises(self.file.PreserveTimesStatError):
            self.file._preserve_times(self.file.filename,
                                      self._modify_testfile)
        with self.assertRaises(FileNotFoundError):
            self._read_testfile()

    def test_preserve_times_nofile_utime(self):
        self._create_testfile()

        def save():
            os.remove(self.file.filename)

        with self.assertRaises(self.file.PreserveTimesUtimeError):
            result = self.file._preserve_times(self.file.filename, save)
Esempio n. 34
0
 def example_2(self):
     file = File("track05.mp3")
     file.state = File.NORMAL
     # The data for this example does not match the release on MusicBrainz,
     # but still works well enough as an example.
     file.metadata["album"] = "Explosive Doowops, Volume 4"
     file.metadata["title"] = "Why? Oh Why?"
     file.metadata["artist"] = "The Fantasys"
     file.metadata["artistsort"] = "Fantasys, The"
     file.metadata["albumartist"] = config.setting["va_name"]
     file.metadata["albumartistsort"] = config.setting["va_name"]
     file.metadata["tracknumber"] = "5"
     file.metadata["totaltracks"] = "26"
     file.metadata["discnumber"] = "2"
     file.metadata["totaldiscs"] = "2"
     file.metadata["date"] = "1999-02-03"
     file.metadata["releasetype"] = ["album", "compilation"]
     file.metadata["~primaryreleasetype"] = ["album"]
     file.metadata["~secondaryreleasetype"] = ["compilation"]
     file.metadata["releasestatus"] = "official"
     file.metadata["releasecountry"] = "US"
     file.metadata["compilation"] = "1"
     file.metadata["~extension"] = "mp3"
     file.metadata["musicbrainz_albumid"] = "bcc97e8a-2055-400b-a6ed-83288285c6fc"
     file.metadata["musicbrainz_albumartistid"] = "89ad4ac3-39f7-470e-963a-56509c546377"
     file.metadata["musicbrainz_artistid"] = "06704773-aafe-4aca-8833-b449e0a6467f"
     file.metadata["musicbrainz_recordingid"] = "d92837ee-b1e4-4649-935f-e433c3e5e429"
     file.metadata["musicbrainz_releasetrackid"] = "eac99807-93d4-3668-9714-fa0c1b487ccf"
     return file
Esempio n. 35
0
 def test_file_metadata(self):
     metadata = Metadata()
     file = File('somepath/somefile.mp3')
     self.assertEqual('', script_to_filename('$has_file()', metadata))
     self.assertEqual(
         '1', script_to_filename('$has_file()', metadata, file=file))
Esempio n. 36
0
 def setUp(self):
     super().setUp()
     self.tagger.acoustidmanager = MagicMock()
     self.file = File('somepath/somefile.mp3')
Esempio n. 37
0
def dummy_file(i):
    file = File('foo%d.flac' % i)
    file.acoustid_fingerprint = 'Z' * FINGERPRINT_SIZE
    file.acoustid_length = 120
    return file
Esempio n. 38
0
 def example_2(self):
     file = File("track05.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = "Coup d'État, Volume 1: Ku De Ta / Prologue"
     file.metadata['title'] = "I've Got to Learn the Mambo"
     file.metadata['artist'] = "Snowboy feat. James Hunter"
     file.metadata['artistsort'] = "Snowboy feat. Hunter, James"
     file.metadata['albumartist'] = config.setting['va_name']
     file.metadata['albumartistsort'] = config.setting['va_name']
     file.metadata['tracknumber'] = '5'
     file.metadata['totaltracks'] = '13'
     file.metadata['discnumber'] = '2'
     file.metadata['totaldiscs'] = '2'
     file.metadata['discsubtitle'] = "Beat Up"
     file.metadata['date'] = '2005-07-04'
     file.metadata['releasetype'] = ['album', 'compilation']
     file.metadata['~primaryreleasetype'] = 'album'
     file.metadata['~secondaryreleasetype'] = 'compilation'
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'AU'
     file.metadata['compilation'] = '1'
     file.metadata['~multiartist'] = '1'
     file.metadata['~extension'] = 'mp3'
     file.metadata['musicbrainz_albumid'] = '4b50c71e-0a07-46ac-82e4-cb85dc0c9bdd'
     file.metadata['musicbrainz_recordingid'] = 'b3c487cb-0e55-477d-8df3-01ec6590f099'
     file.metadata['musicbrainz_releasetrackid'] = 'f8649a05-da39-39ba-957c-7abf8f9012be'
     file.metadata['musicbrainz_albumartistid'] = '89ad4ac3-39f7-470e-963a-56509c546377'
     file.metadata['musicbrainz_artistid'] = ['7b593455-d207-482c-8c6f-19ce22c94679',
                                              '9e082466-2390-40d1-891e-4803531f43fd']
     return file
Esempio n. 39
0
 def example_2(self):
     file = File("track05.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = u"Coup d'État, Volume 1: Ku De Ta / Prologue"
     file.metadata['title'] = u"I've Got to Learn the Mambo"
     file.metadata['artist'] = u"Snowboy feat. James Hunter"
     file.metadata['artistsort'] = u"Snowboy feat. Hunter, James"
     file.metadata['albumartist'] = config.setting['va_name']
     file.metadata['albumartistsort'] = config.setting['va_name']
     file.metadata['tracknumber'] = '5'
     file.metadata['totaltracks'] = '13'
     file.metadata['discnumber'] = '2'
     file.metadata['totaldiscs'] = '2'
     file.metadata['discsubtitle'] = u"Beat Up"
     file.metadata['date'] = u'2005-07-04'
     file.metadata['releasetype'] = [u'album', u'compilation']
     file.metadata['~primaryreleasetype'] = u'album'
     file.metadata['~secondaryreleasetype'] = u'compilation'
     file.metadata['releasestatus'] = u'official'
     file.metadata['releasecountry'] = u'AU'
     file.metadata['compilation'] = '1'
     file.metadata['~multiartist'] = '1'
     file.metadata['~extension'] = 'mp3'
     file.metadata[
         'musicbrainz_albumid'] = u'4b50c71e-0a07-46ac-82e4-cb85dc0c9bdd'
     file.metadata[
         'musicbrainz_recordingid'] = u'b3c487cb-0e55-477d-8df3-01ec6590f099'
     file.metadata[
         'musicbrainz_releasetrackid'] = u'f8649a05-da39-39ba-957c-7abf8f9012be'
     file.metadata[
         'musicbrainz_albumartistid'] = u'89ad4ac3-39f7-470e-963a-56509c546377'
     file.metadata['musicbrainz_artistid'] = [
         u'7b593455-d207-482c-8c6f-19ce22c94679',
         u'9e082466-2390-40d1-891e-4803531f43fd'
     ]
     return file
Esempio n. 40
0
 def example_1(self):
     file = File("ticket_to_ride.mp3")
     file.state = File.NORMAL
     file.metadata['album'] = 'Help!'
     file.metadata['title'] = 'Ticket to Ride'
     file.metadata['artist'] = 'The Beatles'
     file.metadata['artistsort'] = 'Beatles, The'
     file.metadata['albumartist'] = 'The Beatles'
     file.metadata['albumartistsort'] = 'Beatles, The'
     file.metadata['tracknumber'] = '7'
     file.metadata['totaltracks'] = '14'
     file.metadata['discnumber'] = '1'
     file.metadata['totaldiscs'] = '1'
     file.metadata['date'] = '1965-08-06'
     file.metadata['releasetype'] = ['album', 'soundtrack']
     file.metadata['~primaryreleasetype'] = ['album']
     file.metadata['~secondaryreleasetype'] = ['soundtrack']
     file.metadata['releasestatus'] = 'official'
     file.metadata['releasecountry'] = 'US'
     file.metadata['~extension'] = 'mp3'
     file.metadata['musicbrainz_albumid'] = '2c053984-4645-4699-9474-d2c35c227028'
     file.metadata['musicbrainz_albumartistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata['musicbrainz_artistid'] = 'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d'
     file.metadata['musicbrainz_recordingid'] = 'ed052ae1-c950-47f2-8d2b-46e1b58ab76c'
     file.metadata['musicbrainz_releasetrackid'] = '7668a62a-2fac-3151-a744-5707ac8c883c'
     return file