Version 1.0.0
added miniuri/__init__.py added miniuri/miniuri.py changed .hgignore changed README changed setup.py
This commit is contained in:
parent
0843cc1f2b
commit
ba32fbbe4d
5 changed files with 159 additions and 15 deletions
|
|
@ -7,3 +7,4 @@ syntax: glob
|
|||
*.egg*
|
||||
*.gz
|
||||
build/*
|
||||
.cache/*
|
||||
|
|
|
|||
35
README
35
README
|
|
@ -1,13 +1,20 @@
|
|||
How to install
|
||||
===================
|
||||
|
||||
easy_install miniuri
|
||||
|
||||
What does it do?
|
||||
====================
|
||||
miniuri
|
||||
#######
|
||||
|
||||
miniuri is a universal URI parser class.
|
||||
|
||||
It is written in 120 lines of Python.
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
pip install miniuri
|
||||
|
||||
What does it do?
|
||||
================
|
||||
|
||||
The parser grants access to the following attributes:
|
||||
|
||||
.. code-block:: python
|
||||
|
|
@ -21,10 +28,12 @@ The parser grants access to the following attributes:
|
|||
scheme authority | path | extension
|
||||
| |
|
||||
port filename
|
||||
How to use
|
||||
====================
|
||||
Tutorial
|
||||
========
|
||||
|
||||
This example shows how you can set and get any of the URI attributes::
|
||||
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" )
|
||||
|
|
@ -40,19 +49,19 @@ This example shows how you can set and get any of the URI attributes::
|
|||
https://max:pass@www.foxhop.net:81/path/filename.jpg?p=2#5
|
||||
|
||||
How do I thank you?
|
||||
=======================
|
||||
===================
|
||||
|
||||
You should follow me on http://twitter.com/russellbal
|
||||
|
||||
|
||||
License
|
||||
=======================
|
||||
===================
|
||||
|
||||
Public Domain
|
||||
|
||||
|
||||
Public Revision Control
|
||||
=========================
|
||||
=======================
|
||||
|
||||
https://bitbucket.org/russellballestrini/miniuri/overview
|
||||
|
||||
|
|
|
|||
1
miniuri/__init__.py
Normal file
1
miniuri/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .miniuri import Uri
|
||||
121
miniuri/miniuri.py
Normal file
121
miniuri/miniuri.py
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
class Uri( object ):
|
||||
"""
|
||||
miniuri is a universal URI parser class.
|
||||
The parser grants access to the following attributes:
|
||||
|
||||
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
|
||||
"""
|
||||
def __init__( self, uri = None ):
|
||||
if uri: self.uri = uri # invoke uri.setter
|
||||
|
||||
def __str__(self): return self.uri
|
||||
|
||||
@property
|
||||
def uri( self ):
|
||||
"""build and return uri from attributes"""
|
||||
scheme = path = filename = query = fragment = ''
|
||||
if self.scheme: scheme = self.scheme + '://'
|
||||
if self.path: path = self.path
|
||||
if self.query: query = '?' + self.query
|
||||
if self.fragment: fragment = '#' + self.fragment
|
||||
return ''.join([scheme,self.authority,path,filename,query,fragment])
|
||||
|
||||
@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.extension = self.query = self.fragment = None
|
||||
|
||||
if '://' in uri: # attempt to parse scheme
|
||||
self.scheme, uri = uri.split( '://' )
|
||||
|
||||
# invoke authority setter
|
||||
self.authority = uri.split( '/' )[0]
|
||||
|
||||
uri = uri[ len( self.authority ): ]
|
||||
|
||||
if '#' in uri: # set fragment
|
||||
uri, self.fragment = uri.split( '#' )
|
||||
|
||||
if '?' in uri:
|
||||
self.path, self.query = uri.split( '?' )
|
||||
else:
|
||||
self.path = uri
|
||||
|
||||
@property
|
||||
def authority( self ):
|
||||
"""return a authority string from attributes"""
|
||||
a = ''
|
||||
if self.username:
|
||||
a += self.username
|
||||
if self.password: a += ':' + self.password
|
||||
a += '@'
|
||||
a += self.hostname
|
||||
if self.port: a += ':' + self.port
|
||||
return a
|
||||
|
||||
@authority.setter
|
||||
def authority( self, a ):
|
||||
"""set all the attribute that makeup a authority"""
|
||||
self.username = self.password = self.port = None
|
||||
self.hostname = a
|
||||
if '@' in self.hostname:
|
||||
self.userinfo, self.hostname = a.split( '@' ) # userinfo setter
|
||||
if ':' in self.hostname:
|
||||
self.hostname, self.port = self.hostname.split( ':' )
|
||||
|
||||
@property
|
||||
def userinfo( self ):
|
||||
"""return username:password, username, or None"""
|
||||
if self.username:
|
||||
if self.password:
|
||||
return self.username + ':' + self.password
|
||||
return self.username
|
||||
|
||||
@userinfo.setter
|
||||
def userinfo( self, info ):
|
||||
"""set username and password"""
|
||||
self.username, self.password = info, None
|
||||
if ':' in info:
|
||||
self.username, self.password = info.split( ':' )
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
"""return path"""
|
||||
p = self._path if self._path is not None else ''
|
||||
f = self.filename if self.filename is not None else ''
|
||||
return p + f
|
||||
|
||||
@path.setter
|
||||
def path(self, new_path):
|
||||
if new_path:
|
||||
self.filename = new_path.split('/')[-1]
|
||||
self._path = new_path.rstrip(self.filename)
|
||||
else:
|
||||
self._path = None
|
||||
self._filename = None
|
||||
self._extension = None
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
"""return filename"""
|
||||
if self._filename:
|
||||
if self.extension:
|
||||
return self._filename + '.' + self.extension
|
||||
return self._filename
|
||||
|
||||
@filename.setter
|
||||
def filename(self, new_filename):
|
||||
if new_filename:
|
||||
self.extension = new_filename.split('.')[-1] if '.' in new_filename else None
|
||||
self._filename = new_filename.split('.')[0]
|
||||
|
||||
16
setup.py
16
setup.py
|
|
@ -1,10 +1,10 @@
|
|||
# installation: easy_install miniuri
|
||||
# installation: pip install miniuri
|
||||
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='miniuri',
|
||||
version='0.0.2',
|
||||
version='1.0.0',
|
||||
long_description = open('README').read(),
|
||||
description='miniuri: The Universal URI Parser',
|
||||
keywords = 'miniuri uri url parser',
|
||||
|
|
@ -18,6 +18,18 @@ setup(
|
|||
|
||||
py_modules = ['miniuri'],
|
||||
include_package_data=True,
|
||||
|
||||
classifiers = [
|
||||
# Specify the Python versions you support here. In particular, ensure
|
||||
# that you indicate whether you support Python 2, Python 3 or both.
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.2',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
],
|
||||
)
|
||||
|
||||
# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue