Skip to content

Commit 3aa9ddc

Browse files
committed
feat: ruff linter & formatter
1 parent beee375 commit 3aa9ddc

File tree

13 files changed

+763
-370
lines changed

13 files changed

+763
-370
lines changed

.github/workflows/python-CI.yml

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,17 @@ jobs:
1818
runs-on: ubuntu-latest
1919

2020
steps:
21-
- uses: actions/checkout@v3
22-
- name: Set up Python 3.10
23-
uses: actions/setup-python@v3
21+
- uses: actions/checkout@v4
22+
- name: Set up Python 3.11
23+
uses: actions/setup-python@v5
2424
with:
25-
python-version: "3.10"
25+
python-version: "3.11"
2626
- uses: Gr1N/setup-poetry@v8 #install poetry
27-
- name: Install parts of toolchain
28-
run: |
29-
python -m pip install --upgrade pip
30-
pip install flake8 pytest
27+
- name: Upgrade pip
28+
run: python -m pip install --upgrade pip
3129
- name: Install requirements with poetry
3230
run: poetry install
33-
- name: Lint with flake8
34-
run: |
35-
# stop the build if there are Python syntax errors or undefined names
36-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
37-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
38-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
31+
- name: Lint with Ruff
32+
run: poetry run ruff check --output-format=github .
3933
- name: Test with pytest
40-
run: |
41-
poetry run pytest
34+
run: poetry run pytest

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ 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)
24+
- [Linting](#linting)
2325

2426

2527
## Quickstart
@@ -43,6 +45,7 @@ You can then use the client in the command line:
4345
```bash
4446
databusclient --help
4547
databusclient deploy --help
48+
databusclient delete --help
4649
databusclient download --help
4750
```
4851

@@ -553,3 +556,27 @@ from databusclient import deploy
553556
# API key can be found (or generated) at https://$$DATABUS_BASE$$/$$USER$$#settings
554557
deploy(dataset, "mysterious API key")
555558
```
559+
560+
## Development
561+
562+
Install development dependencies yourself or via [Poetry](https://python-poetry.org/):
563+
564+
```bash
565+
poetry install --with dev
566+
```
567+
568+
### Linting
569+
570+
The used linter is [Ruff](https://ruff.rs/). Ruff is configured in `pyproject.toml` and is enforced in CI (`.github/workflows/ruff.yml`).
571+
572+
For development, you can run linting locally with `ruff check . ` and optionally auto-format with `ruff format .`.
573+
574+
To ensuere compatibility with the `pyproject.toml` configured dependencies, run Ruff via Poetry:
575+
576+
```bash
577+
# To check for linting issues:
578+
poetry run ruff check .
579+
580+
# To auto-format code:
581+
poetry run ruff format .
582+
```

databusclient/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from databusclient import cli
2-
from databusclient.api.deploy import create_dataset, deploy, create_distribution
2+
from databusclient.api.deploy import create_dataset, create_distribution, deploy
33

44
__all__ = ["create_dataset", "deploy", "create_distribution"]
55

6+
67
def run():
78
cli.app()

databusclient/api/delete.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
2-
import requests
32
from typing import List
43

5-
from databusclient.api.utils import get_databus_id_parts_from_uri, fetch_databus_jsonld
4+
import requests
5+
6+
from databusclient.api.utils import fetch_databus_jsonld, get_databus_id_parts_from_uri
7+
68

79
def _confirm_delete(databusURI: str) -> str:
810
"""
@@ -17,9 +19,17 @@ def _confirm_delete(databusURI: str) -> str:
1719
- "cancel" if the user chooses to cancel the entire deletion process
1820
"""
1921
print(f"Are you sure you want to delete: {databusURI}?")
20-
print("\nThis action is irreversible and will permanently remove the resource and all its data.")
22+
print(
23+
"\nThis action is irreversible and will permanently remove the resource and all its data."
24+
)
2125
while True:
22-
choice = input("Type 'yes'/'y' to confirm, 'skip'/'s' to skip this resource, or 'cancel'/'c' to abort: ").strip().lower()
26+
choice = (
27+
input(
28+
"Type 'yes'/'y' to confirm, 'skip'/'s' to skip this resource, or 'cancel'/'c' to abort: "
29+
)
30+
.strip()
31+
.lower()
32+
)
2333
if choice in ("yes", "y"):
2434
return "confirm"
2535
elif choice in ("skip", "s"):
@@ -30,7 +40,9 @@ def _confirm_delete(databusURI: str) -> str:
3040
print("Invalid input. Please type 'yes'/'y', 'skip'/'s', or 'cancel'/'c'.")
3141

3242

33-
def _delete_resource(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False):
43+
def _delete_resource(
44+
databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False
45+
):
3446
"""
3547
Delete a single Databus resource (version, artifact, group).
3648
@@ -56,10 +68,7 @@ def _delete_resource(databusURI: str, databus_key: str, dry_run: bool = False, f
5668
if databus_key is None:
5769
raise ValueError("Databus API key must be provided for deletion")
5870

59-
headers = {
60-
"accept": "*/*",
61-
"X-API-KEY": databus_key
62-
}
71+
headers = {"accept": "*/*", "X-API-KEY": databus_key}
6372

6473
if dry_run:
6574
print(f"[DRY RUN] Would delete: {databusURI}")
@@ -70,10 +79,14 @@ def _delete_resource(databusURI: str, databus_key: str, dry_run: bool = False, f
7079
if response.status_code in (200, 204):
7180
print(f"Successfully deleted: {databusURI}")
7281
else:
73-
raise Exception(f"Failed to delete {databusURI}: {response.status_code} - {response.text}")
82+
raise Exception(
83+
f"Failed to delete {databusURI}: {response.status_code} - {response.text}"
84+
)
7485

7586

76-
def _delete_list(databusURIs: List[str], databus_key: str, dry_run: bool = False, force: bool = False):
87+
def _delete_list(
88+
databusURIs: List[str], databus_key: str, dry_run: bool = False, force: bool = False
89+
):
7790
"""
7891
Delete a list of Databus resources.
7992
@@ -85,7 +98,9 @@ def _delete_list(databusURIs: List[str], databus_key: str, dry_run: bool = False
8598
_delete_resource(databusURI, databus_key, dry_run=dry_run, force=force)
8699

87100

88-
def _delete_artifact(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False):
101+
def _delete_artifact(
102+
databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False
103+
):
89104
"""
90105
Delete an artifact and all its versions.
91106
@@ -121,7 +136,10 @@ def _delete_artifact(databusURI: str, databus_key: str, dry_run: bool = False, f
121136
# Finally, delete the artifact itself
122137
_delete_resource(databusURI, databus_key, dry_run=dry_run, force=force)
123138

124-
def _delete_group(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False):
139+
140+
def _delete_group(
141+
databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False
142+
):
125143
"""
126144
Delete a group and all its artifacts and versions.
127145
@@ -154,13 +172,14 @@ def _delete_group(databusURI: str, databus_key: str, dry_run: bool = False, forc
154172
# Finally, delete the group itself
155173
_delete_resource(databusURI, databus_key, dry_run=dry_run, force=force)
156174

175+
157176
def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool):
158177
"""
159178
Delete a dataset from the databus.
160179
161180
Delete a group, artifact, or version identified by the given databus URI.
162181
Will recursively delete all data associated with the dataset.
163-
182+
164183
Parameters:
165184
- databusURIs: List of full databus URIs of the resources to delete
166185
- databus_key: Databus API key to authenticate the deletion requests
@@ -169,7 +188,9 @@ def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool)
169188
"""
170189

171190
for databusURI in databusURIs:
172-
_host, _account, group, artifact, version, file = get_databus_id_parts_from_uri(databusURI)
191+
_host, _account, group, artifact, version, file = get_databus_id_parts_from_uri(
192+
databusURI
193+
)
173194

174195
if group == "collections" and artifact is not None:
175196
print(f"Deleting collection: {databusURI}")

databusclient/api/deploy.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from enum import Enum
2-
from typing import List, Dict, Tuple, Optional, Union
3-
import requests
41
import hashlib
52
import json
3+
from enum import Enum
4+
from typing import Dict, List, Optional, Tuple, Union
5+
6+
import requests
67

78
__debug = False
89

@@ -153,7 +154,7 @@ def get_file_info(distribution_str: str) -> Tuple[Dict[str, str], str, str, str,
153154
cvs = _get_content_variants(distribution_str)
154155
extension_part, format_extension, compression = _get_extensions(distribution_str)
155156

156-
content_variant_part = "_".join([f"{key}={value}" for key, value in cvs.items()])
157+
# content_variant_part = "_".join([f"{key}={value}" for key, value in cvs.items()])
157158

158159
if __debug:
159160
print("DEBUG", distribution_str, extension_part)
@@ -200,7 +201,10 @@ def create_distribution(
200201

201202
return f"{url}|{meta_string}"
202203

203-
def _create_distributions_from_metadata(metadata: List[Dict[str, Union[str, int]]]) -> List[str]:
204+
205+
def _create_distributions_from_metadata(
206+
metadata: List[Dict[str, Union[str, int]]],
207+
) -> List[str]:
204208
"""
205209
Create distributions from metadata entries.
206210
@@ -233,24 +237,30 @@ def _create_distributions_from_metadata(metadata: List[Dict[str, Union[str, int]
233237
size = entry["size"]
234238
url = entry["url"]
235239
if not isinstance(size, int) or size <= 0:
236-
raise ValueError(f"Invalid size for {url}: expected positive integer, got {size}")
240+
raise ValueError(
241+
f"Invalid size for {url}: expected positive integer, got {size}"
242+
)
237243
# Validate SHA-256 hex digest (64 hex chars)
238-
if not isinstance(checksum, str) or len(checksum) != 64 or not all(
239-
c in '0123456789abcdefABCDEF' for c in checksum):
240-
raise ValueError(f"Invalid checksum for {url}")
244+
if (
245+
not isinstance(checksum, str)
246+
or len(checksum) != 64
247+
or not all(c in "0123456789abcdefABCDEF" for c in checksum)
248+
):
249+
raise ValueError(f"Invalid checksum for {url}")
241250

242251
distributions.append(
243252
create_distribution(
244253
url=url,
245254
cvs={"count": f"{counter}"},
246255
file_format=entry.get("file_format"),
247256
compression=entry.get("compression"),
248-
sha256_length_tuple=(checksum, size)
257+
sha256_length_tuple=(checksum, size),
249258
)
250259
)
251260
counter += 1
252261
return distributions
253262

263+
254264
def create_dataset(
255265
version_id: str,
256266
title: str,
@@ -361,7 +371,7 @@ def create_dataset(
361371
"@type": "Artifact",
362372
"title": title,
363373
"abstract": abstract,
364-
"description": description
374+
"description": description,
365375
}
366376
graphs.append(artifact_graph)
367377

@@ -445,7 +455,7 @@ def deploy_from_metadata(
445455
abstract: str,
446456
description: str,
447457
license_url: str,
448-
apikey: str
458+
apikey: str,
449459
) -> None:
450460
"""
451461
Deploy a dataset from metadata entries.
@@ -475,7 +485,7 @@ def deploy_from_metadata(
475485
abstract=abstract,
476486
description=description,
477487
license_url=license_url,
478-
distributions=distributions
488+
distributions=distributions,
479489
)
480490

481491
print(f"Deploying dataset version: {version_id}")

0 commit comments

Comments
 (0)