the result of running black .

This commit is contained in:
russellballestrini 2018-05-25 17:37:42 -04:00
parent ffc97260bd
commit f9a77a14fc
3 changed files with 201 additions and 167 deletions

View file

@ -1,4 +1,4 @@
class Uri( object ): class Uri(object):
""" """
miniuri is a universal URI parser class. miniuri is a universal URI parser class.
The parser grants access to the following attributes: The parser grants access to the following attributes:
@ -13,96 +13,104 @@ class Uri( object ):
| | | |
port filename port filename
""" """
def __init__( self, uri = None ):
if uri: self.uri = uri # invoke uri.setter
def __str__(self): return self.uri def __init__(self, uri=None):
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 = self.scheme + '://' if self.scheme else '' scheme = self.scheme + "://" if self.scheme else ""
return ''.join([scheme, self.authority, self.relative_uri]) return "".join([scheme, self.authority, self.relative_uri])
@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.scheme = self.username = self.password = None
self.hostname = self.port = self.path = None self.hostname = self.port = self.path = None
self.filename = self.query = self.fragment = None self.filename = self.query = self.fragment = None
if '://' in uri: if "://" in uri:
self.scheme, uri = uri.split( '://' ) self.scheme, uri = uri.split("://")
if '#' in uri: if "#" in uri:
uri, self.fragment = uri.split('#') uri, self.fragment = uri.split("#")
if '?' in uri: if "?" in uri:
uri, self.query = uri.split('?') uri, self.query = uri.split("?")
# invoke authority setter. # invoke authority setter.
self.authority = uri.split( '/' )[0] self.authority = uri.split("/")[0]
# invoke path setter. # invoke path setter.
self.path = uri[ len( self.authority ): ] self.path = uri[len(self.authority) :]
@property @property
def authority( self ): def authority(self):
"""return a authority string from attributes""" """return a authority string from attributes"""
a = '' a = ""
if self.username: if self.username:
a += self.username a += self.username
if self.password: a += ':' + self.password if self.password:
a += '@' a += ":" + self.password
a += "@"
a += self.hostname a += self.hostname
if self.port: a += ':' + self.port if self.port:
a += ":" + self.port
return a return a
@authority.setter @authority.setter
def authority( self, a ): def authority(self, a):
"""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 self.hostname: 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 relative_uri(self): def relative_uri(self):
"""return everything that isn't part of the authority.""" """return everything that isn't part of the authority."""
path = query = fragment = '' path = query = fragment = ""
if self.path: path = self.path if self.path:
if self.query: query = '?' + self.query path = self.path
if self.fragment: fragment = '#' + self.fragment if self.query:
return ''.join([path, query, fragment]) query = "?" + self.query
if self.fragment:
fragment = "#" + self.fragment
return "".join([path, query, fragment])
@property @property
def userinfo( self ): def userinfo(self):
"""return username:password, username, or None""" """return username:password, username, or None"""
if self.username: if self.username:
if self.password: if self.password:
return self.username + ':' + self.password return self.username + ":" + self.password
return self.username return self.username
@userinfo.setter @userinfo.setter
def userinfo( self, info ): def userinfo(self, info):
"""set username and password""" """set username and password"""
self.username, self.password = info, None self.username, self.password = info, None
if ':' in info: if ":" in info:
self.username, self.password = info.split( ':' ) self.username, self.password = info.split(":")
@property @property
def path(self): def path(self):
"""return path""" """return path"""
p = self._path if self._path is not None else '' p = self._path if self._path is not None else ""
f = self.filename if self.filename is not None else '' f = self.filename if self.filename is not None else ""
return p + f return p + f
@path.setter @path.setter
def path(self, new_path): def path(self, new_path):
if new_path: if new_path:
self.filename = new_path.split('/')[-1] self.filename = new_path.split("/")[-1]
self._path = new_path.rstrip(self.filename) self._path = new_path.rstrip(self.filename)
else: else:
self.filename = None self.filename = None
@ -111,18 +119,18 @@ class Uri( object ):
@property @property
def filename(self): def filename(self):
"""return filename""" """return filename"""
if self._filename: if self._filename:
if self.extension: if self.extension:
return '{}.{}'.format(self._filename, self.extension) return "{}.{}".format(self._filename, self.extension)
return self._filename return self._filename
return '' return ""
@filename.setter @filename.setter
def filename(self, new_filename): def filename(self, new_filename):
self.extension = '' self.extension = ""
if new_filename: if new_filename:
self._filename = new_filename.split('.')[0] self._filename = new_filename.split(".")[0]
if '.' in new_filename: if "." in new_filename:
self.extension = new_filename.split('.')[-1] self.extension = new_filename.split(".")[-1]
else: else:
self._filename = None self._filename = None

View file

@ -1,41 +1,34 @@
# installation: pip install miniuri # installation: pip install miniuri
from setuptools import ( from setuptools import setup, find_packages
setup,
find_packages,
)
setup( setup(
name='miniuri', name="miniuri",
version='1.0.5', version="1.0.5",
long_description = open('README.rst').read(), long_description=open("README.rst").read(),
description='miniuri: The Universal URI Parser', description="miniuri: The Universal URI Parser",
keywords = 'miniuri uri url parser', keywords="miniuri uri url parser",
author="Russell Ballestrini",
author = 'Russell Ballestrini', author_email="russell@ballestrini.net",
author_email = 'russell@ballestrini.net', url="https://bitbucket.org/russellballestrini/miniuri/src",
url = 'https://bitbucket.org/russellballestrini/miniuri/src', platforms=["All"],
license="Public Domain",
platforms=['All'], packages=find_packages(),
license='Public Domain',
packages = find_packages(),
include_package_data=True, include_package_data=True,
classifiers=[
classifiers = [
# Specify the Python versions you support here. In particular, ensure # Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both. # that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 2', "Programming Language :: Python :: 2",
'Programming Language :: Python :: 2.6', "Programming Language :: Python :: 2.6",
'Programming Language :: Python :: 2.7', "Programming Language :: Python :: 2.7",
'Programming Language :: Python :: 3', "Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.2', "Programming Language :: Python :: 3.2",
'Programming Language :: Python :: 3.3', "Programming Language :: Python :: 3.3",
'Programming Language :: Python :: 3.4', "Programming Language :: Python :: 3.4",
], ],
) )
# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools # setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools
# built and uploaded to pypi with this: # built and uploaded to pypi with this:
#python setup.py sdist bdist_egg register upload # python setup.py sdist bdist_egg register upload

View file

@ -2,152 +2,185 @@ from unittest import TestCase
from miniuri import Uri from miniuri import Uri
class TestMiniUri(TestCase): class TestMiniUri(TestCase):
def setUp(self): def setUp(self):
# "http://www.foxhop.net/samsung/HL-T5087SA/red-LED-failure" # "http://www.foxhop.net/samsung/HL-T5087SA/red-LED-failure"
self.u = Uri('https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5') self.u = Uri("https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5")
def test_uri(self): def test_uri(self):
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_scheme(self): def test_scheme(self):
self.assertEqual(self.u.scheme, 'https') self.assertEqual(self.u.scheme, "https")
def test_authority(self): def test_authority(self):
self.assertEqual(self.u.authority, 'fox:pass@www.foxhop.net:81') self.assertEqual(self.u.authority, "fox:pass@www.foxhop.net:81")
def test_userinfo(self): def test_userinfo(self):
self.assertEqual(self.u.userinfo, 'fox:pass') self.assertEqual(self.u.userinfo, "fox:pass")
def test_username(self): def test_username(self):
self.assertEqual(self.u.username, 'fox') self.assertEqual(self.u.username, "fox")
def test_password(self): def test_password(self):
self.assertEqual(self.u.password, 'pass') self.assertEqual(self.u.password, "pass")
def test_hostname(self): def test_hostname(self):
self.assertEqual(self.u.hostname, 'www.foxhop.net') self.assertEqual(self.u.hostname, "www.foxhop.net")
def test_port(self): def test_port(self):
self.assertEqual(self.u.port, '81') self.assertEqual(self.u.port, "81")
def test_path(self): def test_path(self):
self.assertEqual(self.u.path, '/path/filename.jpg') self.assertEqual(self.u.path, "/path/filename.jpg")
def test_filename(self): def test_filename(self):
self.assertEqual(self.u.filename, 'filename.jpg') self.assertEqual(self.u.filename, "filename.jpg")
def test_extension(self): def test_extension(self):
self.assertEqual(self.u.extension, 'jpg') self.assertEqual(self.u.extension, "jpg")
def test_query(self): def test_query(self):
self.assertEqual(self.u.query, 'p=2') self.assertEqual(self.u.query, "p=2")
def test_fragment(self): def test_fragment(self):
self.assertEqual(self.u.fragment, '5') self.assertEqual(self.u.fragment, "5")
# test edge case URIs # test edge case URIs
def test_uri_no_path(self): def test_uri_no_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81?p=2#5') self.u = Uri("https://fox:pass@www.foxhop.net:81?p=2#5")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81?p=2#5') self.assertEqual(self.u.uri, "https://fox:pass@www.foxhop.net:81?p=2#5")
self.assertEqual(self.u.username, 'fox') self.assertEqual(self.u.username, "fox")
self.assertEqual(self.u.password, 'pass') self.assertEqual(self.u.password, "pass")
self.assertEqual(self.u.hostname, 'www.foxhop.net') self.assertEqual(self.u.hostname, "www.foxhop.net")
self.assertEqual(self.u.port, '81') self.assertEqual(self.u.port, "81")
self.assertEqual(self.u.path, '') self.assertEqual(self.u.path, "")
self.assertEqual(self.u.filename, '') self.assertEqual(self.u.filename, "")
self.assertEqual(self.u.extension, '') self.assertEqual(self.u.extension, "")
def test_uri_filename_only_path(self): def test_uri_filename_only_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5') self.u = Uri("https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5') self.assertEqual(
self.assertEqual(self.u.username, 'fox') self.u.uri, "https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5"
self.assertEqual(self.u.password, 'pass') )
self.assertEqual(self.u.hostname, 'www.foxhop.net') self.assertEqual(self.u.username, "fox")
self.assertEqual(self.u.port, '81') self.assertEqual(self.u.password, "pass")
self.assertEqual(self.u.path, '/filename.jpg') self.assertEqual(self.u.hostname, "www.foxhop.net")
self.assertEqual(self.u.filename, 'filename.jpg') self.assertEqual(self.u.port, "81")
self.assertEqual(self.u.extension, 'jpg') self.assertEqual(self.u.path, "/filename.jpg")
self.assertEqual(self.u.filename, "filename.jpg")
self.assertEqual(self.u.extension, "jpg")
def test_uri_trailing_slash_only_path(self): def test_uri_trailing_slash_only_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81/?p=2#5') self.u = Uri("https://fox:pass@www.foxhop.net:81/?p=2#5")
self.assertEqual(self.u.path, '/') self.assertEqual(self.u.path, "/")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/?p=2#5') self.assertEqual(self.u.uri, "https://fox:pass@www.foxhop.net:81/?p=2#5")
# test_change_attributes:
# test_change_attributes:
def test_change_uri(self): def test_change_uri(self):
self.u.uri = 'http://zell:sapp@wwws.foxhop.net:82/my/file.png?p=3#6' self.u.uri = "http://zell:sapp@wwws.foxhop.net:82/my/file.png?p=3#6"
self.assertEqual(self.u.uri, 'http://zell:sapp@wwws.foxhop.net:82/my/file.png?p=3#6') self.assertEqual(
self.u.uri, "http://zell:sapp@wwws.foxhop.net:82/my/file.png?p=3#6"
)
def test_change_scheme(self): def test_change_scheme(self):
self.u.scheme = 'http' self.u.scheme = "http"
self.assertEqual(self.u.scheme, 'http') self.assertEqual(self.u.scheme, "http")
self.assertEqual(self.u.uri, 'http://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "http://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_change_authority(self): def test_change_authority(self):
self.u.authority = 'zell:ssap@wwws.foxhop.net:82' self.u.authority = "zell:ssap@wwws.foxhop.net:82"
self.assertEqual(self.u.authority, 'zell:ssap@wwws.foxhop.net:82') self.assertEqual(self.u.authority, "zell:ssap@wwws.foxhop.net:82")
self.assertEqual(self.u.uri, 'https://zell:ssap@wwws.foxhop.net:82/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://zell:ssap@wwws.foxhop.net:82/path/filename.jpg?p=2#5"
)
def test_change_userinfo(self): def test_change_userinfo(self):
self.u.userinfo = 'zell:ssap' self.u.userinfo = "zell:ssap"
self.assertEqual(self.u.userinfo, 'zell:ssap') self.assertEqual(self.u.userinfo, "zell:ssap")
self.assertEqual(self.u.uri, 'https://zell:ssap@www.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.userinfo = 'zell' self.u.uri, "https://zell:ssap@www.foxhop.net:81/path/filename.jpg?p=2#5"
self.assertEqual(self.u.userinfo, 'zell') )
self.assertEqual(self.u.uri, 'https://zell@www.foxhop.net:81/path/filename.jpg?p=2#5') self.u.userinfo = "zell"
self.assertEqual(self.u.userinfo, "zell")
self.assertEqual(
self.u.uri, "https://zell@www.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_change_username(self): def test_change_username(self):
self.u.username = 'zell' self.u.username = "zell"
self.assertEqual(self.u.username, 'zell') self.assertEqual(self.u.username, "zell")
self.assertEqual(self.u.uri, 'https://zell:pass@www.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://zell:pass@www.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_change_password(self): def test_change_password(self):
self.u.password = 'ssap' self.u.password = "ssap"
self.assertEqual(self.u.password, 'ssap') self.assertEqual(self.u.password, "ssap")
self.assertEqual(self.u.uri, 'https://fox:ssap@www.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://fox:ssap@www.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_change_hostname(self): def test_change_hostname(self):
self.u.hostname = 'wwws.foxhop.net' self.u.hostname = "wwws.foxhop.net"
self.assertEqual(self.u.hostname, 'wwws.foxhop.net') self.assertEqual(self.u.hostname, "wwws.foxhop.net")
self.assertEqual(self.u.uri, 'https://fox:pass@wwws.foxhop.net:81/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@wwws.foxhop.net:81/path/filename.jpg?p=2#5"
)
def test_change_port(self): def test_change_port(self):
self.u.port = '82' self.u.port = "82"
self.assertEqual(self.u.port, '82') self.assertEqual(self.u.port, "82")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:82/path/filename.jpg?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:82/path/filename.jpg?p=2#5"
)
def test_change_path(self): def test_change_path(self):
self.u.path = '/my/file.png' self.u.path = "/my/file.png"
self.assertEqual(self.u.path, '/my/file.png') self.assertEqual(self.u.path, "/my/file.png")
self.assertEqual(self.u.filename, 'file.png') self.assertEqual(self.u.filename, "file.png")
self.assertEqual(self.u.extension, 'png') self.assertEqual(self.u.extension, "png")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/my/file.png?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/my/file.png?p=2#5"
)
def test_change_filename(self): def test_change_filename(self):
self.u.filename = 'file.png' self.u.filename = "file.png"
self.assertEqual(self.u.filename, 'file.png') self.assertEqual(self.u.filename, "file.png")
self.assertEqual(self.u.extension, 'png') self.assertEqual(self.u.extension, "png")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/file.png?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/path/file.png?p=2#5"
)
def test_change_extension(self): def test_change_extension(self):
self.u.extension = 'png' self.u.extension = "png"
self.assertEqual(self.u.filename, 'filename.png') self.assertEqual(self.u.filename, "filename.png")
self.assertEqual(self.u.extension, 'png') self.assertEqual(self.u.extension, "png")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/filename.png?p=2#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/path/filename.png?p=2#5"
)
def test_change_query(self): def test_change_query(self):
self.u.query = 'p=3' self.u.query = "p=3"
self.assertEqual(self.u.query, 'p=3') self.assertEqual(self.u.query, "p=3")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=3#5') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=3#5"
)
def test_change_fragment(self): def test_change_fragment(self):
self.u.fragment = '6' self.u.fragment = "6"
self.assertEqual(self.u.fragment, '6') self.assertEqual(self.u.fragment, "6")
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#6') self.assertEqual(
self.u.uri, "https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#6"
)
def test_relative_uri(self): def test_relative_uri(self):
self.assertEqual(self.u.relative_uri, '/path/filename.jpg?p=2#5') self.assertEqual(self.u.relative_uri, "/path/filename.jpg?p=2#5")