Skip to content

Commit

Permalink
replace pylint, isort and black by ruff (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Apr 16, 2024
1 parent be9e5e9 commit 76a9460
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 191 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ jobs:
- name: Install Python dependencies
run: poetry install

- name: Linting with Pylint
run: poetry run pylint dsp_permissions_scripts tests
- name: Formatting with ruff
run: poetry run ruff format .

- name: Linting with ruff
run: poetry run ruff check .

- name: Linting with mypy
run: poetry run mypy .
Expand Down
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Configuration of https://pre-commit.com/

repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.7
hooks:
- id: ruff
# args: [
# --ignore=A002,
# --ignore=D101,
# --ignore=D102,
# --ignore=PLR0913,
# --ignore=PLR2004,
# --ignore=PLW0603,
# ]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
args: ['--maxkb=1000']
- id: check-merge-conflict
- id: no-commit-to-branch
args: [--branch, main]
- id: fix-byte-order-marker
- id: detect-private-key
- id: check-symlinks
- id: destroyed-symlinks
- id: mixed-line-ending

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-check-mock-methods
- id: python-no-eval
- id: python-no-log-warn
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
- id: text-unicode-replacement-char
4 changes: 2 additions & 2 deletions dsp_permissions_scripts/ap/ap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def serialize_aps_of_project(
aps_as_dict = {explanation_string: aps_as_dicts}
with open(filepath, mode="w", encoding="utf-8") as f:
f.write(json.dumps(aps_as_dict, ensure_ascii=False, indent=2))
logger.info(f"{len(project_aps)} APs have been written to file {str(filepath)}")
logger.info(f"{len(project_aps)} APs have been written to file {filepath}")


def deserialize_aps_of_project(
Expand All @@ -38,5 +38,5 @@ def deserialize_aps_of_project(
filepath = _get_file_path(shortcode, mode)
with open(filepath, mode="r", encoding="utf-8") as f:
aps_as_dict = json.load(f)
aps_as_dicts = list(aps_as_dict.values())[0]
aps_as_dicts = next(iter(aps_as_dict.values()))
return [Ap.model_validate(d) for d in aps_as_dicts]
4 changes: 2 additions & 2 deletions dsp_permissions_scripts/doap/doap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def serialize_doaps_of_project(
doaps_as_dict = {explanation_string: doaps_as_dicts}
with open(filepath, mode="w", encoding="utf-8") as f:
f.write(json.dumps(doaps_as_dict, ensure_ascii=False, indent=2))
logger.info(f"{len(project_doaps)} DOAPs have been written to file {str(filepath)}")
logger.info(f"{len(project_doaps)} DOAPs have been written to file {filepath}")


def deserialize_doaps_of_project(
Expand All @@ -42,5 +42,5 @@ def deserialize_doaps_of_project(
filepath = _get_file_path(shortcode, mode)
with open(filepath, mode="r", encoding="utf-8") as f:
doaps_as_dict = json.load(f)
doaps_as_dicts = list(doaps_as_dict.values())[0]
doaps_as_dicts = next(iter(doaps_as_dict.values()))
return [Doap.model_validate(d) for d in doaps_as_dicts]
4 changes: 2 additions & 2 deletions dsp_permissions_scripts/oap/oap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def serialize_resource_oaps(
return
folder = _get_project_data_path(shortcode, mode)
folder.mkdir(parents=True, exist_ok=True)
logger.info(f"Writing {len(resource_oaps)} OAPs into {str(folder)}")
logger.info(f"Writing {len(resource_oaps)} OAPs into {folder}")
for res_oap in resource_oaps:
filename = re.sub(r"http://rdfh\.ch/[^/]+/", "resource_", res_oap.object_iri)
with open(folder / f"{filename}.json", mode="w", encoding="utf-8") as f:
f.write(res_oap.model_dump_json(indent=2))
logger.info(f"Successfully wrote {len(resource_oaps)} OAPs into {str(folder)}")
logger.info(f"Successfully wrote {len(resource_oaps)} OAPs into {folder}")


def deserialize_resource_oaps(
Expand Down
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _get_values_to_update(resource: dict[str, Any]) -> list[ValueUpdate]:
return res


def _update_permissions_for_value(
def _update_permissions_for_value( # noqa: PLR0913
resource_iri: str,
value: ValueUpdate,
resource_type: str,
Expand Down Expand Up @@ -65,7 +65,7 @@ def _update_permissions_for_value(
logger.info(f"Updated permissions of resource {resource_iri}, value {value.value_iri}")


def _update_permissions_for_resource(
def _update_permissions_for_resource( # noqa: PLR0913
resource_iri: str,
lmd: str | None,
resource_type: str,
Expand Down Expand Up @@ -98,7 +98,7 @@ def _update_permissions_for_resource_and_values(
"""Updates the permissions for the given resource and its values on a DSP server"""
try:
resource = get_resource(resource_iri, dsp_client)
except Exception as exc: # pylint: disable=broad-exception-caught
except Exception as exc: # noqa: BLE001 (blind exception)
logger.error(f"Cannot update resource {resource_iri}: {exc}")
return resource_iri, False
values = _get_values_to_update(resource)
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions dsp_permissions_scripts/utils/dsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import PACKAGE_NAME

# ruff: noqa: PLR2004 (magic value used in comparison)

logger = get_logger(__name__)

HTTP_OK = 200
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/utils/get_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def log_start_of_script(host: str, shortcode: str) -> None:
logger.info("")

print(f"\n{msg}")
logfile = [handler.baseFilename for handler in logger.handlers if isinstance(handler, logging.FileHandler)][0]
logfile = next(handler.baseFilename for handler in logger.handlers if isinstance(handler, logging.FileHandler))
print(f"There will be no print output, only logging to file {logfile}")
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create_string_from_scope(perm_scope: PermissionScope) -> str:
for perm_letter in perm_scope.model_fields:
if groups := perm_scope.get(perm_letter):
as_dict[perm_letter] = sort_groups(groups)
strs = [f"{k} {','.join([x.val for x in l])}" for k, l in as_dict.items()]
strs = [f"{k} {','.join([x.val for x in v])}" for k, v in as_dict.items()]
return "|".join(strs)


Expand Down
Loading

0 comments on commit 76a9460

Please sign in to comment.