← Back to team overview

spv-dev team mailing list archive

[Branch ~bouf10/slidepresenterview/refactor-pres-design] Rev 61: Add a function to convert a path to an URI + some expected behaviour for Uri class.

 

------------------------------------------------------------
revno: 61
committer: F.A. Bourbonnais <bouf10pub@xxxxxxxxxxx>
branch nick: refactor-pres-design
timestamp: Sun 2010-05-09 01:50:36 -0400
message:
  Add a function to convert a path to an URI + some expected behaviour for Uri class.
modified:
  slidepresenterview/tests/unit/utils/tests_uri.py
  slidepresenterview/utils/uri.py


--
lp:~bouf10/slidepresenterview/refactor-pres-design
https://code.launchpad.net/~bouf10/slidepresenterview/refactor-pres-design

Your team SlidePresenterView Development Team is subscribed to branch lp:~bouf10/slidepresenterview/refactor-pres-design.
To unsubscribe from this branch go to https://code.launchpad.net/~bouf10/slidepresenterview/refactor-pres-design/+edit-subscription
=== modified file 'slidepresenterview/tests/unit/utils/tests_uri.py'
--- slidepresenterview/tests/unit/utils/tests_uri.py	2010-05-09 03:18:25 +0000
+++ slidepresenterview/tests/unit/utils/tests_uri.py	2010-05-09 05:50:36 +0000
@@ -24,8 +24,85 @@
 
 import unittest
 
+from mock import patch, Mock
+
+from slidepresenterview.utils.uri import file_path_to_uri
 from slidepresenterview.utils.uri import Uri
 
+def _fake_abspath(path):
+    if len(path) == 0 or path[0] != '/':
+        return '/root/%s' % path
+    return path
+
+
+class TestFilePathUnixToUri(unittest.TestCase): #pylint: disable-msg=R0904
+    
+    def setUp(self):
+        self.path_filename = 'file.pdf'
+        self.path_rel = 'sub/file.pdf'
+        self.path_abs = '/complete/path/to/file.pdf'
+        self.path_unicode = u'sé/filé.pdf'
+        self.path_not_norm = '//dir/../dir2/./a//file.pdf'
+        
+    
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def test_empty_path_should_be_the_current_dir(self):
+        uri = file_path_to_uri('')
+        self.assertEquals('file:///root', uri.uri)    
+        
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def should_procude_a_valid_uri(self):
+        uri = file_path_to_uri(self.path_abs)
+        self.assertEquals('file:///complete/path/to/file.pdf', uri.uri)
+        
+        
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def test_filename_should_return_an_absolute_path(self):
+        uri = file_path_to_uri(self.path_filename)
+        self.assertEquals('/root/file.pdf', uri.path)
+        
+        
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def test_relative_path_should_return_an_absolute_path(self):
+        uri = file_path_to_uri(self.path_rel)
+        self.assertEquals('/root/sub/file.pdf', uri.path)
+        
+    
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def should_be_normalised(self):
+        uri = file_path_to_uri(self.path_not_norm)
+        self.assertEquals('//dir2/a/file.pdf', uri.path)
+        
+    
+    @patch('os.path.abspath', Mock(side_effect=_fake_abspath))
+    def should_accept_unicode(self):
+        uri = file_path_to_uri(self.path_unicode)
+        self.assertEquals(u'/root/sé/filé.pdf', uri.path)
+        
+        
+class TestFilePathWinToUri(unittest.TestCase):
+    
+    def setUp(self):
+        self.path_win = u'c:/document and settings/aée/My Documents/file.pdf'
+        self.path_win_not_norm = u'c:\document and settings/a\\ée//file.pdf'
+        
+    def should_todo(self):
+        self.fail("Not implemented.")
+
+
+class TestGivenInvalidUri(unittest.TestCase):
+    
+    def test_empty_should_raise_valueerror(self):
+        self.assertRaises(ValueError, Uri, '')
+    
+    def test_no_scheme_should_raise_valueerror(self):
+        self.assertRaises(ValueError, Uri, '/path')
+    
+    def test_no_path_should_raise_valueerror(self):
+        self.assertRaises(ValueError, Uri, 'http:')
+        self.assertRaises(ValueError, Uri, 'http://')
+
+
 class TestGivenFullAsciiUri(unittest.TestCase):
     
     def setUp(self):

=== modified file 'slidepresenterview/utils/uri.py'
--- slidepresenterview/utils/uri.py	2010-05-09 03:18:25 +0000
+++ slidepresenterview/utils/uri.py	2010-05-09 05:50:36 +0000
@@ -22,9 +22,24 @@
 Utilities to manipulate URIs
 """
 
+import os
 from urllib2 import urlparse
 
-__all__ = ['Uri']
+__all__ = ['Uri', 'file_path_to_uri']
+
+
+def file_path_to_uri(file_path):
+    """
+    Converts a file path into a full Uri.
+    
+    If the path is relative, it will be extended to the corresponding
+    absolute location.
+    
+    Paths are normalised.
+    """
+    file_path = os.path.abspath(file_path)
+    file_path = os.path.normpath(file_path)
+    return Uri("file://%s" % file_path)
 
 
 class Uri(object):
@@ -64,6 +79,12 @@
     """
     
     def __init__(self, uri_string):
+        """
+        @raise ValueError: Invalid (malformed) uri string.
+        """
+        if len(uri_string) == 0:
+            raise ValueError("Invalid (empty) URI.")
+        
         uri_split = urlparse.urlsplit(uri_string, allow_fragments=False)
         
         self.scheme = uri_split.scheme
@@ -74,6 +95,11 @@
             self.netloc = uri_split.netloc
             self.path = uri_split.path
         self.query = uri_split.query
+        
+        if len(self.scheme) == 0:
+            raise ValueError("Invalid URI: scheme is mandatory.")
+        if len(self.path) == 0:
+            raise ValueError("Invalid URI: path is mandatory.")
     
     
     @property