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 ):
|
||||
"""
|
||||
>>> from miniuri import Uri
|
||||
>>> u = Uri( "http://www.foxhop.net/samsung/HL-T5087SA/red-LED-failure" )
|
||||
>>> u.uri = "https://fox:pass@www.foxhop.net:8080/path/filename.jpg?p=2#comment"
|
||||
This is a universal URI parser class.
|
||||
Pass a URI string in for access to the following attributes:
|
||||
|
||||
foo://username:password@example.com:8042/over/there/index.php?pet=cat&name=bam#nose
|
||||
\_/ \_______________/ \_________/ \__/ \___/ \_/ \______________/ \__/
|
||||
| | | | | | | |
|
||||
| userinfo hostname port | | query fragment
|
||||
| \________________________________/\_____________|____|/
|
||||
scheme | | | |
|
||||
authority path | extension
|
||||
|
|
||||
filename
|
||||
foo://username:password@test.com:808/go/to/index.php?pet=cat&name=bam#eye
|
||||
\_/ \_______________/ \______/ \_/ \___/ \_/ \_______________/\_/
|
||||
| | | | | | | |
|
||||
| userinfo hostname | | | query fragment
|
||||
| \___________________________|/\________|____|_/
|
||||
| | | | | |
|
||||
scheme authority | path | extension
|
||||
| |
|
||||
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 ):
|
||||
if uri: self.uri = uri # invoke uri.setter
|
||||
|
|
@ -34,6 +47,10 @@ class Uri( object ):
|
|||
@uri.setter
|
||||
def uri( self, uri ):
|
||||
"""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
|
||||
self.scheme, uri = uri.split( '://' )
|
||||
|
||||
|
|
@ -100,3 +117,5 @@ class Uri( object ):
|
|||
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