Skip to content

Commit

Permalink
global: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
ntarocco committed Jul 28, 2023
1 parent c0e0e80 commit 874df5c
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 111 deletions.
3 changes: 1 addition & 2 deletions site/cds_rdm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""CDS-RDM."""

from .ext import CDS_RDM_UI, CDS_RDM_REST
from .ext import CDS_RDM_REST, CDS_RDM_UI

__version__ = "1.0.1"

Expand All @@ -17,4 +17,3 @@
"CDS_RDM_UI",
"CDS_RDM_REST",
)

14 changes: 8 additions & 6 deletions site/cds_rdm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
from invenio_pidstore.models import PersistentIdentifier
from invenio_rdm_records.proxies import current_rdm_records_service
from invenio_rdm_records.records.api import RDMDraft, RDMRecord
from invenio_rdm_records.records.models import (RDMDraftMetadata,
RDMFileDraftMetadata,
RDMFileRecordMetadata,
RDMParentCommunity,
RDMRecordMetadata,
RDMVersionsState)
from invenio_rdm_records.records.models import (
RDMDraftMetadata,
RDMFileDraftMetadata,
RDMFileRecordMetadata,
RDMParentCommunity,
RDMRecordMetadata,
RDMVersionsState,
)
from invenio_requests.proxies import current_requests_service
from invenio_requests.records.api import Request
from invenio_requests.records.models import RequestMetadata
Expand Down
11 changes: 7 additions & 4 deletions site/cds_rdm/ldap/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import uuid
from functools import partial

from cds_rdm.ldap.client import LdapClient
from cds_rdm.ldap.user_importer import LdapUserImporter
from cds_rdm.ldap.utils import InvenioUser, serialize_ldap_user, user_exists
from flask import current_app
from invenio_db import db
from invenio_oauthclient.models import RemoteAccount
from invenio_users_resources.services.users.tasks import reindex_users

from cds_rdm.ldap.client import LdapClient
from cds_rdm.ldap.user_importer import LdapUserImporter
from cds_rdm.ldap.utils import InvenioUser, serialize_ldap_user, user_exists


def get_ldap_users(log_func):
"""Create and return a map of all LDAP users."""
Expand Down Expand Up @@ -114,7 +115,9 @@ def update_invenio_users_from_ldap(remote_accounts, ldap_users_map, log_func):

def import_new_ldap_users(new_ldap_users, log_func):
"""Import any new LDAP user not in Invenio yet."""
remote_account_client_id = current_app.config["CERN_APP_CREDENTIALS"]["consumer_key"]
remote_account_client_id = current_app.config["CERN_APP_CREDENTIALS"][
"consumer_key"
]
importer = LdapUserImporter(remote_account_client_id)
added_count = 0
user_ids = []
Expand Down
3 changes: 2 additions & 1 deletion site/cds_rdm/ldap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

from functools import partial

from cds_rdm.ldap.errors import InvalidLdapUser
from invenio_accounts.models import User
from invenio_oauthclient.models import UserIdentity
from invenio_userprofiles import UserProfile

from cds_rdm.ldap.errors import InvalidLdapUser


def serialize_ldap_user(ldap_user_data, log_func=None):
"""Create ldap user."""
Expand Down
2 changes: 1 addition & 1 deletion site/cds_rdm/migration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-RDM migration module."""
"""CDS-RDM migration module."""
2 changes: 1 addition & 1 deletion site/cds_rdm/migration/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from pathlib import Path

import click
from invenio_rdm_migrator.streams import Runner

from cds_rdm.migration.streams import RecordStreamDefinition, UserStreamDefinition
from invenio_rdm_migrator.streams import Runner


@click.group()
Expand Down
18 changes: 9 additions & 9 deletions site/cds_rdm/migration/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

"""CDS-RDM migration extract module."""

import json
from os import listdir
from os.path import isfile, join
from pathlib import Path

import click
import json

from invenio_rdm_migrator.extract import Extract


Expand All @@ -23,13 +22,14 @@ def __init__(self, dirpath):
self.dirpath = Path(dirpath).absolute()

def run(self):

files = [f for f in listdir(self.dirpath)
if isfile(join(self.dirpath, f)) and not f.startswith('.')
]
files = [
f
for f in listdir(self.dirpath)
if isfile(join(self.dirpath, f)) and not f.startswith(".")
]

for file in files:
with open(join(self.dirpath, file), 'r') as dump_file:
with open(join(self.dirpath, file), "r") as dump_file:
data = json.load(dump_file)
with click.progressbar(data) as records:
for dump_record in records:
Expand All @@ -42,8 +42,8 @@ def __init__(self, filepath):
self.filepath = Path(filepath).absolute()

def run(self):
with open(self.filepath, 'r') as dump_file:
with open(self.filepath, "r") as dump_file:
data = json.load(dump_file)
with click.progressbar(data) as records:
for dump_record in records:
yield dump_record
yield dump_record
6 changes: 3 additions & 3 deletions site/cds_rdm/migration/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-RDM migration streams module."""
from cds_rdm.migration.transform.user_transform import CDSUserTransform
from invenio_rdm_migrator.streams import StreamDefinition
from cds_rdm.migration.extract import LegacyExtract, LegacyUserExtract
from invenio_rdm_migrator.streams.records.load import RDMRecordCopyLoad
from cds_rdm.migration.transform.transform import CDSToRDMRecordTransform
from invenio_rdm_migrator.streams.users import UserCopyLoad

from cds_rdm.migration.extract import LegacyExtract, LegacyUserExtract
from cds_rdm.migration.transform.transform import CDSToRDMRecordTransform
from cds_rdm.migration.transform.user_transform import CDSUserTransform

RecordStreamDefinition = StreamDefinition(
name="records",
Expand Down
8 changes: 3 additions & 5 deletions site/cds_rdm/migration/transform/models/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
"""CDS-RDM CMS note model."""

from __future__ import unicode_literals

from .overdo import CdsOverdo


class CMSNote(CdsOverdo):
"""Translation Index for CDS Books."""

__query__ = (
'980__:INTNOTECMSPUBL 980__:NOTE'
)
__query__ = "980__:INTNOTECMSPUBL 980__:NOTE"

__model_ignore_keys__ = {
}
__model_ignore_keys__ = {}

_default_fields = None

Expand Down
42 changes: 21 additions & 21 deletions site/cds_rdm/migration/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

"""CDS-RDM transform step module."""

import logging
import datetime
import logging

from invenio_rdm_migrator.streams.records.transform import (
RDMRecordEntry,
RDMRecordTransform,
)

from invenio_rdm_migrator.streams.records.transform import RDMRecordTransform, \
RDMRecordEntry
from cds_rdm.migration.transform.xml_processing.dumper import CDSRecordDump

cli_logger = logging.getLogger("migrator")
Expand Down Expand Up @@ -40,8 +43,10 @@ def _access(self, entry, record_dump):
for key, value in record_dump.files.items():
if value[0]["hidden"]:
is_file_public = False
return {"record": "public",
"files": "public" if is_file_public else "restricted"}
return {
"record": "public",
"files": "public" if is_file_public else "restricted",
}

def _index(self, record_dump):
"""Returns the version index of the record."""
Expand All @@ -63,19 +68,20 @@ def _communities(self, json_entry):
return json_entry["communities"]

def _metadata(self, json_entry):

def creators(json):
try:
return json_entry["creators"]
except KeyError:
return [
{"person_or_org":
{"identifiers": [],
"given_name": "unknown",
"name": "unknown",
"family_name": "unknown",
"type": "personal"}
}
{
"person_or_org": {
"identifiers": [],
"given_name": "unknown",
"name": "unknown",
"family_name": "unknown",
"type": "personal",
}
}
]

def _resource_type(data):
Expand All @@ -87,7 +93,7 @@ def _resource_type(data):
"creators": creators(json_entry),
"title": json_entry["title"],
"resource_type": _resource_type(json_entry),
"description": json_entry.get("description", "")
"description": json_entry.get("description", ""),
}

def transform(self, entry):
Expand Down Expand Up @@ -117,19 +123,13 @@ def transform(self, entry):


class CDSToRDMRecordTransform(RDMRecordTransform):

def _community_id(self, entry, record):
communities = record.get("communities")
if communities:
# TODO: handle all slugs
slug = communities[0]
if slug:
return {
"ids": [
slug
],
"default": slug
}
return {"ids": [slug], "default": slug}
return {}

def _parent(self, entry, record):
Expand Down
7 changes: 4 additions & 3 deletions site/cds_rdm/migration/transform/user_transform.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import datetime

from invenio_rdm_migrator.streams.users import UserTransform, UserEntry
from invenio_rdm_migrator.streams.users import UserEntry, UserTransform


class CDSUserTransform(UserTransform):

def _user(self, entry):
"""Transform the user."""
return CDSUserEntry().transform(entry)
Expand Down Expand Up @@ -70,7 +69,9 @@ def _active(self, entry):

def _confirmed_at(self, entry):
"""Returns the confirmation date."""
return entry.get("confirmed_at", datetime.utcnow().isoformat()) # TODO do we migrate inactive users ? the ones not present in ldap ?
return entry.get(
"confirmed_at", datetime.utcnow().isoformat()
) # TODO do we migrate inactive users ? the ones not present in ldap ?

def _username(self, entry):
"""Returns the username."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-RDM migration MARC xml processing module."""
"""CDS-RDM migration MARC xml processing module."""
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
# }
# }


def get_contributor_role(subfield, role, raise_unexpected=False):
"""Clean up roles."""
translations = {
Expand Down Expand Up @@ -133,7 +134,9 @@ def extract_json_contributor_ids(info):
for author_id in author_ids:
match = regex.match(author_id)
if match:
ids.append({"identifier": match.group(3), "scheme": SOURCES[match.group(1)]})
ids.append(
{"identifier": match.group(3), "scheme": SOURCES[match.group(1)]}
)
try:
ids.append({"identifier": info["inspireid"], "scheme": "INSPIRE"})
except KeyError:
Expand Down
2 changes: 0 additions & 2 deletions site/cds_rdm/migration/transform/xml_processing/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""CDS-RDM MARC XML dumper module."""

import arrow

from cds_dojson.marc21.utils import create_record

from cds_rdm.migration.transform import migrator_marc21
Expand All @@ -24,7 +23,6 @@ def __init__(
latest_only=True,
dojson_model=migrator_marc21,
):

"""Initialize."""
self.data = data
self.source_type = source_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""CDS-RDM migration data quality module."""
"""CDS-RDM migration data quality module."""
Loading

0 comments on commit 874df5c

Please sign in to comment.