From ba32fbbe4d87d9f7dfa239f420ea6b7871572b67 Mon Sep 17 00:00:00 2001 From: russellballestrini Date: Sat, 23 Jan 2016 21:25:47 -0500 Subject: [PATCH] Version 1.0.0 added miniuri/__init__.py added miniuri/miniuri.py changed .hgignore changed README changed setup.py --- .hgignore | 1 + README | 35 ++++++++----- miniuri/__init__.py | 1 + miniuri/miniuri.py | 121 ++++++++++++++++++++++++++++++++++++++++++++ setup.py | 16 +++++- 5 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 miniuri/__init__.py create mode 100644 miniuri/miniuri.py diff --git a/.hgignore b/.hgignore index cef8f3e..ffd82f1 100644 --- a/.hgignore +++ b/.hgignore @@ -7,3 +7,4 @@ syntax: glob *.egg* *.gz build/* +.cache/* diff --git a/README b/README index 0fc86cc..c3c4f16 100644 --- a/README +++ b/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 diff --git a/miniuri/__init__.py b/miniuri/__init__.py new file mode 100644 index 0000000..0ddc531 --- /dev/null +++ b/miniuri/__init__.py @@ -0,0 +1 @@ +from .miniuri import Uri diff --git a/miniuri/miniuri.py b/miniuri/miniuri.py new file mode 100644 index 0000000..dc6b47e --- /dev/null +++ b/miniuri/miniuri.py @@ -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] + diff --git a/setup.py b/setup.py index 88f639a..a085e93 100644 --- a/setup.py +++ b/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