Skip to content

Commit a06eedb

Browse files
committed
adding initial draft of parse and DecodedURL API docs. Also added default argument to DecodedURL() to ease programmatic construction. Fixes #125
1 parent 86a4736 commit a06eedb

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

docs/api.rst

+34-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,43 @@ Hyperlink API
55

66
.. automodule:: hyperlink._url
77

8+
.. contents::
9+
:local:
10+
811
Creation
912
--------
1013

11-
Before you can work with URLs, you must create URLs. There are two
12-
ways to create URLs, from parts and from text.
14+
Before you can work with URLs, you must create URLs.
15+
16+
Parsing Text
17+
^^^^^^^^^^^^
18+
19+
If you already have a textual URL, the easiest way to get URL objects
20+
is with the :func:`parse()` function:
21+
22+
.. autofunction:: hyperlink.parse
23+
24+
By default, :func:`~hyperlink.parse()` returns an instance of
25+
:class:`DecodedURL`, a URL type that handles all encoding for you, by
26+
wrapping the lower-level :class:`URL`.
27+
28+
DecodedURL
29+
^^^^^^^^^^
30+
31+
.. autoclass:: hyperlink.DecodedURL
32+
.. automethod:: hyperlink.DecodedURL.from_text
33+
34+
The Encoded URL
35+
^^^^^^^^^^^^^^^
36+
37+
The lower-level :class:`URL` looks very similar to the
38+
:class:`DecodedURL`, but does not handle all encoding cases for
39+
you. Use with caution.
40+
41+
.. note::
42+
43+
:class:`URL` is also available as an alias,
44+
``hyperlink.EncodedURL`` for more explicit usage.
1345

1446
.. autoclass:: hyperlink.URL
1547
.. automethod:: hyperlink.URL.from_text

docs/index.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ library. The easiest way to install is with pip::
3939

4040
Then, URLs are just an import away::
4141

42-
from hyperlink import URL
42+
import hyperlink
4343

44-
url = URL.from_text(u'http://github.com/python-hyper/hyperlink?utm_source=readthedocs')
44+
url = hyperlink.parse(u'http://github.com/python-hyper/hyperlink?utm_source=readthedocs')
4545

4646
better_url = url.replace(scheme=u'https', port=443)
4747
org_url = better_url.click(u'.')
4848

4949
print(org_url.to_text())
5050
# prints: https://github.com/python-hyper/
5151

52-
print(better_url.get(u'utm_source'))
52+
print(better_url.get(u'utm_source')[0])
5353
# prints: readthedocs
5454

5555
See :ref:`the API docs <hyperlink_api>` for more usage examples.

src/hyperlink/_url.py

+33-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
44
Usage is straightforward::
55
6-
>>> from hyperlink import URL
7-
>>> url = URL.from_text(u'http://github.com/mahmoud/hyperlink?utm_source=docs')
6+
>>> import hyperlink
7+
>>> url = hyperlink.parse(u'http://github.com/mahmoud/hyperlink?utm_source=docs')
88
>>> url.host
99
u'github.com'
1010
>>> secure_url = url.replace(scheme=u'https')
1111
>>> secure_url.get('utm_source')[0]
1212
u'docs'
1313
14-
As seen here, the API revolves around the lightweight and immutable
15-
:class:`URL` type, documented below.
14+
Hyperlink's API centers on the :class:`DecodedURL` type, which wraps
15+
the lower-level :class:`URL`, both of which can be returned by the
16+
:func:`parse()` convenience function.
17+
1618
""" # noqa: E501
1719

1820
import re
@@ -1743,13 +1745,23 @@ def remove(
17431745

17441746
EncodedURL = URL # An alias better describing what the URL really is
17451747

1748+
_EMPTY_URL = URL()
17461749

17471750
class DecodedURL(object):
1748-
"""DecodedURL is a type meant to act as a higher-level interface to
1749-
the URL. It is the `unicode` to URL's `bytes`. `DecodedURL` has
1750-
almost exactly the same API as `URL`, but everything going in and
1751-
out is in its maximally decoded state. All percent decoding is
1752-
handled automatically.
1751+
""":class:`DecodedURL` is a type designed to act as a higher-level
1752+
interface to :class:`URL` and the recommended type for most
1753+
operations. By analogy, :class:`DecodedURL` is the
1754+
:class:`unicode` to URL's :class:`bytes`.
1755+
1756+
:class:`DecodedURL` automatically handles encoding and decoding
1757+
all its components, such that all inputs and outputs are in a
1758+
maximally-decoded state. Note that this means, for some special
1759+
cases, a URL may not "roundtrip" character-for-character, but this
1760+
is considered a good tradeoff for the safety of automatic
1761+
encoding.
1762+
1763+
Otherwise, :class:`DecodedURL` has almost exactly the same API as
1764+
:class:`URL`.
17531765
17541766
Where applicable, a UTF-8 encoding is presumed. Be advised that
17551767
some interactions can raise :exc:`UnicodeEncodeErrors` and
@@ -1763,8 +1775,18 @@ class DecodedURL(object):
17631775
lazy (bool): Set to True to avoid pre-decode all parts of the URL to
17641776
check for validity. Defaults to False.
17651777
1778+
.. note::
1779+
1780+
The :class:`DecodedURL` initializer takes a :class:`URL` object,
1781+
not URL components, like :class:`URL`. To programmatically
1782+
construct a :class:`DecodedURL`, you can use this pattern:
1783+
1784+
>>> DecodedURL().replace(host='pypi.org', path=('projects', 'hyperlink').to_text()
1785+
"http://pypi.org/projects/hyperlink"
1786+
1787+
17661788
"""
1767-
def __init__(self, url, lazy=False):
1789+
def __init__(self, url=_EMPTY_URL, lazy=False):
17681790
# type: (URL, bool) -> None
17691791
self._url = url
17701792
if not lazy:
@@ -2098,7 +2120,7 @@ def parse(url, decoded=True, lazy=False):
20982120
decoded (bool): Whether or not to return a :class:`DecodedURL`,
20992121
which automatically handles all
21002122
encoding/decoding/quoting/unquoting for all the various
2101-
accessors of parts of the URL, or an :class:`EncodedURL`,
2123+
accessors of parts of the URL, or a :class:`URL`,
21022124
which has the same API, but requires handling of special
21032125
characters for different parts of the URL.
21042126

0 commit comments

Comments
 (0)