Skip to content

Commit a762f63

Browse files
committed
dropped dependency on requests
1 parent 53495da commit a762f63

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.
77

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

1112

1213
## [1.41.0] - 2025-02-15

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ install_requires =
4444
# pybtex requires setuptools, but doesn't seem to declare this.
4545
setuptools
4646
pybtex
47-
requests
4847
newick
4948
commonnexus>=1.2.0
5049
python-frontmatter

src/pycldf/cli_util.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Functionality to use in commandline tools which need to access CLDF datasets.
33
"""
44
import argparse
5+
import urllib.request
56

67
from clldutils.clilib import PathType, ParserError
78
from csvw.utils import is_url
8-
import requests
99

1010
from pycldf import Dataset, Database
1111
from pycldf.ext import discovery
@@ -45,11 +45,23 @@ def __call__(self, string):
4545
return super().__call__(string)
4646

4747

48+
49+
def head(url): # pragma: no cover
50+
class NoRedirection(urllib.request.HTTPErrorProcessor):
51+
def http_response(self, request, response):
52+
return response
53+
54+
https_response = http_response
55+
56+
opener = urllib.request.build_opener(NoRedirection)
57+
return opener.open(urllib.request.Request(url, method="HEAD")).status
58+
59+
4860
class UrlOrPathType(PathType):
4961
def __call__(self, string):
5062
if is_url(string):
5163
if self._must_exist:
52-
sc = requests.head(string).status_code
64+
sc = head(string)
5365
# We accept not only HTTP 200 as valid but also common redirection codes because
5466
# these are used e.g. for DOIs.
5567
if sc not in {200, 301, 302}:

src/pycldf/dataset.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,8 @@ def validate(
10261026
:raises ValueError: if a validation error is encountered (and `log` is `None`).
10271027
:return: Flag signaling whether schema and data are valid.
10281028
"""
1029+
# We must import components with custom validation to make sure they can be detected as
1030+
# subclasses of ComponentWithValidation.
10291031
from pycldf.media import MediaTable
10301032
from pycldf.trees import TreeTable
10311033

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

13121314

13131315
class ComponentWithValidation:
1316+
"""
1317+
A virtual base class for custom, component-centered validation.
1318+
"""
13141319
def __init__(self, ds: Dataset):
13151320
self.ds = ds
13161321
self.component = self.__class__.__name__
@@ -1347,11 +1352,11 @@ def sniff(p: pathlib.Path) -> bool:
13471352
return d.get('dc:conformsTo', '').startswith(TERMS_URL)
13481353

13491354

1350-
def iter_datasets(d: pathlib.Path) -> typing.Iterator[Dataset]:
1355+
def iter_datasets(d: pathlib.Path) -> typing.Generator[Dataset, None, None]:
13511356
"""
13521357
Discover CLDF datasets - by identifying metadata files - in a directory.
13531358
1354-
:param d: directory
1359+
:param d: directory in which to look for CLDF datasets (recursively).
13551360
:return: generator of `Dataset` instances.
13561361
"""
13571362
for p in walk(d, mode='files'):

tests/test_cli_util.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77

88
def test_UrlOrPathType(mocker):
9-
mocker.patch(
10-
'pycldf.cli_util.requests',
11-
mocker.Mock(head=mocker.Mock(return_value=mocker.Mock(status_code=200))))
9+
mocker.patch('pycldf.cli_util.head', mocker.Mock(return_value=200))
1210
url = 'http://example.com'
1311
assert UrlOrPathType()(url) == url
1412

15-
mocker.patch(
16-
'pycldf.cli_util.requests',
17-
mocker.Mock(head=mocker.Mock(return_value=mocker.Mock(status_code=404))))
13+
mocker.patch('pycldf.cli_util.head', mocker.Mock(return_value=404))
1814
with pytest.raises(argparse.ArgumentTypeError):
1915
_ = UrlOrPathType()(url)

0 commit comments

Comments
 (0)