예제 #1
0
    def destination(self, fragment=False, basedir=None, platform=None,
                    path_formats=None):
        """Returns the path in the library directory designated for the
        item (i.e., where the file ought to be). fragment makes this
        method return just the path fragment underneath the root library
        directory; the path is also returned as Unicode instead of
        encoded as a bytestring. basedir can override the library's base
        directory for the destination.
        """
        self._check_db()
        platform = platform or sys.platform
        basedir = basedir or self._db.directory
        path_formats = path_formats or self._db.path_formats

        # Use a path format based on a query, falling back on the
        # default.
        for query, path_format in path_formats:
            if query == PF_KEY_DEFAULT:
                continue
            query = get_query(query, type(self))
            if query.match(self):
                # The query matches the item! Use the corresponding path
                # format.
                break
        else:
            # No query matched; fall back to default.
            for query, path_format in path_formats:
                if query == PF_KEY_DEFAULT:
                    break
            else:
                assert False, "no default path format"
        if isinstance(path_format, Template):
            subpath_tmpl = path_format
        else:
            subpath_tmpl = Template(path_format)

        # Evaluate the selected template.
        subpath = self.evaluate_template(subpath_tmpl, True)

        # Prepare path for output: normalize Unicode characters.
        if platform == 'darwin':
            subpath = unicodedata.normalize('NFD', subpath)
        else:
            subpath = unicodedata.normalize('NFC', subpath)
        # Truncate components and remove forbidden characters.
        subpath = util.sanitize_path(subpath, self._db.replacements)
        # Encode for the filesystem.
        if not fragment:
            subpath = bytestring_path(subpath)

        # Preserve extension.
        _, extension = os.path.splitext(self.path)
        if fragment:
            # Outputting Unicode.
            extension = extension.decode('utf8', 'ignore')
        subpath += extension.lower()

        # Truncate too-long components.
        maxlen = beets.config['max_filename_length'].get(int)
        if not maxlen:
            # When zero, try to determine from filesystem.
            maxlen = util.max_filename_length(self._db.directory)
        subpath = util.truncate_path(subpath, maxlen)

        if fragment:
            return subpath
        else:
            return normpath(os.path.join(basedir, subpath))
예제 #2
0
파일: test_db.py 프로젝트: jenzie/beets
 def test_truncate_unicode(self):
     p = util.truncate_path(u'abcde/fgh', posixpath, 4)
     self.assertEqual(p, u'abcd/fgh')
예제 #3
0
파일: test_db.py 프로젝트: jenzie/beets
 def test_truncate_preserves_extension(self):
     p = util.truncate_path(u'abcde/fgh.ext', posixpath, 5)
     self.assertEqual(p, u'abcde/f.ext')
예제 #4
0
 def test_truncate_preserves_extension(self):
     with _common.platform_posix():
         p = util.truncate_path(u'abcde/fgh.ext', 5)
     self.assertEqual(p, u'abcde/f.ext')
예제 #5
0
파일: test_db.py 프로젝트: jenzie/beets
 def test_truncate_bytestring(self):
     p = util.truncate_path('abcde/fgh', posixpath, 4)
     self.assertEqual(p, 'abcd/fgh')
예제 #6
0
 def test_truncate_bytestring(self):
     with _common.platform_posix():
         p = util.truncate_path(b'abcde/fgh', 4)
     self.assertEqual(p, b'abcd/fgh')
예제 #7
0
 def test_truncate_unicode(self):
     with _common.platform_posix():
         p = util.truncate_path(u'abcde/fgh', 4)
     self.assertEqual(p, u'abcd/fgh')
예제 #8
0
 def test_truncate_unicode(self):
     with _common.platform_posix():
         p = util.truncate_path(u'abcde/fgh', 4)
     self.assertEqual(p, u'abcd/fgh')
예제 #9
0
 def test_truncate_preserves_extension(self):
     with _common.platform_posix():
         p = util.truncate_path(u'abcde/fgh.ext', 5)
     self.assertEqual(p, u'abcde/f.ext')
예제 #10
0
 def test_truncate_bytestring(self):
     with _common.platform_posix():
         p = util.truncate_path('abcde/fgh', 4)
     self.assertEqual(p, 'abcd/fgh')
예제 #11
0
    def destination(self, fragment=False, basedir=None, platform=None,
                    path_formats=None):
        """Returns the path in the library directory designated for the
        item (i.e., where the file ought to be). fragment makes this
        method return just the path fragment underneath the root library
        directory; the path is also returned as Unicode instead of
        encoded as a bytestring. basedir can override the library's base
        directory for the destination.
        """
        self._check_db()
        platform = platform or sys.platform
        basedir = basedir or self._db.directory
        path_formats = path_formats or self._db.path_formats

        # Use a path format based on a query, falling back on the
        # default.
        for query, path_format in path_formats:
            if query == PF_KEY_DEFAULT:
                continue
            query = get_query(query, type(self))
            if query.match(self):
                # The query matches the item! Use the corresponding path
                # format.
                break
        else:
            # No query matched; fall back to default.
            for query, path_format in path_formats:
                if query == PF_KEY_DEFAULT:
                    break
            else:
                assert False, "no default path format"
        if isinstance(path_format, Template):
            subpath_tmpl = path_format
        else:
            subpath_tmpl = Template(path_format)

        # Evaluate the selected template.
        subpath = self.evaluate_template(subpath_tmpl, True)

        # Prepare path for output: normalize Unicode characters.
        if platform == 'darwin':
            subpath = unicodedata.normalize('NFD', subpath)
        else:
            subpath = unicodedata.normalize('NFC', subpath)
        # Truncate components and remove forbidden characters.
        subpath = util.sanitize_path(subpath, self._db.replacements)
        # Encode for the filesystem.
        if not fragment:
            subpath = bytestring_path(subpath)

        # Preserve extension.
        _, extension = os.path.splitext(self.path)
        if fragment:
            # Outputting Unicode.
            extension = extension.decode('utf8', 'ignore')
        subpath += extension.lower()

        # Truncate too-long components.
        maxlen = beets.config['max_filename_length'].get(int)
        if not maxlen:
            # When zero, try to determine from filesystem.
            maxlen = util.max_filename_length(self._db.directory)
        subpath = util.truncate_path(subpath, maxlen)

        if fragment:
            return subpath
        else:
            return normpath(os.path.join(basedir, subpath))
예제 #12
0
파일: test_db.py 프로젝트: Bitdemon/beets
 def test_truncate_preserves_extension(self):
     p = util.truncate_path(u'abcde/fgh.ext', posixpath, 5)
     self.assertEqual(p, u'abcde/f.ext')
예제 #13
0
파일: test_db.py 프로젝트: Bitdemon/beets
 def test_truncate_unicode(self):
     p = util.truncate_path(u'abcde/fgh', posixpath, 4)
     self.assertEqual(p, u'abcd/fgh')
예제 #14
0
파일: test_db.py 프로젝트: Bitdemon/beets
 def test_truncate_bytestring(self):
     p = util.truncate_path('abcde/fgh', posixpath, 4)
     self.assertEqual(p, 'abcd/fgh')