generated from mlibrary/python-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dor-file-provider-change
- Loading branch information
Showing
23 changed files
with
359 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
name: Continuous Integration | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
pull_request: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
ci: | ||
if: ${{ github.event_name == 'push' || github.event.pull_request.merged == true }} | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Run tests | ||
run: | | ||
./init.sh | ||
docker compose run --rm app poetry run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#Scratch workflow placeholder to create new workflows from a branch other then main | ||
name: Scratch | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
tag: | ||
description: tag | ||
required: true | ||
|
||
jobs: | ||
get_short_tag: | ||
name: get-short-tag | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: save short tag to environment | ||
run: echo "short_tag=$(echo ${{ github.event.inputs.tag }} | head -c 8 )" >> $GITHUB_ENV | ||
- name: echo env var | ||
run: echo "short_tag ${short_tag}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import uuid | ||
from typing import Callable, Type, Tuple | ||
|
||
import typer | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from dor.adapters.bag_adapter import BagAdapter | ||
from dor.adapters.catalog import Base, _custom_json_serializer | ||
from dor.config import config | ||
from dor.domain.events import ( | ||
Event, | ||
BinCataloged, | ||
PackageReceived, | ||
PackageStored, | ||
PackageSubmitted, | ||
PackageUnpacked, | ||
PackageVerified, | ||
) | ||
from dor.providers.file_system_file_provider import FilesystemFileProvider | ||
from dor.providers.package_resource_provider import PackageResourceProvider | ||
from dor.providers.translocator import Translocator, Workspace | ||
from dor.service_layer.handlers.catalog_bin import catalog_bin | ||
from dor.service_layer.handlers.receive_package import receive_package | ||
from dor.service_layer.handlers.store_files import store_files | ||
from dor.service_layer.handlers.unpack_package import unpack_package | ||
from dor.service_layer.handlers.verify_package import verify_package | ||
from dor.service_layer.message_bus.memory_message_bus import MemoryMessageBus | ||
from dor.service_layer.unit_of_work import SqlalchemyUnitOfWork | ||
from gateway.ocfl_repository_gateway import OcflRepositoryGateway | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
def bootstrap() -> Tuple[MemoryMessageBus, SqlalchemyUnitOfWork]: | ||
gateway = OcflRepositoryGateway(storage_path=config.storage_path) | ||
|
||
engine = create_engine( | ||
config.get_database_engine_url(), json_serializer=_custom_json_serializer | ||
) | ||
session_factory = sessionmaker(bind=engine) | ||
uow = SqlalchemyUnitOfWork(gateway=gateway, session_factory=session_factory) | ||
|
||
translocator = Translocator( | ||
inbox_path=config.inbox_path, | ||
workspaces_path=config.workspaces_path, | ||
minter = lambda: str(uuid.uuid4()) | ||
) | ||
file_provider = FilesystemFileProvider() | ||
|
||
handlers: dict[Type[Event], list[Callable]] = { | ||
PackageSubmitted: [lambda event: receive_package(event, uow, translocator)], | ||
PackageReceived: [lambda event: verify_package(event, uow, BagAdapter, Workspace)], | ||
PackageVerified: [ | ||
lambda event: unpack_package( | ||
event, uow, BagAdapter, PackageResourceProvider, Workspace, file_provider | ||
) | ||
], | ||
PackageUnpacked: [lambda event: store_files(event, uow, Workspace)], | ||
PackageStored: [lambda event: catalog_bin(event, uow)], | ||
BinCataloged: [] | ||
} | ||
message_bus = MemoryMessageBus(handlers) | ||
return (message_bus, uow) | ||
|
||
|
||
@app.command() | ||
def initialize(): | ||
gateway = OcflRepositoryGateway(storage_path=config.storage_path) | ||
gateway.create_repository() | ||
|
||
engine = create_engine( | ||
config.get_database_engine_url(), json_serializer=_custom_json_serializer | ||
) | ||
Base.metadata.create_all(engine) | ||
|
||
|
||
@app.command() | ||
def store( | ||
package_identifier: str = typer.Option(help="Name of the package directory"), | ||
): | ||
message_bus, uow = bootstrap() | ||
event = PackageSubmitted(package_identifier=package_identifier) | ||
message_bus.handle(event, uow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from dor.domain.events import PackageStored, BinCataloged | ||
from dor.domain.models import Bin | ||
from dor.service_layer.unit_of_work import AbstractUnitOfWork | ||
|
||
def catalog_bin(event: PackageStored, uow: AbstractUnitOfWork) -> None: | ||
root_resource = [resource for resource in event.resources if resource.type == 'Monograph'][0] | ||
common_metadata_file = [ | ||
metadata_file for metadata_file in root_resource.metadata_files if "common" in metadata_file.ref.locref | ||
][0] | ||
common_metadata_file_path = Path(common_metadata_file.ref.locref) | ||
object_files = uow.gateway.get_object_files(event.identifier) | ||
matching_object_file = [ | ||
object_file for object_file in object_files if common_metadata_file_path == object_file.logical_path | ||
][0] | ||
literal_common_metadata_path = matching_object_file.literal_path | ||
common_metadata = json.loads(literal_common_metadata_path.read_text()) | ||
|
||
bin = Bin( | ||
identifier=event.identifier, | ||
alternate_identifiers=[root_resource.alternate_identifier.id], | ||
common_metadata=common_metadata, | ||
package_resources=event.resources | ||
) | ||
with uow: | ||
uow.catalog.add(bin) | ||
uow.commit() | ||
|
||
uow.add_event(BinCataloged(identifier=event.identifier, tracking_identifier=event.tracking_identifier)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.