[Zope-CVS] SVN: zpkgtools/trunk/zpkgsetup/ refactor urlutils to add a pathname2url() that works consistently

Fred L. Drake, Jr. fdrake at gmail.com
Fri Sep 17 13:01:47 EDT 2004


Log message for revision 27612:
  refactor urlutils to add a pathname2url() that works consistently


Changed:
  U   zpkgtools/trunk/zpkgsetup/tests/test_urlutils.py
  U   zpkgtools/trunk/zpkgsetup/urlutils.py


-=-
Modified: zpkgtools/trunk/zpkgsetup/tests/test_urlutils.py
===================================================================
--- zpkgtools/trunk/zpkgsetup/tests/test_urlutils.py	2004-09-17 16:42:56 UTC (rev 27611)
+++ zpkgtools/trunk/zpkgsetup/tests/test_urlutils.py	2004-09-17 17:01:47 UTC (rev 27612)
@@ -27,7 +27,7 @@
     This should only be used on Windows systems.
     """
 
-    def test_with_drive_letter(self):
+    def test_file_url_with_drive_letter(self):
         self.assertEqual(urlutils.file_url("c:\\some\\file.txt"),
                          "file:///C|/some/file.txt")
         self.assertEqual(urlutils.file_url("c:some\\file.txt"),
@@ -37,7 +37,7 @@
         self.assertEqual(urlutils.file_url("\\some\\folder\\"),
                          "file:///some/folder")
 
-    def test_without_drive_letter(self):
+    def test_file_url_without_drive_letter(self):
         self.assertEqual(urlutils.file_url("\\some\\file.txt"),
                          "file:///some/file.txt")
         self.assertEqual(urlutils.file_url("\\some\\folder"),
@@ -45,13 +45,31 @@
         self.assertEqual(urlutils.file_url("\\some\\folder\\"),
                          "file:///some/folder")
 
+    def test_pathname2url_with_drive_letter(self):
+        self.assertEqual(urlutils.pathname2url("c:\\some\\file.txt"),
+                         "file:///C|/some/file.txt")
+        self.assertEqual(urlutils.pathname2url("c:some\\file.txt"),
+                         "file:///C|/some/file.txt")
+        self.assertEqual(urlutils.pathname2url("\\some\\folder"),
+                         "file:///some/folder")
+        self.assertEqual(urlutils.pathname2url("\\some\\folder\\"),
+                         "file:///some/folder")
 
+    def test_pathname2url_without_drive_letter(self):
+        self.assertEqual(urlutils.pathname2url("\\some\\file.txt"),
+                         "/some/file.txt")
+        self.assertEqual(urlutils.pathname2url("\\some\\folder"),
+                         "/some/folder")
+        self.assertEqual(urlutils.pathname2url("\\some\\folder\\"),
+                         "/some/folder")
+
+
 class PosixUrlutilsTestCase(unittest.TestCase):
     """Tests of POSIX path-to-URL conversions.
 
     This should only be used on Unix-like systems.
     """
-    def test_paths(self):
+    def test_file_url(self):
         self.assertEqual(urlutils.file_url("/some/file.txt"),
                          "file:///some/file.txt")
         self.assertEqual(urlutils.file_url("/some/folder"),
@@ -59,7 +77,15 @@
         self.assertEqual(urlutils.file_url("/some/folder/"),
                          "file:///some/folder")
 
+    def test_pathname2url(self):
+        self.assertEqual(urlutils.pathname2url("/some/file.txt"),
+                         "/some/file.txt")
+        self.assertEqual(urlutils.pathname2url("/some/folder"),
+                         "/some/folder")
+        self.assertEqual(urlutils.pathname2url("/some/folder/"),
+                         "/some/folder")
 
+
 def test_suite():
     if sys.platform[:3].lower().startswith("win"):
         testcls = WindowsUrlutilsTestCase

Modified: zpkgtools/trunk/zpkgsetup/urlutils.py
===================================================================
--- zpkgtools/trunk/zpkgsetup/urlutils.py	2004-09-17 16:42:56 UTC (rev 27611)
+++ zpkgtools/trunk/zpkgsetup/urlutils.py	2004-09-17 17:01:47 UTC (rev 27612)
@@ -20,8 +20,13 @@
 
 
 def file_url(path):
+    return "file://" + pathname2url(path)
+
+def pathname2url(path):
     urlpart = urllib.pathname2url(path)
+    # On Windows, pathname2url() returns too many slashes, or it
+    # returns too few on Unix.  This makes everything conform to the
+    # expectations for Unix.
     if urlpart.startswith("///"):
         urlpart = urlpart[2:]
-    urlpart = posixpath.normpath(urlpart)
-    return "file://" + urlpart
+    return posixpath.normpath(urlpart)



More information about the Zope-CVS mailing list