Skip to content

Commit

Permalink
Replace most uses of urllib with yarl
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Mar 1, 2024
1 parent 2cafb50 commit 94eae70
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
11 changes: 6 additions & 5 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from time import sleep, time
from types import TracebackType
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from urllib.parse import quote_plus, urlparse, urlunparse

import click
from dandischema import models
from pydantic import BaseModel, Field, PrivateAttr
import requests
import tenacity
from yarl import URL

from . import get_logger
from .consts import (
Expand Down Expand Up @@ -1521,7 +1521,7 @@ def get_content_url(
else:
raise # reraise since we need to figure out how to handle such a case
if strip_query:
url = urlunparse(urlparse(url)._replace(query=""))
url = str(URL(url).with_query(None))
return url

def get_download_file_iter(
Expand Down Expand Up @@ -1970,9 +1970,10 @@ def get_download_file_iter(
Returns a function that when called (optionally with an offset into the
file to start downloading at) returns a generator of chunks of the file
"""
prefix = quote_plus(str(self))
url = self.client.get_url(
f"/zarr/{self.zarr_id}/files?prefix={prefix}&download=true"
url = str(
URL(self.client.get_url(f"/zarr/{self.zarr_id}/files/")).with_query(
{"prefix": str(self), "download": "true"}
)
)

def downloader(start_at: int = 0) -> Iterator[bytes]:
Expand Down
32 changes: 18 additions & 14 deletions dandi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import traceback
import types
from typing import IO, Any, List, Optional, Protocol, TypeVar, Union
from urllib.parse import parse_qs, urlparse, urlunparse

import dateutil.parser
from multidict import MultiDict # dependency of yarl
from pydantic import BaseModel, Field
import requests
import ruamel.yaml
from semantic_version import Version
from yarl import URL

from . import __version__, get_logger
from .consts import DandiInstance, known_instances, known_instances_rev
Expand Down Expand Up @@ -567,8 +568,9 @@ def get_instance(dandi_instance_id: str | DandiInstance) -> DandiInstance:
else:
instance = None
is_api = False
bits = urlparse(redirector_url)
redirector_url = urlunparse((bits[0], bits[1], "", "", "", ""))
redirector_url = str(
URL(redirector_url).with_path("").with_query(None).with_fragment(None)
)
else:
dandi_id = dandi_instance_id
instance = known_instances[dandi_id]
Expand Down Expand Up @@ -619,13 +621,13 @@ def _get_instance(
if dandi_id is None:
# Don't use pydantic.AnyHttpUrl, as that sets the `port` attribute even
# if it's not present in the string.
bits = urlparse(api_url)
if bits.hostname is not None:
dandi_id = bits.hostname
if bits.port is not None:
u = URL(api_url)
if u.host is not None:
dandi_id = u.host
if (port := u.explicit_port) is not None:
if ":" in dandi_id:
dandi_id = f"[{dandi_id}]"
dandi_id += f":{bits.port}"
dandi_id += f":{port}"
else:
dandi_id = api_url
return DandiInstance(
Expand Down Expand Up @@ -790,12 +792,14 @@ def is_page2_url(page1: str, page2: str) -> bool:
Tests whether the URL ``page2`` is the same as ``page1`` but with the
``page`` query parameter set to ``2``
"""
bits1 = urlparse(page1)
params1 = parse_qs(bits1.query)
params1["page"] = ["2"]
bits2 = urlparse(page2)
params2 = parse_qs(bits2.query)
return (bits1[:3], params1, bits1.fragment) == (bits2[:3], params2, bits2.fragment)
url1 = URL(page1)
params1 = MultiDict(url1.query)
params1["page"] = "2"
url1 = url1.with_query(None)
url2 = URL(page2)
params2 = url2.query
url2 = url2.with_query(None)
return (url1, sorted(params1.items())) == (url2, sorted(params2.items()))


def exclude_from_zarr(path: Path) -> bool:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ install_requires =
ruamel.yaml >=0.15, <1
semantic-version
tenacity
yarl ~= 1.9
zarr ~= 2.10
zarr_checksum ~= 0.4.0
zip_safe = False
Expand Down

0 comments on commit 94eae70

Please sign in to comment.