Add copyright to files that miss copyright
[platform/upstream/mic.git] / mic / utils / safeurl.py
1 # Copyright (c) 2014 Intel, Inc.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the Free
5 # Software Foundation; version 2 of the License
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc., 59
14 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
16 """
17 This module provides a class SafeURL which can contain url/user/password read
18 from config file, and hide plain user and password when it print to screen
19 """
20 import os.path
21 import urllib
22 from urlparse import urlsplit, urlunsplit
23
24
25 def join_userpass(href, user, passwd):
26     """Return authenticated URL with user and passwd embeded"""
27     if not user and not passwd:
28         return href
29
30     if passwd:
31         userpass = '%s:%s' % (urllib.quote(user, safe=''),
32                               urllib.quote(passwd, safe=''))
33     else:
34         userpass = urllib.quote(user, safe='')
35
36     parts = urlsplit(href)
37     netloc = '%s@%s' % (userpass, parts[1])
38     comps = list(parts)
39     comps[1] = netloc
40     return urlunsplit(comps)
41
42
43 def split_userpass(href):
44     """Returns (href, user, passwd) of an authenticated URL"""
45     parts = urlsplit(href)
46
47     netloc = parts[1]
48     if '@' not in netloc:
49         return href, None, None
50
51     userpass, netloc = netloc.split('@', 1)
52     if ':' in userpass:
53         user, passwd = [ urllib.unquote(i)
54                            for i in userpass.split(':', 1) ]
55     else:
56         user, passwd = userpass, None
57
58     comps = list(parts)
59     comps[1] = netloc
60     return urlunsplit(comps), user, passwd
61
62
63 class SafeURL(str):
64     '''SafeURL can hide user info when it's printed to console.
65     Use property full to get url with user info
66     '''
67     def __new__(cls, urlstring, user=None, passwd=None):
68         """Imuutable object"""
69         href, user1, passwd1 = split_userpass(urlstring)
70         user = user if user else user1
71         passwd = passwd if passwd else passwd1
72
73         obj = super(SafeURL, cls).__new__(cls, href)
74         obj.user = user
75         obj.passwd = passwd
76         obj.full = join_userpass(href, user, passwd)
77
78         parts = urlsplit(href)
79         obj.scheme = parts[0]
80         obj.netloc = parts[1]
81         obj.path = parts[2]
82         obj.host = parts.hostname
83         obj.port = parts.port
84         return obj
85
86     def join(self, *path):
87         """Returns a new SafeURL with new path. Search part is removed since
88         after join path is changed, keep the same search part is useless.
89         """
90         idx = self.full.find('?')
91         url = self.full if idx < 0 else self.full[:idx]
92         return SafeURL(os.path.join(url.rstrip('/'), *path))