Skip to content

Commit 072f37c

Browse files
committed
feat: python 3.11 migration
1 parent 1c4a636 commit 072f37c

File tree

11 files changed

+171
-162
lines changed

11 files changed

+171
-162
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.10-slim
1+
FROM python:3.11-slim
22

33
WORKDIR /data
44

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ Command-line and Python client for downloading and deploying datasets on DBpedia
2020
- [Delete](#cli-delete)
2121
- [Module Usage](#module-usage)
2222
- [Deploy](#module-deploy)
23-
- [Contributing](#contributing)
23+
- [Development & Contributing](#development--contributing)
2424
- [Linting](#linting)
25+
- [Testing](#testing)
2526

2627

2728
## Quickstart
@@ -32,7 +33,7 @@ You can use either **Python** or **Docker**. Both methods support all client fea
3233

3334
### Python
3435

35-
Requirements: [Python](https://www.python.org/downloads/) and [pip](https://pip.pypa.io/en/stable/installation/)
36+
Requirements: [Python 3.11+](https://www.python.org/downloads/) and [pip](https://pip.pypa.io/en/stable/installation/)
3637

3738
Before using the client, install it via pip:
3839

@@ -186,8 +187,8 @@ Options:
186187
e.g. https://databus.dbpedia.org/sparql)
187188
--vault-token TEXT Path to Vault refresh token file
188189
--databus-key TEXT Databus API key to download from protected databus
189-
--latest-only When downloading artifacts, only download the latest
190-
version
190+
--all-versions When downloading artifacts, download all versions
191+
instead of only the latest
191192
--authurl TEXT Keycloak token endpoint URL [default:
192193
https://auth.dbpedia.org/realms/dbpedia/protocol/openid-
193194
connect/token]
@@ -557,7 +558,7 @@ from databusclient import deploy
557558
deploy(dataset, "mysterious API key")
558559
```
559560
560-
## Development
561+
## Development & Contributing
561562
562563
Install development dependencies yourself or via [Poetry](https://python-poetry.org/):
563564
@@ -569,14 +570,32 @@ poetry install --with dev
569570
570571
The used linter is [Ruff](https://ruff.rs/). Ruff is configured in `pyproject.toml` and is enforced in CI (`.github/workflows/ruff.yml`).
571572
572-
For development, you can run linting locally with `ruff check . ` and optionally auto-format with `ruff format .`.
573+
For development, you can run linting locally with `ruff check .` and optionally auto-format with `ruff format .`.
573574
574-
To ensuere compatibility with the `pyproject.toml` configured dependencies, run Ruff via Poetry:
575+
To ensure compatibility with the `pyproject.toml` configured dependencies, run Ruff via Poetry:
575576
576577
```bash
577578
# To check for linting issues:
578579
poetry run ruff check .
579580
580581
# To auto-format code:
581582
poetry run ruff format .
583+
```
584+
585+
### Testing
586+
587+
When developing new features please make sure to add appropriate tests and ensure that all tests pass. Tests are under `tests/` and use [pytest](https://docs.pytest.org/en/7.4.x/) as test framework.
588+
589+
When fixing bugs or refactoring existing code, please make sure to add tests that cover the affected functionality. The current test coverage is very low, so any additional tests are highly appreciated.
590+
591+
To run tests locally, use:
592+
593+
```bash
594+
pytest tests/
595+
```
596+
597+
Or to ensure compatibility with the `pyproject.toml` configured dependencies, run pytest via Poetry:
598+
599+
```bash
600+
poetry run pytest tests/
582601
```

databusclient/api/delete.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
import requests
55

6-
from databusclient.api.utils import fetch_databus_jsonld, get_databus_id_parts_from_uri
6+
from databusclient.api.utils import (
7+
fetch_databus_jsonld,
8+
get_databus_id_parts_from_file_url,
9+
)
710

811

912
def _confirm_delete(databusURI: str) -> str:
@@ -161,7 +164,7 @@ def _delete_group(
161164
uri = item.get("@id")
162165
if not uri:
163166
continue
164-
_, _, _, _, version, _ = get_databus_id_parts_from_uri(uri)
167+
_, _, _, _, version, _ = get_databus_id_parts_from_file_url(uri)
165168
if version is None:
166169
artifact_uris.append(uri)
167170

@@ -188,8 +191,8 @@ def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool)
188191
"""
189192

190193
for databusURI in databusURIs:
191-
_host, _account, group, artifact, version, file = get_databus_id_parts_from_uri(
192-
databusURI
194+
_host, _account, group, artifact, version, file = (
195+
get_databus_id_parts_from_file_url(databusURI)
193196
)
194197

195198
if group == "collections" and artifact is not None:

databusclient/api/deploy.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import requests
77

8-
__debug = False
8+
_debug = False
99

1010

1111
class DeployError(Exception):
@@ -36,6 +36,11 @@ def _get_content_variants(distribution_str: str) -> Optional[Dict[str, str]]:
3636

3737
cvs = {}
3838
for kv in cv_str.split("_"):
39+
if "=" not in kv:
40+
raise BadArgumentException(
41+
f"Invalid content variant format: '{kv}'. Expected 'key=value' format."
42+
)
43+
3944
key, value = kv.split("=")
4045
cvs[key] = value
4146

@@ -141,8 +146,8 @@ def _get_file_stats(distribution_str: str) -> Tuple[Optional[str], Optional[int]
141146

142147

143148
def _load_file_stats(url: str) -> Tuple[str, int]:
144-
resp = requests.get(url)
145-
if resp.status_code > 400:
149+
resp = requests.get(url, timeout=30)
150+
if resp.status_code >= 400:
146151
raise requests.exceptions.RequestException(response=resp)
147152

148153
sha256sum = hashlib.sha256(bytes(resp.content)).hexdigest()
@@ -156,7 +161,7 @@ def get_file_info(distribution_str: str) -> Tuple[Dict[str, str], str, str, str,
156161

157162
# content_variant_part = "_".join([f"{key}={value}" for key, value in cvs.items()])
158163

159-
if __debug:
164+
if _debug:
160165
print("DEBUG", distribution_str, extension_part)
161166

162167
sha256sum, content_length = _get_file_stats(distribution_str)
@@ -306,7 +311,13 @@ def create_dataset(
306311
"""
307312

308313
_versionId = str(version_id).strip("/")
309-
_, account_name, group_name, artifact_name, version = _versionId.rsplit("/", 4)
314+
parts = _versionId.rsplit("/", 4)
315+
if len(parts) < 5:
316+
raise BadArgumentException(
317+
f"Invalid version_id format: '{version_id}'. "
318+
f"Expected format: <BASE>/<ACCOUNT>/<GROUP>/<ARTIFACT>/<VERSION>"
319+
)
320+
_, _account_name, _group_name, _artifact_name, version = parts
310321

311322
# could be build from stuff above,
312323
# was not sure if there are edge cases BASE=http://databus.example.org/"base"/...
@@ -428,22 +439,30 @@ def deploy(
428439

429440
headers = {"X-API-KEY": f"{api_key}", "Content-Type": "application/json"}
430441
data = json.dumps(dataid)
431-
base = "/".join(dataid["@graph"][0]["@id"].split("/")[0:3])
442+
443+
try:
444+
base = "/".join(dataid["@graph"][0]["@id"].split("/")[0:3])
445+
except (KeyError, IndexError, TypeError) as e:
446+
raise DeployError(f"Invalid dataid structure: {e}")
447+
432448
api_uri = (
433449
base
434450
+ f"/api/publish?verify-parts={str(verify_parts).lower()}&log-level={log_level.name}"
435451
)
436-
resp = requests.post(api_uri, data=data, headers=headers)
452+
resp = requests.post(api_uri, data=data, headers=headers, timeout=30)
437453

438-
if debug or __debug:
439-
dataset_uri = dataid["@graph"][0]["@id"]
454+
if debug or _debug:
455+
try:
456+
dataset_uri = dataid["@graph"][0]["@id"]
457+
except (KeyError, IndexError, TypeError) as e:
458+
raise DeployError(f"Invalid dataid structure: {e}")
440459
print(f"Trying submitting data to {dataset_uri}:")
441460
print(data)
442461

443462
if resp.status_code != 200:
444463
raise DeployError(f"Could not deploy dataset to databus. Reason: '{resp.text}'")
445464

446-
if debug or __debug:
465+
if debug or _debug:
447466
print("---------")
448467
print(resp.text)
449468

0 commit comments

Comments
 (0)