示例#1
0
    def browse(self, uri):
        logger.debug("Browsing files at: %s", uri)
        result = []
        local_path = path.uri_to_path(uri)

        if str(local_path) == "root":
            return list(self._get_media_dirs_refs())

        if not self._is_in_basedir(local_path):
            logger.warning(
                "Rejected attempt to browse path (%s) outside dirs defined "
                "in file/media_dirs config.",
                uri,
            )
            return []
        if path.uri_to_path(uri).is_file():
            logger.error("Rejected attempt to browse file (%s)", uri)
            return []

        for dir_entry in local_path.iterdir():
            child_path = dir_entry.resolve()
            uri = path.path_to_uri(child_path)

            if not self._show_dotfiles and dir_entry.name.startswith("."):
                continue

            if (self._excluded_file_extensions
                    and dir_entry.suffix in self._excluded_file_extensions):
                continue

            if child_path.is_symlink() and not self._follow_symlinks:
                logger.debug("Ignoring symlink: %s", uri)
                continue

            if not self._is_in_basedir(child_path):
                logger.debug("Ignoring symlink to outside base dir: %s", uri)
                continue

            if child_path.is_dir():
                result.append(
                    models.Ref.directory(name=dir_entry.name, uri=uri))
            elif child_path.is_file():
                result.append(models.Ref.track(name=dir_entry.name, uri=uri))

        def order(item):
            return (item.type != models.Ref.DIRECTORY, item.name)

        result.sort(key=order)

        return result
示例#2
0
def local_uri_to_path(uri, media_dir):
    """Convert local track or directory URI to absolute path."""
    if (not uri.startswith('local:directory:')
            and not uri.startswith('local:track:')):
        raise ValueError('Invalid URI.')
    file_path = path.uri_to_path(uri).split(b':', 1)[1]
    return os.path.join(media_dir, file_path)
示例#3
0
def local_uri_to_path(uri, media_dir):
    """Convert local track or directory URI to absolute path."""
    if (
            not uri.startswith('local:directory:') and
            not uri.startswith('local:track:')):
        raise ValueError('Invalid URI.')
    file_path = path.uri_to_path(uri).split(b':', 1)[1]
    return os.path.join(media_dir, file_path)
示例#4
0
    def lookup(self, uri):
        logger.debug("Looking up file URI: %s", uri)
        local_path = path.uri_to_path(uri)

        try:
            result = self._scanner.scan(uri)
            track = tags.convert_tags_to_track(result.tags).replace(
                uri=uri, length=result.duration)
        except exceptions.ScannerError as e:
            logger.warning("Failed looking up %s: %s", uri, e)
            track = models.Track(uri=uri)

        if not track.name:
            track = track.replace(name=local_path.name)

        return [track]
示例#5
0
    def browse(self, uri):
        logger.debug('Browsing files at: %s', uri)
        result = []
        local_path = path.uri_to_path(uri)

        if local_path == 'root':
            return list(self._get_media_dirs_refs())

        if not self._is_in_basedir(os.path.realpath(local_path)):
            logger.warning(
                'Rejected attempt to browse path (%s) outside dirs defined '
                'in file/media_dirs config.', uri)
            return []

        for dir_entry in os.listdir(local_path):
            child_path = os.path.join(local_path, dir_entry)
            uri = path.path_to_uri(child_path)

            if not self._show_dotfiles and dir_entry.startswith(b'.'):
                continue

            if (self._excluded_file_extensions
                    and dir_entry.endswith(self._excluded_file_extensions)):
                continue

            if os.path.islink(child_path) and not self._follow_symlinks:
                logger.debug('Ignoring symlink: %s', uri)
                continue

            if not self._is_in_basedir(os.path.realpath(child_path)):
                logger.debug('Ignoring symlink to outside base dir: %s', uri)
                continue

            name = dir_entry.decode(FS_ENCODING, 'replace')
            if os.path.isdir(child_path):
                result.append(models.Ref.directory(name=name, uri=uri))
            elif os.path.isfile(child_path):
                result.append(models.Ref.track(name=name, uri=uri))

        def order(item):
            return (item.type != models.Ref.DIRECTORY, item.name)

        result.sort(key=order)

        return result
示例#6
0
    def lookup(self, uri):
        logger.debug('Looking up file URI: %s', uri)
        local_path = path.uri_to_path(uri)

        try:
            result = self._scanner.scan(uri)
            track = tags.convert_tags_to_track(result.tags).copy(
                uri=uri, length=result.duration)
        except exceptions.ScannerError as e:
            logger.warning('Failed looking up %s: %s', uri, e)
            track = models.Track(uri=uri)

        if not track.name:
            filename = os.path.basename(local_path)
            name = urllib2.unquote(filename).decode(FS_ENCODING, 'replace')
            track = track.copy(name=name)

        return [track]
示例#7
0
文件: library.py 项目: connrs/mopidy
    def lookup(self, uri):
        logger.debug('Looking up file URI: %s', uri)
        local_path = path.uri_to_path(uri)

        try:
            result = self._scanner.scan(uri)
            track = tags.convert_tags_to_track(result.tags).copy(
                uri=uri, length=result.duration)
        except exceptions.ScannerError as e:
            logger.warning('Failed looking up %s: %s', uri, e)
            track = models.Track(uri=uri)

        if not track.name:
            filename = os.path.basename(local_path)
            name = urllib2.unquote(filename).decode(FS_ENCODING, 'replace')
            track = track.copy(name=name)

        return [track]
示例#8
0
文件: library.py 项目: mopidy/mopidy
    def browse(self, uri):
        logger.debug('Browsing files at: %s', uri)
        result = []
        local_path = path.uri_to_path(uri)

        if local_path == 'root':
            return list(self._get_media_dirs_refs())

        if not self._is_in_basedir(os.path.realpath(local_path)):
            logger.warning(
                'Rejected attempt to browse path (%s) outside dirs defined '
                'in file/media_dirs config.', uri)
            return []

        for dir_entry in os.listdir(local_path):
            child_path = os.path.join(local_path, dir_entry)
            uri = path.path_to_uri(child_path)

            if not self._show_dotfiles and dir_entry.startswith(b'.'):
                continue

            if (self._excluded_file_extensions and
                    dir_entry.endswith(self._excluded_file_extensions)):
                continue

            if os.path.islink(child_path) and not self._follow_symlinks:
                logger.debug('Ignoring symlink: %s', uri)
                continue

            if not self._is_in_basedir(os.path.realpath(child_path)):
                logger.debug('Ignoring symlink to outside base dir: %s', uri)
                continue

            name = dir_entry.decode(FS_ENCODING, 'replace')
            if os.path.isdir(child_path):
                result.append(models.Ref.directory(name=name, uri=uri))
            elif os.path.isfile(child_path):
                result.append(models.Ref.track(name=name, uri=uri))

        def order(item):
            return (item.type != models.Ref.DIRECTORY, item.name)
        result.sort(key=order)

        return result
示例#9
0
def playlist_uri_to_path(uri, playlists_dir):
    if not uri.startswith('m3u:'):
        raise ValueError('Invalid URI %s' % uri)
    file_path = path.uri_to_path(uri)
    return os.path.join(playlists_dir, file_path)
示例#10
0
def uri_to_path(uri):
    """Convert URI to file path."""
    return path.uri_to_path(uri)
示例#11
0
 def test_latin1_in_uri(self):
     result = path.uri_to_path('file:///tmp/%E6%F8%E5')
     self.assertEqual(result, '/tmp/æøå'.encode('latin-1'))
示例#12
0
 def test_unicode_in_uri(self):
     result = path.uri_to_path('file:///tmp/%C3%A6%C3%B8%C3%A5')
     self.assertEqual(result, '/tmp/æøå'.encode('utf-8'))
示例#13
0
 def test_space_in_uri(self):
     result = path.uri_to_path('file:///tmp/test%20this')
     self.assertEqual(result, '/tmp/test this'.encode('utf-8'))
示例#14
0
 def test_simple_uri(self):
     result = path.uri_to_path('file:///etc/fstab')
     self.assertEqual(result, '/etc/fstab'.encode('utf-8'))
示例#15
0
    def test_latin1_in_uri(self):
        result = path.uri_to_path("file:///tmp/%E6%F8%E5")

        assert bytes(result) == b"/tmp/\xe6\xf8\xe5"
示例#16
0
    def test_unicode_in_uri(self):
        result = path.uri_to_path("file:///tmp/%C3%A6%C3%B8%C3%A5")

        assert result == pathlib.Path("/tmp/æøå")
示例#17
0
    def test_space_in_uri(self):
        result = path.uri_to_path("file:///tmp/test%20this")

        assert result == pathlib.Path("/tmp/test this")
示例#18
0
    def test_simple_uri(self):
        result = path.uri_to_path("file:///etc/fstab")

        assert result == pathlib.Path("/etc/fstab")
示例#19
0
def playlist_uri_to_path(uri, playlists_dir):
    if not uri.startswith('m3u:'):
        raise ValueError('Invalid URI %s' % uri)
    file_path = path.uri_to_path(uri)
    return os.path.join(playlists_dir, file_path)
示例#20
0
 def test_latin1_in_uri(self):
     result = path.uri_to_path('file:///tmp/%E6%F8%E5')
     self.assertEqual(result, '/tmp/æøå'.encode('latin-1'))
示例#21
0
 def test_unicode_in_uri(self):
     result = path.uri_to_path('file:///tmp/%C3%A6%C3%B8%C3%A5')
     self.assertEqual(result, '/tmp/æøå'.encode('utf-8'))
示例#22
0
 def test_space_in_uri(self):
     result = path.uri_to_path('file:///tmp/test%20this')
     self.assertEqual(result, '/tmp/test this'.encode('utf-8'))
示例#23
0
 def test_simple_uri(self):
     result = path.uri_to_path('file:///etc/fstab')
     self.assertEqual(result, '/etc/fstab'.encode('utf-8'))