4e49c2dc4b41f63ac3ab199960225ee320a40408
[htsworkflow.git] / htsworkflow / util / url.py
1 """
2 Utilities to help handle urls
3 """
4
5 def normalize_url(url, scheme='http'):
6     """
7     Make sure there is a http at the head of what should be a url
8
9     >>> normalize_url("google.com")
10     'http://google.com'
11     >>> normalize_url("http://google.com")
12     'http://google.com'
13     >>> normalize_url("foo.com/a/b/c/d/e/f.html")
14     'http://foo.com/a/b/c/d/e/f.html'
15     >>> normalize_url("foo.com", "https")
16     'https://foo.com'
17     >>> normalize_url(None)
18     """
19     # not much to do with None except avoid an exception
20     if url is None:
21         return None
22     
23     scheme_sep = '://'
24     if url.find(scheme_sep) != -1:
25         return url
26     else:
27         return scheme + scheme_sep + url