Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 75 additions and 29 deletions.
  1. +7 −6 README.rst
  2. +3 −0  TODO
  3. +57 −23 purl/__init__.py
  4. +8 −0 tests/tests.py
View
13 README.rst
@@ -23,19 +23,20 @@ Construct::
>>> from purl import URL
- # Explicit constructor
- >>> u = URL(scheme='https', host='www.google.com', path='/search', query='q=testing')
+ # String constructor
+ >>> from_str = URL('https://www.google.com/search?q=testing')
- # Use factory
- >>> u = URL.from_string('https://www.google.com/search?q=testing')
+ # Keyword constructor
+ >>> from_kwargs = URL(scheme='https', host='www.google.com', path='/search', query='q=testing')
# Combine
- >>> u = URL.from_string('https://www.google.com').path('search').query_param('q', 'testing')
+ >>> from_combo = URL('https://www.google.com').path('search').query_param('q', 'testing')
URL objects are immutable - all mutator methods return a new instance.
Interrogate::
+ >>> u = URL('https://www.google.com/search?q=testing')
>>> u.scheme()
'https'
>>> u.host()
@@ -143,7 +144,7 @@ Clone and install testing dependencies::
Ensure tests pass::
- nosetests
+ ./runtests.sh
Hack away
View
3  TODO
@@ -1,4 +1,7 @@
Rework constructor to take string
+- warning on old method
+- passing both string and params
+
query method?
maybe rethink the jquery style
clone method
View
80 purl/__init__.py
@@ -7,13 +7,64 @@
_URLTuple = namedtuple("_URLTuple", "host username password scheme port path query fragment")
+def parse(url_str):
+ """
+ Extract all parts from a URL string and return them as a dictionary
+ """
+ result = urlparse.urlparse(url_str)
+ netloc_parts = result.netloc.split('@')
+ if len(netloc_parts) == 1:
+ username = password = None
+ host = netloc_parts[0]
+ else:
+ username, password = netloc_parts[0].split(':')
+ host = netloc_parts[1]
+
+ if host and ':' in host:
+ host = host.split(':')[0]
+
+ return {'host': host,
+ 'username': username,
+ 'password': password,
+ 'scheme': result.scheme,
+ 'port': result.port,
+ 'path': result.path,
+ 'query': result.query,
+ 'fragment': result.fragment}
+
+
class URL(object):
__slots__ = ("_tuple",)
- def __init__(self, host=None, username=None, password=None, scheme='http',
- port=None, path='/', query=None, fragment=None):
- self._tuple = _URLTuple(host, username, password, scheme, port, path, query, fragment)
+ def __init__(self, url_str=None, host=None, username=None, password=None,
+ scheme=None, port=None, path=None, query=None, fragment=None):
+ if url_str is not None:
+ params = parse(url_str)
+ else:
+ # Defaults
+ params = {'scheme': 'http',
+ 'username': None,
+ 'password': None,
+ 'host': None,
+ 'port': None,
+ 'path': '/',
+ 'query': None,
+ 'fragment': None}
+
+ # Kwargs override the url_str
+ for var in 'host username password scheme port path query fragment'.split():
+ if locals()[var] is not None:
+ params[var] = locals()[var]
+
+ self._tuple = _URLTuple(params['host'],
+ params['username'],
+ params['password'],
+ params['scheme'],
+ params['port'],
+ params['path'],
+ params['query'],
+ params['fragment'])
def __eq__(self, other):
return self._tuple == other._tuple
@@ -181,24 +232,7 @@ def _mutate(cls, url, **kwargs):
def from_string(cls, url_str):
"""
Factory method to create a new instance based on a passed string
+
+ This method is deprecated now
"""
- result = urlparse.urlparse(url_str)
- netloc_parts = result.netloc.split('@')
- if len(netloc_parts) == 1:
- username = password = None
- host = netloc_parts[0]
- else:
- username, password = netloc_parts[0].split(':')
- host = netloc_parts[1]
-
- if host and ':' in host:
- host = host.split(':')[0]
-
- return cls(host=host,
- username=username,
- password=password,
- scheme=result.scheme,
- port=result.port,
- path=result.path,
- query=result.query,
- fragment=result.fragment)
+ return cls(url_str)
View
8 tests/tests.py
@@ -25,6 +25,14 @@ def test_no_args_to_constructor(self):
def test_as_string(self):
self.assertEqual('/', URL().as_string())
+ def test_full_url_can_be_used_as_first_param(self):
+ u = URL('https://github.com')
+ self.assertEqual('https://github.com', u.as_string())
+
+ def test_kwargs_take_priority_when_used_with_full_url(self):
+ u = URL('https://github.com', scheme='http')
+ self.assertEqual('http://github.com', u.as_string())
+
class MoreFactoryTests(TestCase):

No commit comments for this range

Something went wrong with that request. Please try again.