Add function to parse scp / ssh style URLs.
authorDiane Trout <diane@ghic.org>
Wed, 17 Jul 2013 00:02:30 +0000 (17:02 -0700)
committerDiane Trout <diane@ghic.org>
Wed, 17 Jul 2013 00:02:30 +0000 (17:02 -0700)
Also move some tests around from htsworkflow.util.url

htsworkflow/util/test/test_url.py [new file with mode: 0644]
htsworkflow/util/url.py

diff --git a/htsworkflow/util/test/test_url.py b/htsworkflow/util/test/test_url.py
new file mode 100644 (file)
index 0000000..979e144
--- /dev/null
@@ -0,0 +1,46 @@
+from unittest2 import TestCase
+
+from htsworkflow.util.url import normalize_url, parse_ssh_url
+
+class TestURLUtilities(TestCase):
+    def test_normalize_url(self):
+
+        self.assertEqual(normalize_url('caltech.edu'), 
+                         'http://caltech.edu')
+        self.assertEqual(normalize_url('http://caltech.edu'),
+                         'http://caltech.edu')
+        self.assertEqual(normalize_url("foo.com/a/b/c/d/e/f.html"),
+                         'http://foo.com/a/b/c/d/e/f.html')
+        self.assertEqual(normalize_url("foo.com", "https"),
+                         'https://foo.com')
+        self.assertEqual(normalize_url(None),
+                         None)
+
+    def test_parse_ssh_url(self):
+
+        u = parse_ssh_url('me@caltech.edu:/test/path')
+        self.assertEqual(u.user, 'me')
+        self.assertEqual(u.host, 'caltech.edu')
+        self.assertEqual(u.path, '/test/path')
+
+        u = parse_ssh_url('caltech.edu:path@there')
+        self.assertEqual(u.user, None)
+        self.assertEqual(u.host, 'caltech.edu')
+        self.assertEqual(u.path, 'path@there')
+
+        u = parse_ssh_url('caltech.edu:C:/me/@work')
+        self.assertEqual(u.user, None)
+        self.assertEqual(u.host, 'caltech.edu')
+        self.assertEqual(u.path, 'C:/me/@work')
+
+        self.assertRaises(ValueError, parse_ssh_url, 'hello')
+        
+def suite():
+    from unittest2 import TestSuite, defaultTestLoader
+    suite = TestSuite()
+    suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestURLUtilities))
+    return suite
+
+if __name__ == '__main__':
+    from unittest2 import main
+    main(defaultTest="suite")
index 4e49c2dc4b41f63ac3ab199960225ee320a40408..503e9e38056e02e4d749a32eb9e492d0c66b57d2 100644 (file)
@@ -1,20 +1,11 @@
 """
 Utilities to help handle urls
 """
+import collections
 
 def normalize_url(url, scheme='http'):
     """
     Make sure there is a http at the head of what should be a url
-
-    >>> normalize_url("google.com")
-    'http://google.com'
-    >>> normalize_url("http://google.com")
-    'http://google.com'
-    >>> normalize_url("foo.com/a/b/c/d/e/f.html")
-    'http://foo.com/a/b/c/d/e/f.html'
-    >>> normalize_url("foo.com", "https")
-    'https://foo.com'
-    >>> normalize_url(None)
     """
     # not much to do with None except avoid an exception
     if url is None:
@@ -25,3 +16,30 @@ def normalize_url(url, scheme='http'):
         return url
     else:
         return scheme + scheme_sep + url
+
+SSHURL = collections.namedtuple("SSHURL", "user host path")
+
+def parse_ssh_url(url):
+    """Parse scp-style username, host and path.
+    """
+    # simple initialization
+    user = None
+    host = None
+    path = None
+    
+    colon = url.find(':')
+    if colon == -1:
+        raise ValueError("Invalid SSH URL: need <host>:<path>")
+    
+    path = url[colon+1:]
+    
+    user_host = url[:colon]
+    atsign = user_host.find('@')
+    if atsign != -1:
+        user = user_host[:atsign]
+        host = user_host[atsign+1:]
+    else:
+        host = user_host
+
+    return SSHURL(user, host, path)
+