diff --git a/buildurl/builder.py b/buildurl/builder.py index 982a9d3..2f9ea8a 100644 --- a/buildurl/builder.py +++ b/buildurl/builder.py @@ -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 @@ -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() @@ -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. @@ -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) @@ -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) diff --git a/tests/test_buildurl.py b/tests/test_buildurl.py index e1172ab..f453e6e 100644 --- a/tests/test_buildurl.py +++ b/tests/test_buildurl.py @@ -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")