From d785b9b14e52d01b5b05564d37423cb49c22a0ef Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 29 Oct 2010 22:16:33 +0200 Subject: [PATCH] Added uri_to_path with tests --- mopidy/utils/path.py | 8 ++++++++ tests/utils/path_test.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 9220c53d..73d89d49 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -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) diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index e0359a16..c60e7833 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -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)))