Add split path util
This commit is contained in:
parent
059ad2d4c9
commit
a48e881040
@ -35,6 +35,16 @@ def uri_to_path(uri):
|
||||
path = urllib.url2pathname(re.sub('^file://', '', uri))
|
||||
return path.encode('latin1').decode('utf-8') # Undo double encoding
|
||||
|
||||
def split_path(path):
|
||||
parts = []
|
||||
while True:
|
||||
path, part = os.path.split(path)
|
||||
if part:
|
||||
parts.insert(0, part)
|
||||
if not path or path == '/':
|
||||
break
|
||||
return parts
|
||||
|
||||
def find_files(path):
|
||||
if os.path.isfile(path):
|
||||
yield os.path.abspath(path)
|
||||
|
||||
@ -7,7 +7,7 @@ import tempfile
|
||||
import unittest
|
||||
|
||||
from mopidy.utils.path import (get_or_create_folder,
|
||||
path_to_uri, uri_to_path, find_files)
|
||||
path_to_uri, uri_to_path, split_path, find_files)
|
||||
|
||||
from tests import SkipTest, data_folder
|
||||
|
||||
@ -98,6 +98,26 @@ class UriToPathTest(unittest.TestCase):
|
||||
self.assertEqual(result, u'/tmp/æøå')
|
||||
|
||||
|
||||
class SplitPathTest(unittest.TestCase):
|
||||
def test_empty_path(self):
|
||||
self.assertEqual([], split_path(''))
|
||||
|
||||
def test_single_folder(self):
|
||||
self.assertEqual(['foo'], split_path('foo'))
|
||||
|
||||
def test_folders(self):
|
||||
self.assertEqual(['foo', 'bar', 'baz'], split_path('foo/bar/baz'))
|
||||
|
||||
def test_folders(self):
|
||||
self.assertEqual(['foo', 'bar', 'baz'], split_path('foo/bar/baz'))
|
||||
|
||||
def test_initial_slash_is_ignored(self):
|
||||
self.assertEqual(['foo', 'bar', 'baz'], split_path('/foo/bar/baz'))
|
||||
|
||||
def test_only_slash(self):
|
||||
self.assertEqual([], split_path('/'))
|
||||
|
||||
|
||||
class FindFilesTest(unittest.TestCase):
|
||||
def find(self, path):
|
||||
return list(find_files(data_folder(path)))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user