Skip to content

Commit 40e59f8

Browse files
Update sftp_notice_publisher.py
1 parent d25b4b3 commit 40e59f8

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

ted_sws/notice_publisher/adapters/sftp_notice_publisher.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import pysftp
22

3-
from ted_sws.notice_publisher.adapters.notice_publisher_abc import NoticePublisherABC
43
from ted_sws import config
4+
from ted_sws.notice_publisher.adapters.sftp_publisher_abc import SFTPPublisherABC
55

66

7-
class SFTPNoticePublisher(NoticePublisherABC):
7+
class SFTPPublisher(SFTPPublisherABC):
8+
"""
9+
10+
"""
11+
812
connection: pysftp.Connection = None
913
default_host = config.SFTP_HOST
1014
default_user = config.SFTP_USER
1115
default_pass = config.SFTP_PASSWORD
1216
default_port = config.SFTP_PORT
1317

14-
def __init__(self, hostname=default_host, username=default_user, password=default_pass, port=default_port,
15-
remote_path=None):
18+
def __init__(self, hostname=default_host, username=default_user, password=default_pass, port=default_port):
1619
"""Constructor Method"""
1720
# Set connection object to None (initial value)
1821
self.hostname = hostname
1922
self.username = username
2023
self.password = password
21-
self.port = port or self.default_port
22-
self.remote_path = remote_path
24+
self.port = port
25+
self.is_connected = False
2326

2427
def connect(self):
2528
"""Connects to the sftp server and returns the sftp connection object"""
@@ -39,30 +42,31 @@ def connect(self):
3942
except Exception as err:
4043
raise Exception(err)
4144

45+
self.is_connected = True
46+
4247
def disconnect(self):
4348
"""Closes the sftp connection"""
44-
self.connection.close()
49+
if self.is_connected:
50+
self.connection.close()
51+
self.is_connected = False
4552

46-
def publish(self, source_path, remote_path=None) -> bool:
47-
return self.upload(source_path, remote_path)
53+
def __del__(self):
54+
self.disconnect()
4855

49-
def upload(self, source_path, remote_path=None) -> bool:
56+
def publish(self, source_path, remote_path) -> bool:
5057
"""
51-
Uploads the notice's METS manifestation to the sftp server remote path.
58+
Publish file_content to the sftp server remote path.
5259
"""
53-
if remote_path is None:
54-
remote_path = self.remote_path
55-
56-
if remote_path is None:
57-
raise ValueError("No remote path specified.")
58-
5960
try:
6061
self.connection.put(source_path, remote_path)
6162
return True
6263
except Exception as err:
6364
raise Exception(err)
6465

6566
def remove(self, remote_path) -> bool:
67+
"""
68+
69+
"""
6670
try:
6771
self.connection.unlink(remote_path)
6872
return True

0 commit comments

Comments
 (0)