Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add specific model for bitstream information (DEV-2893) #613

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/dsp_tools/models/xmlresource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import Optional, Union

import regex
Expand All @@ -11,6 +12,18 @@
from dsp_tools.models.xmlproperty import XMLProperty


@dataclass(frozen=True)
class BitstreamInfo:
"""
Represents a bitstream object, consisting of it's file name on the local file system,
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved
the internal file name assigned by SIPi and optionally its permissions.
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved
"""

local_file: str
internal_file_name: str
permissions: Permissions | None = None


class XMLResource: # pylint: disable=too-many-instance-attributes
"""
Represents a resource in the XML used for data import.
Expand Down Expand Up @@ -175,7 +188,7 @@ def get_propvals(

def get_bitstream_information(
self, internal_file_name_bitstream: str, permissions_lookup: dict[str, Permissions]
) -> Optional[dict[str, Union[str, Permissions]]]:
) -> BitstreamInfo | None:
"""
Get the bitstream object belonging to the resource
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -184,14 +197,13 @@ def get_bitstream_information(
permissions_lookup: Is used to resolve the permission id's to permission sets
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved

Returns:
A dict of the bitstream object
A BitstreamInfo object
"""
tmp: Optional[dict[str, Union[str, Permissions]]] = None
if self.bitstream:
bitstream = self.bitstream
tmp = {"value": bitstream.value, "internal_file_name": internal_file_name_bitstream}
if bitstream.permissions:
permissions = permissions_lookup.get(bitstream.permissions)
if permissions:
tmp["permissions"] = permissions
return tmp
if not self.bitstream:
return None
permissions = permissions_lookup.get(self.bitstream.permissions) if self.bitstream.permissions else None
return BitstreamInfo(
local_file=self.bitstream.value,
internal_file_name=internal_file_name_bitstream,
permissions=permissions,
)
6 changes: 3 additions & 3 deletions src/dsp_tools/utils/xmlupload/resource_multimedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dsp_tools.models.permission import Permissions
from dsp_tools.models.sipi import Sipi
from dsp_tools.models.xmlbitstream import XMLBitstream
from dsp_tools.models.xmlresource import XMLResource
from dsp_tools.models.xmlresource import BitstreamInfo, XMLResource
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.shared import try_network_action

Expand All @@ -19,7 +19,7 @@ def _upload_bitstream(
sipi_server: Sipi,
imgdir: str,
permissions_lookup: dict[str, Permissions],
) -> dict[str, str | Permissions] | None:
) -> BitstreamInfo | None:
"""
This function uploads a specified bitstream file to SIPI and then returns the file information from SIPI.

Expand Down Expand Up @@ -52,7 +52,7 @@ def handle_bitstream(
permissions_lookup: dict[str, Permissions],
sipi_server: Sipi,
imgdir: str,
) -> dict[str, Any] | None:
) -> BitstreamInfo | None:
"""
Upload a bitstream file to SIPI

Expand Down
15 changes: 12 additions & 3 deletions src/dsp_tools/utils/xmlupload/xmlupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dsp_tools.models.resource import KnoraStandoffXmlEncoder, ResourceInstance, ResourceInstanceFactory
from dsp_tools.models.sipi import Sipi
from dsp_tools.models.xmlpermission import XmlPermission
from dsp_tools.models.xmlresource import XMLResource
from dsp_tools.models.xmlresource import BitstreamInfo, XMLResource
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.shared import login, try_network_action
from dsp_tools.utils.xmlupload.ark2iri import convert_ark_v0_to_resource_iri
Expand Down Expand Up @@ -400,20 +400,29 @@ def _create_resource(
res_type: type,
resource: XMLResource,
resource_iri: str | None,
bitstream_information: dict[str, Any] | None,
bitstream_information: BitstreamInfo | None,
con: Connection,
permissions_lookup: dict[str, Permissions],
id2iri_mapping: dict[str, str],
) -> tuple[str, str] | None:
properties = resource.get_propvals(id2iri_mapping, permissions_lookup)
try:
if bitstream_information:
bitstream_info: dict[str, str | Permissions] | None = {
"value": bitstream_information.local_file,
"internal_file_name": bitstream_information.internal_file_name,
}
if bitstream_information.permissions and bitstream_info:
bitstream_info["permissions"] = bitstream_information.permissions
else:
bitstream_info = None
BalduinLandolt marked this conversation as resolved.
Show resolved Hide resolved
resource_instance: ResourceInstance = res_type(
con=con,
label=resource.label,
iri=resource_iri,
permissions=permissions_lookup.get(str(resource.permissions)),
creation_date=resource.creation_date,
bitstream=bitstream_information,
bitstream=bitstream_info,
values=properties,
)
created_resource: ResourceInstance = try_network_action(resource_instance.create)
Expand Down