Skip to content

Commit

Permalink
feat: support trailing slash
Browse files Browse the repository at this point in the history
Closes #8

BREAKING CHANGE: Removed public `path_list` attribute, should now be accessed through `path`.
BREAKING CHANGE: Trailing slash no longer removed.
  • Loading branch information
MicaelJarniac committed Apr 2, 2021
1 parent 4762d62 commit d34ac1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
19 changes: 13 additions & 6 deletions buildurl/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def __init__(self, base: str = ""):
# part of `path`, and not isolated
self.scheme: str = purl.scheme
self.netloc: str = purl.netloc
self.path_list: PathList = list()
self._path_list: PathList = list()
self.query_dict: QueryDict = dict()
self.fragment: str = purl.fragment

self.trailing_slash: bool = False

path_str: str = purl.path
if path_str:
self.path = path_str
Expand Down Expand Up @@ -74,7 +76,7 @@ def add_path(self, path: Path) -> None:
https://example.com/test/more/paths
>>> url.add_path("/again/and/again/")
>>> print(url.get)
https://example.com/test/more/paths/again/and/again
https://example.com/test/more/paths/again/and/again/
"""

path_list = list()
Expand All @@ -85,9 +87,11 @@ def add_path(self, path: Path) -> None:
else:
raise AttributeError

if len(path_list):
self.trailing_slash = path_list[-1] == ""
path_list = [p for p in path_list if p] # Remove empty strings

self.path_list.extend(path_list)
self._path_list.extend(path_list)

def add_query(self, query: Query) -> None:
"""Add a query argument.
Expand Down Expand Up @@ -122,12 +126,15 @@ def add_query(self, query: Query) -> None:
@property
def path(self) -> str:
"""Path string."""
return "/".join(self.path_list)
path = "/".join(self._path_list)
if self.trailing_slash:
path += "/"
return path

@path.setter
def path(self, path: Optional[Path]):
"""Replace current path."""
self.path_list = list()
self._path_list = list()
if path is not None:
self.add_path(path)

Expand Down Expand Up @@ -181,7 +188,7 @@ def __itruediv__(self, path: Path) -> "BuildURL":
https://example.com/test/more/paths
>>> url /= "/again/and/again/"
>>> print(url.get)
https://example.com/test/more/paths/again/and/again
https://example.com/test/more/paths/again/and/again/
"""

self.add_path(path)
Expand Down
16 changes: 14 additions & 2 deletions tests/test_buildurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ def test_path():
url.path = ["still", "testing"]
assert url.get == "https://example.com/still/testing"
url.path = "/once/more/"
assert url.get == "https://example.com/once/more"
url.path_list = ["again", "and", "again"]
assert url.get == "https://example.com/once/more/"
url.path = ["again", "and", "again"]
assert url.get == "https://example.com/again/and/again"
url.path = None
assert url.get == "https://example.com"

url = BuildURL("https://example.com/")
assert url.get == "https://example.com/"
url /= "path"
assert url.get == "https://example.com/path"
url /= "another//path"
assert url.get == "https://example.com/path/another/path"
url /= "/again/"
assert url.get == "https://example.com/path/another/path/again/"

url = BuildURL("https://example.com/folder/")
assert url.get == "https://example.com/folder/"


def test_query():
url = BuildURL("https://example.com")
Expand Down

0 comments on commit d34ac1d

Please sign in to comment.