Skip to content

Commit

Permalink
wip: refactore to service approach
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Jul 26, 2024
1 parent 408886b commit 95497b9
Show file tree
Hide file tree
Showing 27 changed files with 462 additions and 415 deletions.
4 changes: 0 additions & 4 deletions .ruff.toml

This file was deleted.

32 changes: 0 additions & 32 deletions .tx/config

This file was deleted.

11 changes: 0 additions & 11 deletions Pipfile

This file was deleted.

23 changes: 0 additions & 23 deletions babel.ini

This file was deleted.

6 changes: 6 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2022 Technische Universität Graz.
# Copyright (C) 2024 Graz University of Technology.
#
# invenio-pure is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -30,6 +31,11 @@
"sphinx.ext.viewcode",
]

# add nitpick ignore
nitpick_ignore = [
("py:class", "flask.app.Flask"),
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

Expand Down
6 changes: 2 additions & 4 deletions invenio_pure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Technische Universität Graz.
# Copyright (C) 2021-2024 Graz University of Technology.
#
# invenio-pure is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -9,15 +9,13 @@

from .errors import PureAPIError, PureRuntimeError
from .ext import InvenioPure
from .types import URL, PureConfigs, PureID, PureRecord
from .types import URL, PureID

__version__ = "0.1.2"

__all__ = (
"__version__",
"InvenioPure",
"PureConfigs",
"PureRecord",
"PureID",
"PureAPIError",
"PureRuntimeError",
Expand Down
45 changes: 0 additions & 45 deletions invenio_pure/api.py

This file was deleted.

44 changes: 17 additions & 27 deletions invenio_pure/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2023 Graz University of Technology.
# Copyright (C) 2021-2024 Graz University of Technology.
#
# invenio-pure is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -12,10 +12,10 @@
from click_params import URL
from flask import current_app
from flask.cli import with_appcontext
from invenio_access.utils import get_identity
from invenio_accounts import current_accounts

from .api import import_from_pure
from .click_options import JSON
from .types import PureConfigs
from .services import PureRESTService, build_service


@group()
Expand All @@ -25,36 +25,26 @@ def pure() -> None:

@pure.command("import")
@with_appcontext
@option("--endpoint", type=URL)
@option("--token", type=STRING)
@option("--pure_username", type=STRING)
@option("--pure_password", type=STRING)
@option("--user-email", type=STRING)
@option("--pure-record", type=JSON())
@option("--endpoint", type=URL, required=True)
@option("--token", type=STRING, required=True)
@option("--username", type=STRING, required=True)
@option("--password", type=STRING, required=True)
@option("--user-email", type=STRING, required=True)
@option("--pure-id", type=STRING, required=True)
@option("--no-color", is_flag=True, default=False)
@build_service
def import_records_from_pure(
endpoint: str,
token: str,
pure_username: str,
pure_password: str,
pure_service: PureRESTService,
user_email: str,
pure_record: dict,
pure_id: str,
*,
no_color: bool,
) -> None:
"""Import a record from given JSON file or JSON string."""
import_func = current_app.config["PURE_IMPORT_FUNC"]
recipients = current_app.config["PURE_ERROR_MAIL_RECIPIENTS"]
sender = current_app.config["PURE_ERROR_MAIL_SENDER"]
configs = PureConfigs(
endpoint,
token,
pure_username,
pure_password,
user_email,
recipients,
sender,
)
record = import_from_pure(import_func, pure_record, configs)
user = current_accounts.datastore.get_user_by_email(user_email)
identity = get_identity(user)
record = import_func(identity, pure_id, pure_service)

color = "green" if not no_color else "black"
secho(f"record.id: {record.id}", fg=color)
48 changes: 0 additions & 48 deletions invenio_pure/click_options.py

This file was deleted.

45 changes: 4 additions & 41 deletions invenio_pure/config.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2022 Technische Universität Graz.
# Copyright (C) 2021-2024 Graz University of Technology.
#
# invenio-pure 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 that adds pure."""
from collections.abc import Callable

from .types import (
URL,
EmailAddress,
FilePath,
PureConfigs,
PureID,
PurePassword,
PureRecord,
PureToken,
PureUsername,
)

PURE_CELERY_BEAT_SCHEDULE = {}
from .types import URL, EmailAddress, PurePassword, PureToken, PureUsername

PURE_CELERY_BEAT_SCHEDULE: dict[str, dict] = {}
"""The celery beat schedule is used to configure the import schedule.
Following is a example configuration. One option to add this to the
Expand All @@ -35,27 +25,6 @@
}
"""

PURE_IMPORT_FUNC: Callable[
[
PureRecord,
PureConfigs,
Callable[[PureID, URL, PureUsername, PurePassword], FilePath],
],
None,
] = None
"""This function is called to import the pure record into the repository.
It needs as an parameter the pure record as a json, the pure configuration
and a callable which is provided from this package to download the file
from pure."""

PURE_SIEVE_FUNC: Callable[[PureRecord], bool] = None
"""This function implements the import criteria.
The import criteria handles the conditions which have to be true
as that the record will be imported into the repository.
"""

PURE_PURE_ENDPOINT: URL = ""
"""This is the endpoint of the pure instance."""

Expand All @@ -70,9 +39,3 @@

PURE_USER_EMAIL: EmailAddress = ""
"""This is the user email of the pure user within the InvenioRDM instance."""

PURE_ERROR_MAIL_RECIPIENTS: list[EmailAddress] = []
"""The list of recipients to send emails if errors happen in the import process."""

PURE_ERROR_MAIL_SENDER: EmailAddress = ""
"""The sender of the error emails."""
16 changes: 14 additions & 2 deletions invenio_pure/ext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2023 Technische Universität Graz.
# Copyright (C) 2021-2024 Graz University of Technology.
#
# invenio-pure is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -9,15 +9,27 @@

from flask import Flask

from .services import PureRESTService, PureRESTServiceConfig


class InvenioPure:
"""invenio-pure extension."""

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

def init_app(self, app: Flask) -> None:
"""Flask application initialization."""
self.init_services(app)
app.extensions["invenio-pure"] = self

def init_services(self, app: Flask) -> None:
"""Initialize services."""
endpoint = app.config.get("PURE_ENDPOINT", "")
token = app.config.get("PURE_TOKEN", "")
username = app.config.get("PURE_USERNAME", "")
password = app.config.get("PURE_PASSWORD", "")
config = PureRESTServiceConfig(endpoint, token, username, password)
self.pure_rest_service = PureRESTService(config)
Loading

0 comments on commit 95497b9

Please sign in to comment.