def _track_to_refs(track): track_path = translator.local_track_uri_to_path(track.uri, b'/') track_path = track_path.decode(sys.getfilesystemencoding(), 'replace') parts = re.findall(r'([^/]+)', track_path) track_ref = Ref.track(uri=track.uri, name=parts.pop()) refs = [Ref.directory(uri='local:directory')] for i in range(len(parts)): directory = '/'.join(parts[:i+1]) uri = translator.path_to_local_directory_uri(directory) refs.append(Ref.directory(uri=unicode(uri), name=parts[i])) return refs + [track_ref]
def _browse_directory_path(self, uri): root = uritools.urisplit(str(uri)).getpath().partition(':')[2] refs, tracks = [], [] for name in sorted(os.listdir(os.path.join(self._media_dir, root))): path = os.path.join(root, name) if os.path.isdir(os.path.join(self._media_dir, path)): uri = translator.path_to_local_directory_uri(path) refs.append(Ref.directory(uri=uri, name=name)) else: uri = translator.path_to_local_track_uri(path) tracks.append(Ref.track(uri=uri, name=name)) with self._connect() as c: refs += [track for track in tracks if schema.exists(c, track.uri)] return refs
def _browse_directory_path(self, uri): root = uritools.urisplit(str(uri)).getpath().partition(':')[2] refs, tracks = [], [] for name in sorted(os.listdir(os.path.join(self._media_dir, root))): path = os.path.join(root, name) if os.path.isdir(os.path.join(self._media_dir, path)): uri = translator.path_to_local_directory_uri(path) refs.append(Ref.directory(uri=uri, name=name)) else: uri = translator.path_to_local_track_uri(path) tracks.append(Ref.track(uri=uri, name=name)) with self._connect() as c: refs += [track for track in tracks if schema.exists(c, track.uri)] return refs
def _browse_path(self, uri, encoding=sys.getfilesystemencoding()): root = local_directory_uri_to_path(uri, b'') refs, tracks = [], [] for file in sorted(os.listdir(os.path.join(self._media_dir, root))): relpath = os.path.join(root, file) name = file.decode(encoding, 'replace') if os.path.isdir(os.path.join(self._media_dir, relpath)): uri = translator.path_to_local_directory_uri(relpath) refs.append(Ref.directory(uri=uri, name=name)) else: uri = translator.path_to_local_track_uri(relpath) tracks.append(Ref.track(uri=uri, name=name)) with self._connect() as c: refs += [track for track in tracks if schema.exists(c, track.uri)] return refs
def _browse_path(self, uri, encoding=sys.getfilesystemencoding()): root = local_uri_to_path(uri, b'') dirs, tracks = [], [] for file in sorted(os.listdir(os.path.join(self._media_dir, root))): path = os.path.join(root, file) name = file.decode(encoding, 'replace') if os.path.isdir(os.path.join(self._media_dir, path)): uri = translator.path_to_local_directory_uri(path) dirs.append(Ref.directory(uri=uri, name=name)) elif not path.lower().endswith(self._excluded_ext): uri = path_to_file_uri(os.path.join(self._media_dir, path)) tracks.append(Ref.track(uri=uri, name=name)) else: logger.debug('Skipped %s: File extension excluded.', path) return dirs + tracks
def __init__(self, uris): self._cache = { local.Library.ROOT_DIRECTORY_URI: collections.OrderedDict() } for track_uri in uris: path = translator.local_track_uri_to_path(track_uri, b'/') parts = self.splitpath_re.findall( path.decode(self.encoding, 'replace')) track_ref = models.Ref.track(uri=track_uri, name=parts.pop()) # Look for our parents backwards as this is faster than having to # do a complete search for each add. parent_uri = None child = None for i in reversed(range(len(parts))): directory = '/'.join(parts[:i + 1]) uri = translator.path_to_local_directory_uri(directory) # First dir we process is our parent if not parent_uri: parent_uri = uri # We found ourselves and we exist, done. if uri in self._cache: if child: self._cache[uri][child.uri] = child break # Initialize ourselves, store child if present, and add # ourselves as child for next loop. self._cache[uri] = collections.OrderedDict() if child: self._cache[uri][child.uri] = child child = models.Ref.directory(uri=uri, name=parts[i]) else: # Loop completed, so final child needs to be added to root. if child: self._cache[local.Library.ROOT_DIRECTORY_URI][ child.uri] = child # If no parent was set we belong in the root. if not parent_uri: parent_uri = local.Library.ROOT_DIRECTORY_URI self._cache[parent_uri][track_uri] = track_ref
def __init__(self, uris): self._cache = { local.Library.ROOT_DIRECTORY_URI: collections.OrderedDict()} for track_uri in uris: path = translator.local_track_uri_to_path(track_uri, b'/') parts = self.splitpath_re.findall( path.decode(self.encoding, 'replace')) track_ref = models.Ref.track(uri=track_uri, name=parts.pop()) # Look for our parents backwards as this is faster than having to # do a complete search for each add. parent_uri = None child = None for i in reversed(range(len(parts))): directory = '/'.join(parts[:i + 1]) uri = translator.path_to_local_directory_uri(directory) # First dir we process is our parent if not parent_uri: parent_uri = uri # We found ourselves and we exist, done. if uri in self._cache: if child: self._cache[uri][child.uri] = child break # Initialize ourselves, store child if present, and add # ourselves as child for next loop. self._cache[uri] = collections.OrderedDict() if child: self._cache[uri][child.uri] = child child = models.Ref.directory(uri=uri, name=parts[i]) else: # Loop completed, so final child needs to be added to root. if child: self._cache[ local.Library.ROOT_DIRECTORY_URI][child.uri] = child # If no parent was set we belong in the root. if not parent_uri: parent_uri = local.Library.ROOT_DIRECTORY_URI self._cache[parent_uri][track_uri] = track_ref
def test_path_to_local_directory_uri(path, uri): assert translator.path_to_local_directory_uri(path) == uri
def test_path_to_local_directory_uri(path, uri): assert translator.path_to_local_directory_uri(path) == uri
def test_path_to_local_directory_uri(path, uri): result = translator.path_to_local_directory_uri(path) assert isinstance(result, compat.text_type) assert result == uri
def test_path_to_local_directory_uri(path, uri): result = translator.path_to_local_directory_uri(path) assert isinstance(result, compat.text_type) assert result == uri