Skip to content

Commit

Permalink
dropped dependency on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Feb 19, 2025
1 parent 53495da commit a762f63
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.

- Make sure all local media files are copied with `Dataset.copy` as well.
- Allow passing a description to `Dataset.add_table` and `Dataset.add_component`.
- Drop dependency on requests.


## [1.41.0] - 2025-02-15
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ install_requires =
# pybtex requires setuptools, but doesn't seem to declare this.
setuptools
pybtex
requests
newick
commonnexus>=1.2.0
python-frontmatter
Expand Down
16 changes: 14 additions & 2 deletions src/pycldf/cli_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Functionality to use in commandline tools which need to access CLDF datasets.
"""
import argparse
import urllib.request

from clldutils.clilib import PathType, ParserError
from csvw.utils import is_url
import requests

from pycldf import Dataset, Database
from pycldf.ext import discovery
Expand Down Expand Up @@ -45,11 +45,23 @@ def __call__(self, string):
return super().__call__(string)



def head(url): # pragma: no cover
class NoRedirection(urllib.request.HTTPErrorProcessor):
def http_response(self, request, response):
return response

https_response = http_response

opener = urllib.request.build_opener(NoRedirection)
return opener.open(urllib.request.Request(url, method="HEAD")).status


class UrlOrPathType(PathType):
def __call__(self, string):
if is_url(string):
if self._must_exist:
sc = requests.head(string).status_code
sc = head(string)
# We accept not only HTTP 200 as valid but also common redirection codes because
# these are used e.g. for DOIs.
if sc not in {200, 301, 302}:
Expand Down
9 changes: 7 additions & 2 deletions src/pycldf/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,8 @@ def validate(
:raises ValueError: if a validation error is encountered (and `log` is `None`).
:return: Flag signaling whether schema and data are valid.
"""
# We must import components with custom validation to make sure they can be detected as
# subclasses of ComponentWithValidation.
from pycldf.media import MediaTable
from pycldf.trees import TreeTable

Expand Down Expand Up @@ -1311,6 +1313,9 @@ def sentences(self) -> typing.List[orm.Example]:


class ComponentWithValidation:
"""
A virtual base class for custom, component-centered validation.
"""
def __init__(self, ds: Dataset):
self.ds = ds
self.component = self.__class__.__name__
Expand Down Expand Up @@ -1347,11 +1352,11 @@ def sniff(p: pathlib.Path) -> bool:
return d.get('dc:conformsTo', '').startswith(TERMS_URL)


def iter_datasets(d: pathlib.Path) -> typing.Iterator[Dataset]:
def iter_datasets(d: pathlib.Path) -> typing.Generator[Dataset, None, None]:
"""
Discover CLDF datasets - by identifying metadata files - in a directory.
:param d: directory
:param d: directory in which to look for CLDF datasets (recursively).
:return: generator of `Dataset` instances.
"""
for p in walk(d, mode='files'):
Expand Down
8 changes: 2 additions & 6 deletions tests/test_cli_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@


def test_UrlOrPathType(mocker):
mocker.patch(
'pycldf.cli_util.requests',
mocker.Mock(head=mocker.Mock(return_value=mocker.Mock(status_code=200))))
mocker.patch('pycldf.cli_util.head', mocker.Mock(return_value=200))
url = 'http://example.com'
assert UrlOrPathType()(url) == url

mocker.patch(
'pycldf.cli_util.requests',
mocker.Mock(head=mocker.Mock(return_value=mocker.Mock(status_code=404))))
mocker.patch('pycldf.cli_util.head', mocker.Mock(return_value=404))
with pytest.raises(argparse.ArgumentTypeError):
_ = UrlOrPathType()(url)

0 comments on commit a762f63

Please sign in to comment.