Skip to content

Commit

Permalink
Model: Improve implementation of AddressPair.navigate()
Browse files Browse the repository at this point in the history
- Do not use the fundamental `.navigate()` method, as it needs too many
  workarounds.
- Do not store and copy query parameters, because the implementation
  does not use `.navigate()` any longer.
- Manipulate the `.path` property directly instead, computing it using
  the canonical `urljoin` function.
- Adjustments about missing trailing slashes still need to take place.
  • Loading branch information
amotl committed Sep 13, 2024
1 parent 8f2176e commit a25127e
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions cratedb_toolkit/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses
import typing as t
from copy import deepcopy
from pathlib import Path
from urllib.parse import urljoin

from attr import Factory
from attrs import define
Expand Down Expand Up @@ -141,26 +141,21 @@ class AddressPair:
__SERVER_SCHEMES__ = ["http", "https", "mongodb", "mongodb+srv"]

def navigate(self, source_path: str, target_path: str) -> "AddressPair":
source_url_query_parameters = self.source_url.query_params
target_url_query_parameters = self.target_url.query_params

source_url = deepcopy(self.source_url)
target_url = deepcopy(self.target_url)

# Q: What the hack?
# A: It makes subsequent `.navigate()` operations work.
if (
source_url.scheme in self.__SERVER_SCHEMES__
and Path(source_url.path).is_absolute()
and source_url.path[-1] != "/"
):
# A: Adjustments about missing trailing slashes, business as usual.
# It makes subsequent `.navigate()` operations work.
# Remark: It is not applicable for filesystem paths including wildcards,
# like `./datasets/*.ndjson`. In this case, `.navigate()` should
# strip the `*.ndjson` part, and replace it by the designated label.
if source_url.scheme in self.__SERVER_SCHEMES__ and source_url.path[-1] != "/":
source_url.path += "/"
if target_url.path[-1] != "/":
target_url.path += "/"

source_url = source_url.navigate(f"./{source_path}")
source_url.query_params = source_url_query_parameters
target_url = target_url.navigate(f"./{target_path}")
target_url.query_params = target_url_query_parameters
source_url.path = urljoin(source_url.path, source_path)
target_url.path = urljoin(target_url.path, target_path)

return AddressPair(source_url, target_url)

0 comments on commit a25127e

Please sign in to comment.