|
1 | 1 | import base64 |
| 2 | +import pathlib |
2 | 3 | import tempfile |
3 | | -from pathlib import Path |
4 | | -from typing import Union |
5 | 4 |
|
6 | 5 | from ted_sws import config |
7 | | -from ted_sws.core.model.notice import Notice, NoticeStatus, UnsupportedStatusTransition |
| 6 | +from ted_sws.core.model.notice import Notice, NoticeStatus |
8 | 7 | from ted_sws.data_manager.adapters.notice_repository import NoticeRepositoryABC |
9 | | -from ted_sws.notice_packager.adapters.archiver import ARCHIVE_DEFAULT_FORMAT |
10 | | -from ted_sws.notice_publisher.adapters.notice_publisher import NoticePublisherFactory |
11 | | -from ted_sws.notice_publisher.adapters.notice_publisher_abc import NoticePublisherABC |
12 | | - |
13 | | - |
14 | | -class NoticePublishBuilder: |
15 | | - def __init__(self, notice: Union[Notice, str], notice_repository: NoticeRepositoryABC, |
16 | | - notice_publisher: NoticePublisherABC, remote_path: str = None): |
17 | | - self.notice_repository = notice_repository |
18 | | - if isinstance(notice, str): |
19 | | - notice_id = notice |
20 | | - notice: Notice = self.notice_repository.get(reference=notice_id) |
21 | | - if notice is None: |
22 | | - raise Exception(f"Notice {notice_id} could not be found.") |
23 | | - |
24 | | - self.notice = notice |
25 | | - self.check_publish_eligibility() |
26 | | - |
27 | | - self.notice_publisher = notice_publisher |
28 | | - self.remote_path = remote_path |
29 | | - |
30 | | - def check_publish_eligibility(self): |
31 | | - if self.notice.status != NoticeStatus.ELIGIBLE_FOR_PUBLISHING: |
32 | | - raise UnsupportedStatusTransition( |
33 | | - f"Notice {self.notice.ted_id} is not Eligible for Publishing. Status: {self.notice.status}") |
34 | | - |
35 | | - def package_content(self) -> bytes: |
36 | | - mets_manifestation = self.notice.mets_manifestation |
37 | | - if not mets_manifestation or not mets_manifestation.object_data: |
38 | | - raise ValueError("Notice does not have a METS manifestation to be published.") |
39 | | - |
40 | | - package_content = base64.b64decode(bytes(mets_manifestation.object_data, encoding='utf-8'), validate=True) |
41 | | - return package_content |
42 | | - |
43 | | - def build_remote_path(self) -> str: |
44 | | - remote_path = self.remote_path |
45 | | - if remote_path is None: |
46 | | - remote_path = f"{config.SFTP_PATH}/{self.notice.ted_id}" |
47 | | - if ARCHIVE_DEFAULT_FORMAT == "zip": |
48 | | - remote_path += '.zip' |
49 | | - return remote_path |
50 | | - |
51 | | - def publish(self): |
52 | | - source_file = tempfile.NamedTemporaryFile() |
53 | | - source_file.write(self.package_content()) |
54 | | - |
55 | | - try: |
56 | | - if self.notice_publisher.publish(source_path=Path(source_file.name), remote_path=self.build_remote_path()): |
57 | | - self.notice.update_status_to(NoticeStatus.PUBLISHED) |
58 | | - self.notice_repository.update(self.notice) |
59 | | - except Exception as e: |
60 | | - raise Exception(f"Notice {self.notice.ted_id} could not be published: " + str(e)) |
61 | | - |
62 | | - return self.notice.status == NoticeStatus.PUBLISHED |
63 | | - |
64 | | - |
65 | | -def publish_notice(notice: Union[Notice, str], notice_publisher: NoticePublisherABC, |
66 | | - notice_repository: NoticeRepositoryABC, remote_path=None) -> bool: |
67 | | - publish_builder = NoticePublishBuilder(notice, notice_repository, notice_publisher, remote_path) |
68 | | - return publish_builder.publish() |
69 | | - |
70 | | - |
71 | | -def publish_single_notice(notice: Union[Notice, str], notice_repository: NoticeRepositoryABC, hostname, username, |
72 | | - password, port=None, remote_path=None) -> bool: |
73 | | - notice_publisher: NoticePublisherABC = NoticePublisherFactory.get_publisher(hostname=hostname, username=username, |
74 | | - password=password, port=port, |
75 | | - remote_path=remote_path) |
76 | | - notice_publisher.connect() |
77 | | - notice_published: bool = publish_notice(notice, notice_publisher, notice_repository, remote_path) |
78 | | - notice_publisher.disconnect() |
79 | | - |
80 | | - return notice_published |
| 8 | +from ted_sws.notice_publisher.adapters.sftp_notice_publisher import SFTPPublisher |
| 9 | +from ted_sws.notice_publisher.adapters.sftp_publisher_abc import SFTPPublisherABC |
| 10 | + |
| 11 | + |
| 12 | +def publish_notice(notice: Notice, publisher: SFTPPublisherABC = SFTPPublisher(), |
| 13 | + remote_folder_path=config.SFTP_PATH) -> bool: |
| 14 | + """ |
| 15 | + This function publishes the METS manifestation for a Notice in Cellar. |
| 16 | + """ |
| 17 | + |
| 18 | + mets_manifestation = notice.mets_manifestation |
| 19 | + if not mets_manifestation or not mets_manifestation.object_data: |
| 20 | + raise ValueError("Notice does not have a METS manifestation to be published.") |
| 21 | + |
| 22 | + package_content = base64.b64decode(bytes(mets_manifestation.object_data, encoding='utf-8'), validate=True) |
| 23 | + remote_notice_path = f"{remote_folder_path}/{notice.ted_id}.zip" |
| 24 | + source_file = tempfile.NamedTemporaryFile() |
| 25 | + source_file.write(package_content) |
| 26 | + try: |
| 27 | + publisher.connect() |
| 28 | + if publisher.publish(source_path=pathlib.Path(source_file.name), |
| 29 | + remote_path=remote_notice_path): |
| 30 | + notice.update_status_to(NoticeStatus.PUBLISHED) |
| 31 | + publisher.disconnect() |
| 32 | + except Exception as e: |
| 33 | + raise Exception(f"Notice {notice.ted_id} could not be published: " + str(e)) |
| 34 | + |
| 35 | + return notice.status == NoticeStatus.PUBLISHED |
| 36 | + |
| 37 | + |
| 38 | +def publish_notice_by_id(notice_id: str, notice_repository: NoticeRepositoryABC, |
| 39 | + publisher: SFTPPublisherABC = SFTPPublisher(), remote_folder_path=config.SFTP_PATH) -> bool: |
| 40 | + """ |
| 41 | + This function publishes the METS manifestation of a Notice, based on notice_id, in Cellar. |
| 42 | + """ |
| 43 | + notice = notice_repository.get(reference=notice_id) |
| 44 | + result = publish_notice(notice=notice, publisher=publisher, remote_folder_path=remote_folder_path) |
| 45 | + if result: |
| 46 | + notice_repository.update(notice=notice) |
| 47 | + return result |
0 commit comments