cleaning up code, removing unreferenced method and comments
changed miniuri.py
This commit is contained in:
parent
671ecd408a
commit
3f1f60a132
1 changed files with 12 additions and 29 deletions
41
miniuri.py
41
miniuri.py
|
|
@ -12,27 +12,18 @@ class Uri( object ):
|
||||||
scheme authority | path | extension
|
scheme authority | path | extension
|
||||||
| |
|
| |
|
||||||
port filename
|
port filename
|
||||||
|
|
||||||
How to use:
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
>>> from miniuri import Uri
|
|
||||||
>>> u = Uri("https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5")
|
|
||||||
>>> print u.hostname
|
|
||||||
www.foxhop.net
|
|
||||||
>>> u.username = 'max'
|
|
||||||
>>> print u.uri
|
|
||||||
https://max:pass@www.foxhop.net:81/path/filename.jpg?p=2#5
|
|
||||||
"""
|
"""
|
||||||
def __init__( self, uri = None ):
|
def __init__( self, uri = None ):
|
||||||
if uri: self.uri = uri # invoke uri.setter
|
if uri: self.uri = uri # invoke uri.setter
|
||||||
|
|
||||||
|
def __str__(self): return self.uri
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def uri( self ):
|
def uri( self ):
|
||||||
"""build and return uri from attributes"""
|
"""build and return uri from attributes"""
|
||||||
scheme = path = filename = query = fragment = ''
|
scheme = path = filename = query = fragment = ''
|
||||||
if self.path: path = '/'.join( self.path.split('/')[:-1] )
|
|
||||||
if self.scheme: scheme = self.scheme + '://'
|
if self.scheme: scheme = self.scheme + '://'
|
||||||
|
if self.path: path = '/'.join(self.path.split('/')[:-1])
|
||||||
if self.filename: filename = '/' + self.filename
|
if self.filename: filename = '/' + self.filename
|
||||||
if self.query: query = '?' + self.query
|
if self.query: query = '?' + self.query
|
||||||
if self.fragment: fragment = '#' + self.fragment
|
if self.fragment: fragment = '#' + self.fragment
|
||||||
|
|
@ -79,18 +70,18 @@ class Uri( object ):
|
||||||
"""set all the attribute that makeup a authority"""
|
"""set all the attribute that makeup a authority"""
|
||||||
self.username = self.password = self.port = None
|
self.username = self.password = self.port = None
|
||||||
self.hostname = a
|
self.hostname = a
|
||||||
if '@' in a:
|
if '@' in self.hostname:
|
||||||
self.userinfo, self.hostname = a.split( '@' ) # userinfo setter
|
self.userinfo, self.hostname = a.split( '@' ) # userinfo setter
|
||||||
if ':' in self.hostname:
|
if ':' in self.hostname:
|
||||||
self.hostname, self.port = self.hostname.split( ':' )
|
self.hostname, self.port = self.hostname.split( ':' )
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def userinfo( self ):
|
def userinfo( self ):
|
||||||
"""return username:password"""
|
"""return username:password, username, or None"""
|
||||||
if self.username and self.password:
|
if self.username:
|
||||||
return self.username + ':' + self.password
|
if self.password:
|
||||||
if self.username: return self.username
|
return self.username + ':' + self.password
|
||||||
else: return None
|
return self.username
|
||||||
|
|
||||||
@userinfo.setter
|
@userinfo.setter
|
||||||
def userinfo( self, info ):
|
def userinfo( self, info ):
|
||||||
|
|
@ -100,16 +91,8 @@ class Uri( object ):
|
||||||
self.username, self.password = info.split( ':' )
|
self.username, self.password = info.split( ':' )
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extension( self ):
|
def extension(self):
|
||||||
"""return extension"""
|
"""return filename extension"""
|
||||||
if '.' in self.filename: return self.filename.split('.')[-1]
|
return self.filename.split('.')[-1] if '.' in self.filename else None
|
||||||
else: return None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def parts( self ):
|
|
||||||
"""remove scheme then return a list of uri parts splitting on / """
|
|
||||||
p = self.uri
|
|
||||||
if self.scheme: p = p[ len(self.scheme) + 3: ]
|
|
||||||
return p.split( '/' )
|
|
||||||
|
|
||||||
def __str__( self ): return self.uri
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue