Skip to content

Commit 2b744be

Browse files
author
Kolea Plesco
committed
Notice Publisher
1 parent 9ebcf87 commit 2b744be

7 files changed

Lines changed: 93 additions & 0 deletions

File tree

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ agraph-python==101.0.10
2121
decorator~=5.1.1
2222
urllib3[secure]
2323
semantic-version==2.10.0
24+
pysftp
25+

ted_sws/notice_publisher/__init__.py

Whitespace-only changes.

ted_sws/notice_publisher/adapters/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from abc import ABC
2+
from ted_sws.core.model.notice import Notice
3+
4+
5+
class NoticePublisherABC(ABC):
6+
def upload(self, source_path, remote_path=None) -> bool:
7+
pass
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pysftp
2+
3+
from ted_sws.core.model.notice import Notice
4+
from ted_sws.notice_publisher.adapters.notice_publisher import NoticePublisherABC
5+
6+
7+
class SFTPNoticePublisher(NoticePublisherABC):
8+
def __init__(self, hostname, username, password, port=22, remote_path=None):
9+
"""Constructor Method"""
10+
# Set connection object to None (initial value)
11+
self.connection = None
12+
self.hostname = hostname
13+
self.username = username
14+
self.password = password
15+
self.remote_path = remote_path
16+
self.port = port
17+
18+
def connect(self):
19+
"""Connects to the sftp server and returns the sftp connection object"""
20+
21+
try:
22+
# Get the sftp connection object
23+
self.connection = pysftp.Connection(
24+
host=self.hostname,
25+
username=self.username,
26+
password=self.password,
27+
port=self.port,
28+
)
29+
except Exception as err:
30+
raise Exception(err)
31+
32+
def disconnect(self):
33+
"""Closes the sftp connection"""
34+
self.connection.close()
35+
36+
def upload(self, source_path, remote_path=None) -> bool:
37+
"""
38+
Uploads the notice's METS manifestation to the sftp server remote path.
39+
"""
40+
if remote_path is None:
41+
remote_path = self.remote_path
42+
43+
if remote_path is None:
44+
raise ValueError("No remote path specified.")
45+
46+
try:
47+
self.connection.put(source_path, remote_path)
48+
return True
49+
except Exception as err:
50+
raise Exception(err)

ted_sws/notice_publisher/services/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from ted_sws.notice_publisher.adapters.notice_publisher import NoticePublisherABC
2+
from ted_sws.notice_publisher.adapters.sftp_notice_publisher import SFTPNoticePublisher
3+
from ted_sws.core.model.notice import Notice, NoticeStatus
4+
from ted_sws.data_manager.adapters.notice_repository import NoticeRepositoryABC
5+
import base64
6+
import tempfile
7+
8+
9+
def publish_notice_package(notice: Notice, notice_publisher: NoticePublisherABC, notice_repository: NoticeRepositoryABC,
10+
remote_path=None):
11+
mets_manifestation = notice.mets_manifestation
12+
if not mets_manifestation:
13+
raise ValueError("Notice does not have a METS manifestation to be published.")
14+
15+
package_content = base64.b64decode(bytes(mets_manifestation.object_data), validate=True)
16+
17+
source_file = tempfile.NamedTemporaryFile()
18+
source_file.write(package_content)
19+
20+
if notice_publisher.upload(source_file, remote_path):
21+
notice.update_status_to(NoticeStatus.PUBLISHED)
22+
notice_repository.update(notice)
23+
else:
24+
raise Exception(f"Notice {notice.ted_id} could not be published.")
25+
26+
27+
def publish_notice(notice: Notice, notice_repository: NoticeRepositoryABC, hostname, username, password,
28+
remote_path=None, port=22):
29+
notice_publisher = SFTPNoticePublisher(hostname=hostname, username=username, password=password, port=port,
30+
remote_path=remote_path)
31+
notice_publisher.connect()
32+
publish_notice_package(notice, notice_publisher, notice_repository)
33+
notice_publisher.disconnect()
34+
pass

0 commit comments

Comments
 (0)