Ticket #2093: urlpath.diff

File urlpath.diff, 1.7 KB (added by oubiwann, 10 years ago)

diff with the new authority class added

  • twisted/python/urlpath.py

    old new  
    77import urlparse
    88import urllib
    99
     10class Authority:
     11    """
     12    An abstraction of the "authority" section of a URI.
     13
     14    Term taken from RFC2396.
     15    """
     16    def __init__(self, auth):
     17        self.auth = auth
     18        auths = auth.split('@')
     19        if len(auths) == 2:
     20            userpass = auths.pop(0)
     21            userpass = userpass.split(':')
     22            self.user = userpass.pop(0)
     23            try:
     24                self.password = userpass.pop(0)
     25            except:
     26                self.password = ''
     27        else:
     28            self.user = self.password = ''
     29        hostport = auths[0].split(':')
     30        self.host = hostport.pop(0) or ''
     31        try:
     32            self.port = int(hostport.pop(0))
     33        except:
     34            self.port = 0
     35
     36    def parse(self):
     37        """
     38        Return a tuple representing the parts of the authority.
     39        """
     40        return (self.user, self.password, self.host, self.port)
     41
     42    def __str__(self):
     43        return self.auth
     44
     45    def __repr__(self):
     46        return ('Authority(user=%r, password=%r, host=%r, port=%r)'
     47                % self.parse())
     48           
     49
    1050class URLPath:
    1151    def __init__(self, scheme='', netloc='localhost', path='',
    1252                 query='', fragment=''):
     
    1555        self.path = path or '/'
    1656        self.query = query
    1757        self.fragment = fragment
     58        auth = Authority(netloc)
     59        self.user, self.password, self.host, self.port = auth.parse()
    1860
    1961    _qpathlist = None
    2062    _uqpathlist = None