Added uri_to_path with tests
This commit is contained in:
parent
21eadf3dc7
commit
d785b9b14e
@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import urllib
|
||||
|
||||
logger = logging.getLogger('mopidy.utils.path')
|
||||
@ -27,6 +28,13 @@ def path_to_uri(*paths):
|
||||
return 'file:' + urllib.pathname2url(path)
|
||||
return 'file://' + urllib.pathname2url(path)
|
||||
|
||||
def uri_to_path(uri):
|
||||
if sys.platform == 'win32':
|
||||
path = urllib.url2pathname(re.sub('^file:', '', uri))
|
||||
else:
|
||||
path = urllib.url2pathname(re.sub('^file://', '', uri))
|
||||
return path.encode('latin1').decode('utf-8') # Undo double encoding
|
||||
|
||||
def find_files(path):
|
||||
if os.path.isfile(path):
|
||||
yield os.path.abspath(path)
|
||||
|
||||
@ -6,7 +6,8 @@ import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from mopidy.utils.path import get_or_create_folder, path_to_uri, find_files
|
||||
from mopidy.utils.path import (get_or_create_folder,
|
||||
path_to_uri, uri_to_path, find_files)
|
||||
|
||||
from tests import SkipTest, data_folder
|
||||
|
||||
@ -71,6 +72,32 @@ class PathToFileURITest(unittest.TestCase):
|
||||
self.assertEqual(result, u'file:///tmp/%C3%A6%C3%B8%C3%A5')
|
||||
|
||||
|
||||
class UriToPathTest(unittest.TestCase):
|
||||
def test_simple_uri(self):
|
||||
if sys.platform == 'win32':
|
||||
result = uri_to_path('file:///C://WINDOWS/clock.avi')
|
||||
self.assertEqual(result, u'C:/WINDOWS/clock.avi')
|
||||
else:
|
||||
result = uri_to_path('file:///etc/fstab')
|
||||
self.assertEqual(result, u'/etc/fstab')
|
||||
|
||||
def test_space_in_uri(self):
|
||||
if sys.platform == 'win32':
|
||||
result = uri_to_path('file:///C://test%20this')
|
||||
self.assertEqual(result, u'C:/test this')
|
||||
else:
|
||||
result = uri_to_path(u'file:///tmp/test%20this')
|
||||
self.assertEqual(result, u'/tmp/test this')
|
||||
|
||||
def test_unicode_in_uri(self):
|
||||
if sys.platform == 'win32':
|
||||
result = uri_to_path( 'file:///C://%C3%A6%C3%B8%C3%A5')
|
||||
self.assertEqual(result, u'C:/æøå')
|
||||
else:
|
||||
result = uri_to_path(u'file:///tmp/%C3%A6%C3%B8%C3%A5')
|
||||
self.assertEqual(result, u'/tmp/æøå')
|
||||
|
||||
|
||||
class FindFilesTest(unittest.TestCase):
|
||||
def find(self, path):
|
||||
return list(find_files(data_folder(path)))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user