added better docstring
This commit is contained in:
parent
ae5dc75bb8
commit
6399d25cad
1 changed files with 33 additions and 14 deletions
47
miniuri.py
47
miniuri.py
|
|
@ -1,21 +1,34 @@
|
||||||
class Uri( object ):
|
class Uri( object ):
|
||||||
"""
|
"""
|
||||||
>>> from miniuri import Uri
|
This is a universal URI parser class.
|
||||||
>>> u = Uri( "http://www.foxhop.net/samsung/HL-T5087SA/red-LED-failure" )
|
Pass a URI string in for access to the following attributes:
|
||||||
>>> u.uri = "https://fox:pass@www.foxhop.net:8080/path/filename.jpg?p=2#comment"
|
|
||||||
|
|
||||||
foo://username:password@example.com:8042/over/there/index.php?pet=cat&name=bam#nose
|
foo://username:password@test.com:808/go/to/index.php?pet=cat&name=bam#eye
|
||||||
\_/ \_______________/ \_________/ \__/ \___/ \_/ \______________/ \__/
|
\_/ \_______________/ \______/ \_/ \___/ \_/ \_______________/\_/
|
||||||
| | | | | | | |
|
| | | | | | | |
|
||||||
| userinfo hostname port | | query fragment
|
| userinfo hostname | | | query fragment
|
||||||
| \________________________________/\_____________|____|/
|
| \___________________________|/\________|____|_/
|
||||||
scheme | | | |
|
| | | | | |
|
||||||
authority path | extension
|
scheme authority | path | extension
|
||||||
|
|
| |
|
||||||
filename
|
port filename
|
||||||
|
|
||||||
|
This example shows how you can set and get any of the URI attributes:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> from miniuri import Uri
|
||||||
|
>>> u = Uri( "http://www.foxhop.net/samsung/HL-T5087SA/red-LED-failure" )
|
||||||
|
>>> u.uri = "https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5"
|
||||||
|
>>> print u.uri
|
||||||
|
https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5
|
||||||
|
>>> print u.hostname
|
||||||
|
www.foxhop.net
|
||||||
|
>>> print u.scheme
|
||||||
|
https
|
||||||
|
>>> u.username = 'max'
|
||||||
|
>>> print u
|
||||||
|
https://max:pass@www.foxhop.net:81/path/filename.jpg?p=2#5
|
||||||
"""
|
"""
|
||||||
scheme = username = password = hostname = port = None
|
|
||||||
path = filename = query = fragment = None
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -34,6 +47,10 @@ class Uri( object ):
|
||||||
@uri.setter
|
@uri.setter
|
||||||
def uri( self, uri ):
|
def uri( self, uri ):
|
||||||
"""parse and set all uri attributes"""
|
"""parse and set all uri attributes"""
|
||||||
|
self.scheme = self.username = self.password = None
|
||||||
|
self.hostname = self.port = self.path = None
|
||||||
|
self.filename = self.query = self.fragment = None
|
||||||
|
|
||||||
if '://' in uri: # attempt to parse scheme
|
if '://' in uri: # attempt to parse scheme
|
||||||
self.scheme, uri = uri.split( '://' )
|
self.scheme, uri = uri.split( '://' )
|
||||||
|
|
||||||
|
|
@ -100,3 +117,5 @@ class Uri( object ):
|
||||||
p = self.uri
|
p = self.uri
|
||||||
if self.scheme: p = p[ len(self.scheme) + 3: ]
|
if self.scheme: p = p[ len(self.scheme) + 3: ]
|
||||||
return p.split( '/' )
|
return p.split( '/' )
|
||||||
|
|
||||||
|
def __str__( self ): return self.uri
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue