Skip to content

Commit

Permalink
Resource policies duplicity
Browse files Browse the repository at this point in the history
assigned one group to a resource policy based on the action type
  • Loading branch information
Paurikova2 authored Oct 10, 2024
1 parent 13a67fe commit 3f9c999
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/pump/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class collections:
]

TYPE = 3
ITEM = "ITEM"
BITSTREAM = "BITSTREAM"

def __init__(self, col_file_str: str, com2col_file_str: str, metadata_file_str: str):
self._col = read_json(col_file_str)
Expand All @@ -33,6 +35,7 @@ def __init__(self, col_file_str: str, com2col_file_str: str, metadata_file_str:

self._logos = {}
self._groups_id2uuid = {}
self._groups_uuid2type = {}

if len(self._col) == 0:
_logger.info(f"Empty input collections: [{col_file_str}].")
Expand Down Expand Up @@ -81,6 +84,10 @@ def imported_groups(self):
def groups_id2uuid(self):
return self._groups_id2uuid

@property
def groups_uuid2type(self):
return self._groups_uuid2type

@time_method
def import_to(self, dspace, handles, metadatas, coms):
expected = len(self)
Expand Down Expand Up @@ -150,6 +157,7 @@ def import_to(self, dspace, handles, metadatas, coms):
resp = dspace.put_collection_bitstream_read_group(col_uuid)
self._groups_id2uuid.setdefault(str(group_col), []).append(resp['id'])
self._imported["group"] += 1
self._groups_uuid2type[resp['id']] = collections.BITSTREAM
except Exception as e:
_logger.error(
f'put_collection_bitstream_read_group: [{col_id}] failed [{str(e)}]')
Expand All @@ -158,6 +166,7 @@ def import_to(self, dspace, handles, metadatas, coms):
resp = dspace.put_collection_item_read_group(col_uuid)
self._groups_id2uuid.setdefault(str(group_col), []).append(resp['id'])
self._imported["group"] += 1
self._groups_uuid2type[resp['id']] = collections.ITEM
except Exception as e:
_logger.error(
f'put_collection_item_read_group: [{col_id}] failed [{str(e)}]')
Expand All @@ -172,6 +181,7 @@ def serialize(self, file_str: str):
"logos": self._logos,
"groups_id2uuid": self._groups_id2uuid,
"imported": self._imported,
"groups_uuid2type": self._groups_uuid2type,
}
serialize(file_str, data)

Expand All @@ -183,3 +193,4 @@ def deserialize(self, file_str: str):
self._logos = data["logos"]
self._groups_id2uuid = data["groups_id2uuid"]
self._imported = data["imported"]
self._groups_uuid2type = data["groups_uuid2type"]
33 changes: 30 additions & 3 deletions src/pump/_resourcepolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def __init__(self, resourcepolicy_file_str: str):
"respol": 0,
}

DEFAULT_BITSTREAM_READ = "DEFAULT_BITSTREAM_READ"

def __len__(self):
return len(self._respol)

Expand Down Expand Up @@ -92,11 +94,36 @@ def import_to(self, env, dspace, repo):
# get group if it is not none
eg_id = res_policy['epersongroup_id']
if eg_id is not None:
group_list1 = repo.groups.uuid(eg_id)
group_list2 = repo.collections.group_uuid(eg_id)
group_list = set(group_list1 + group_list2)
# groups created with coll and comm are already in the group
group_list = repo.groups.uuid(eg_id)
if len(group_list) == 0:
continue
if len(group_list) > 1:
if len(group_list) != 2:
raise RuntimeError(
f'Unexpected size of mapped groups to group [{eg_id}]: {len(group_list)}. '
f'Expected size: 2.')
group_types = repo.collections.groups_uuid2type
# Determine the target type based on the action
target_type = (
repo.collections.BITSTREAM
if dspace_actions[actionId] == resourcepolicies.DEFAULT_BITSTREAM_READ
else repo.collections.ITEM
)
# Filter group_list to find the appropriate group based on type using list comprehension
group_type_list = [
group for group in group_list
if group in group_types and group_types[group] == target_type
]

if len(group_type_list) != 1:
raise RuntimeError(
f'Unexpected size of filtered groups for group [{eg_id}] '
f'of type [{target_type}]: {len(group_type_list)}. Expected size: 1.'
)

group_list = group_type_list

imported_groups = 0
for group in group_list:
params['group'] = group
Expand Down
9 changes: 4 additions & 5 deletions src/repo_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,14 @@ def deserialize(resume: bool, obj, cache_file: str) -> bool:
repo.diff(repo.usermetadatas)
_logger.info(import_sep)

# before importing of resource policies we have to delete all
# created data
repo.raw_db_7.delete_resource_policy()

# import bitstreams
# import resourcepolicy
cache_file = env["cache"]["resourcepolicy"]
if deserialize(args.resume, repo.resourcepolicies, cache_file):
_logger.info(f"Resuming resourcepolicies [{repo.resourcepolicies.imported}]")
else:
# before importing of resource policies we have to delete all
# created data
repo.raw_db_7.delete_resource_policy()
repo.resourcepolicies.import_to(env, dspace_be, repo)
repo.resourcepolicies.serialize(cache_file)
repo.diff(repo.resourcepolicies)
Expand Down

0 comments on commit 3f9c999

Please sign in to comment.