Skip to content

Commit

Permalink
setup: introduce ruff
Browse files Browse the repository at this point in the history
* remove unused .tx. the translation is done without transifex

* remove unused files

* remove unused checks because ruff took over
  • Loading branch information
utnapischtim committed Jul 18, 2024
1 parent 7a4d875 commit 373cee3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 306 deletions.
33 changes: 0 additions & 33 deletions .tx/config

This file was deleted.

16 changes: 9 additions & 7 deletions invenio_config_tugraz/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,45 @@

"""invenio module that adds tugraz configs."""

from flask import Flask

from . import config
from .custom_fields import ip_network, single_ip


class InvenioConfigTugraz(object):
class InvenioConfigTugraz:
"""invenio-config-tugraz extension."""

def __init__(self, app=None):
def __init__(self, app: Flask = None) -> None:
"""Extension initialization."""
if app:
self.init_app(app)

def init_app(self, app):
def init_app(self, app: Flask) -> None:
"""Flask application initialization."""
self.init_config(app)
self.add_custom_fields(app)
app.extensions["invenio-config-tugraz"] = self

def init_config(self, app):
def init_config(self, app: Flask) -> None:
"""Initialize configuration."""
for k in dir(config):
if k.startswith("INVENIO_CONFIG_TUGRAZ_"):
app.config.setdefault(k, getattr(config, k))

def add_custom_fields(self, app):
def add_custom_fields(self, app: Flask) -> None:
"""Add custom fields."""
app.config.setdefault("RDM_CUSTOM_FIELDS", [])
app.config["RDM_CUSTOM_FIELDS"].append(ip_network)
app.config["RDM_CUSTOM_FIELDS"].append(single_ip)


def finalize_app(app):
def finalize_app(app: Flask) -> None:
"""Finalize app."""
rank_blueprint_higher(app)


def rank_blueprint_higher(app):
def rank_blueprint_higher(app: Flask) -> None:
"""Rank this module's blueprint higher than blueprint of security module.
Needed in order to overwrite email templates.
Expand Down
32 changes: 11 additions & 21 deletions invenio_config_tugraz/permissions/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
can_read = [IfRestricted("record", then_=can_view, else_=can_all)]

can_read_deleted = [
IfRecordDeleted(then_=[UserManager, SystemProcess()], else_=can_read)
IfRecordDeleted(then_=[UserManager, SystemProcess()], else_=can_read),
]
can_read_deleted_files = can_read_deleted
can_media_read_deleted_files = can_read_deleted_files
Expand All @@ -124,7 +124,7 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
ResourceAccessToken("read"),
]
can_get_content_files = [
IfFileIsLocal(then_=can_read_files, else_=[SystemProcess()])
IfFileIsLocal(then_=can_read_files, else_=[SystemProcess()]),
]
can_create = can_tugraz_authenticated

Expand All @@ -137,10 +137,10 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
can_update_draft = can_review
can_draft_create_files = can_review
can_draft_set_content_files = [
IfFileIsLocal(then_=can_review, else_=[SystemProcess()])
IfFileIsLocal(then_=can_review, else_=[SystemProcess()]),
]
can_draft_get_content_files = [
IfFileIsLocal(then_=can_draft_read_files, else_=[SystemProcess()])
IfFileIsLocal(then_=can_draft_read_files, else_=[SystemProcess()]),
]
can_draft_commit_files = [IfFileIsLocal(then_=can_review, else_=[SystemProcess()])]
can_draft_update_files = can_review
Expand All @@ -150,14 +150,14 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
"RDM_ALLOW_METADATA_ONLY_RECORDS",
then_=[IfNewRecord(then_=can_tugraz_authenticated, else_=can_review)],
else_=[],
)
),
]
can_manage_record_access = [
IfConfig(
"RDM_ALLOW_RESTRICTED_RECORDS",
then_=[IfNewRecord(then_=can_tugraz_authenticated, else_=can_review)],
else_=[],
)
),
]

#
Expand All @@ -179,7 +179,7 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
"RDM_ALLOW_EXTERNAL_DOI_VERSIONING",
then_=can_curate,
else_=[IfExternalDOIRecord(then_=[Disable()], else_=can_curate)],
)
),
]
can_publish = can_review
can_lift_embargo = can_manage
Expand All @@ -198,13 +198,13 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
can_draft_media_create_files = can_review
can_draft_media_read_files = can_review
can_draft_media_set_content_files = [
IfFileIsLocal(then_=can_review, else_=[SystemProcess()])
IfFileIsLocal(then_=can_review, else_=[SystemProcess()]),
]
can_draft_media_get_content_files = [
IfFileIsLocal(then_=can_preview, else_=[SystemProcess()])
IfFileIsLocal(then_=can_preview, else_=[SystemProcess()]),
]
can_draft_media_commit_files = [
IfFileIsLocal(then_=can_preview, else_=[SystemProcess()])
IfFileIsLocal(then_=can_preview, else_=[SystemProcess()]),
]
can_draft_media_delete_files = can_review
can_draft_media_update_files = can_review
Expand All @@ -217,7 +217,7 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
ResourceAccessToken("read"),
]
can_media_get_content_files = [
IfFileIsLocal(then_=can_read, else_=[SystemProcess()])
IfFileIsLocal(then_=can_read, else_=[SystemProcess()]),
]
can_media_create_files = [Disable()]
can_media_set_content_files = [Disable()]
Expand Down Expand Up @@ -251,13 +251,3 @@ class TUGrazRDMRecordPermissionPolicy(RecordPermissionPolicy):
# Used to hide at the moment the `parent.is_verified` field. It should be set to
# correct permissions based on which the field will be exposed only to moderators
can_moderate = [Disable()]


# Plans on where to use `RecordIp` generator:
# class TUGRAZPermissionPolicy(BasePermissionPolicy):

# # Read access to API given to everyone.
# can_search = [AnyUser(), RecordIp()]

# # Read access given to everyone if public record/files and owners always.
# can_read = [AnyUserIfPublic(), RecordOwners(), RecordIp()]
16 changes: 11 additions & 5 deletions invenio_config_tugraz/utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# invenio-config-tugraz is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""Utils file."""

import warnings

from flask_principal import Identity
from invenio_access import any_user
from invenio_access.utils import get_identity
from invenio_accounts import current_accounts


def get_identity_from_user_by_email(email: str = None) -> Identity:
def get_identity_from_user_by_email(email: str | None = None) -> Identity:
"""Get the user specified via email or ID."""
warnings.warn("deprecated", DeprecationWarning, stacklevel=2)

if email is None:
raise ValueError("the email has to be set to get a identity")
msg = "the email has to be set to get a identity"
raise ValueError(msg)

user = current_accounts.datastore.get_user(email)

if user is None:
raise LookupError(f"user with {email} not found")
msg = f"user with {email} not found"
raise LookupError(msg)

identity = get_identity(user)

Expand All @@ -32,7 +38,7 @@ def get_identity_from_user_by_email(email: str = None) -> Identity:
return identity


def tugraz_account_setup_extension(user, account_info): # noqa: W0613
def tugraz_account_setup_extension(user, account_info) -> None: # noqa: ANN001, ARG001
"""Add tugraz_authenticated role to user after SAML-login was acknowledged.
To use, have `acs_handler_factory` call invenio_saml's `default_account_setup` first,
Expand Down
19 changes: 10 additions & 9 deletions invenio_config_tugraz/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2022 Graz University of Technology.
# Copyright (C) 2020-2024 Graz University of Technology.
#
# invenio-config-tugraz is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""invenio module for TUGRAZ config."""

from flask import Blueprint, redirect, url_for
from flask import Blueprint, Flask, redirect, url_for
from invenio_i18n import get_locale
from werkzeug.wrappers import Response as BaseResponse


def ui_blueprint(app):
def ui_blueprint(app: Flask) -> Blueprint:
"""Blueprint for the routes and resources provided by invenio-config-tugraz."""
routes = app.config.get("CONFIG_TUGRAZ_ROUTES")

Expand All @@ -30,37 +31,37 @@ def ui_blueprint(app):
return blueprint


def guide():
def guide() -> BaseResponse:
"""TUGraz_Repository_Guide."""
locale = get_locale()
return redirect(
url_for(
"static",
filename=f"documents/TUGraz_Repository_Guide_02.1_{locale}.pdf",
_external=True,
)
),
)


def terms():
def terms() -> BaseResponse:
"""Terms_And_Conditions."""
locale = get_locale()
return redirect(
url_for(
"static",
filename=f"documents/TUGraz_Repository_Terms_And_Conditions_{locale}.pdf",
_external=True,
)
),
)


def gdpr():
def gdpr() -> BaseResponse:
"""General_Data_Protection_Rights."""
locale = get_locale()
return redirect(
url_for(
"static",
filename=f"documents/TUGraz_Repository_General_Data_Protection_Rights_{locale}.pdf",
_external=True,
)
),
)
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
[build-system]
requires = ["setuptools", "wheel", "babel>2.8"]
build-backend = "setuptools.build_meta"

[tool.ruff]
exclude = ["docs"]

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"ANN101", "ANN102",
"D203", "D211", "D212", "D213",
"E501",
"ERA001",
"FA102",
"FIX002",
"INP001",
"RUF005", "RUF012",
"S101",
"TD002", "TD003",
"UP009",
]
13 changes: 0 additions & 13 deletions requirements-devel.txt

This file was deleted.

13 changes: 2 additions & 11 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Copyright (C) 2019-2020 CERN.
# Copyright (C) 2019-2020 Northwestern University.
# Copyright (C) 2020 Graz University of Technology.
# Copyright (C) 2020-2024 Graz University of Technology.
#
# invenio-config-tugraz is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand All @@ -16,17 +16,8 @@ set -o errexit
# Quit on unbound symbols
set -o nounset

# Always bring down docker services

function cleanup() {
eval "$(docker-services-cli down --env)"
}
trap cleanup EXIT
ruff check .

python -m check_manifest
python -m sphinx.cmd.build -qnN docs docs/_build/html
eval "$(docker-services-cli up --search ${SEARCH:-elasticsearch} --env)"
python -m pytest
tests_exit_code=$?
python -m sphinx.cmd.build -qnN -b doctest docs docs/_build/doctest
exit "$tests_exit_code"
Loading

0 comments on commit 373cee3

Please sign in to comment.