X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=htsworkflow.git;a=blobdiff_plain;f=htsworkflow%2Futil%2Furl.py;h=503e9e38056e02e4d749a32eb9e492d0c66b57d2;hp=4e49c2dc4b41f63ac3ab199960225ee320a40408;hb=23e4222a5cd431b0a59f78fe8dc618f02425303d;hpb=0be6d8df8447d45048189a9cdf94a2077d68e035 diff --git a/htsworkflow/util/url.py b/htsworkflow/util/url.py index 4e49c2d..503e9e3 100644 --- a/htsworkflow/util/url.py +++ b/htsworkflow/util/url.py @@ -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 :") + + 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) +