Skip to content

Commit

Permalink
Add raw_path_qs attribute (#137)
Browse files Browse the repository at this point in the history
* Add raw_path_qs attribute

* Update __init__.py

* Update api.rst
  • Loading branch information
guyskk authored and asvetlov committed Nov 23, 2017
1 parent 42c3b25 commit 7782e45
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ celerybeat-schedule
.env

# virtualenv
.venv/
venv/
ENV/

Expand Down
11 changes: 11 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ There are two kinds of properties: *decoded* and *encoded* (with
'/path/to?a1=a&a2=b'


.. attribute:: URL.raw_path_qs

Encoded *path* part of URL and query string, ``'/'`` for absolute URLs without *path* part.

.. doctest::

>>> URL('http://example.com/путь/сюда?ключ=знач').raw_path_qs
'/%D0%BF%D1%83%D1%82%D1%8C/%D1%81%D1%8E%D0%B4%D0%B0?%D0%BA%D0%BB%D1%8E%D1%87=%D0%B7%D0%BD%D0%B0%D1%87'

.. versionadded:: 0.15

.. attribute:: URL.raw_path

Encoded *path* part of URL, ``'/'`` for absolute URLs without *path* part.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ def test_path_qs():
assert url.path_qs == '/path?б=в&ю=к'


def test_raw_path_qs():
url = URL('http://example.com/')
assert url.raw_path_qs == '/'
url = URL('http://example.com/?б=в&ю=к')
assert url.raw_path_qs == '/?%D0%B1=%D0%B2&%D1%8E=%D0%BA'
url = URL('http://example.com/path?б=в&ю=к')
assert url.raw_path_qs == '/path?%D0%B1=%D0%B2&%D1%8E=%D0%BA'
url = URL('http://example.com/путь?a=1&b=2')
assert url.raw_path_qs == '/%D0%BF%D1%83%D1%82%D1%8C?a=1&b=2'


def test_query_string_spaces():
url = URL('http://example.com?a+b=c+d&e=f+g')
assert url.query_string == 'a b=c d&e=f g'
Expand Down
10 changes: 8 additions & 2 deletions yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,18 @@ def query_string(self):

@cached_property
def path_qs(self):
"""Decoded path of URL with query
"""
"""Decoded path of URL with query."""
if not self.query_string:
return self.path
return '{}?{}'.format(self.path, self.query_string)

@cached_property
def raw_path_qs(self):
"""Encoded path of URL with query."""
if not self.raw_query_string:
return self.raw_path
return '{}?{}'.format(self.raw_path, self.raw_query_string)

@property
def raw_fragment(self):
"""Encoded fragment part of URL.
Expand Down
1 change: 1 addition & 0 deletions yarl/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class URL:
raw_query_string = ... # type: str
query_string = ... # type: str
path_qs = ... # type: str
raw_path_qs = ... # type: str
raw_fragment = ... # type: str
fragment = ... # type: str
query = ... # type: multidict.MultiDict
Expand Down

0 comments on commit 7782e45

Please sign in to comment.