コード例 #1
0
ファイル: library.py プロジェクト: leagloriane/beets
    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
ファイル: test_library.py プロジェクト: Brainversation/beets
 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
ファイル: test_library.py プロジェクト: Brainversation/beets
 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
ファイル: test_library.py プロジェクト: Brainversation/beets
 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')