Skip to content

Commit

Permalink
add type command to ftp (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
beck3905 authored Sep 2, 2023
1 parent b436f86 commit a0c870a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions smart_open/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import smart_open.utils
from ftplib import FTP, FTP_TLS, error_reply
import types

logger = logging.getLogger(__name__)

SCHEMES = ("ftp", "ftps")
Expand Down Expand Up @@ -55,8 +56,13 @@ def open_uri(uri, mode, transport_params):
uri_path = parsed_uri.pop("uri_path")
scheme = parsed_uri.pop("scheme")
secure_conn = True if scheme == "ftps" else False
return open(uri_path, mode, secure_connection=secure_conn,
transport_params=transport_params, **parsed_uri)
return open(
uri_path,
mode,
secure_connection=secure_conn,
transport_params=transport_params,
**parsed_uri,
)


def convert_transport_params_to_args(transport_params):
Expand Down Expand Up @@ -90,7 +96,9 @@ def _connect(hostname, username, port, password, secure_connection, transport_pa
try:
ftp.login(username, password)
except error_reply as e:
logger.error("Unable to login to FTP server: try checking the username and password!")
logger.error(
"Unable to login to FTP server: try checking the username and password!"
)
raise e
if secure_connection:
ftp.prot_p()
Expand All @@ -99,7 +107,7 @@ def _connect(hostname, username, port, password, secure_connection, transport_pa

def open(
path,
mode="r",
mode="rb",
host=None,
user=None,
password=None,
Expand Down Expand Up @@ -146,13 +154,15 @@ def open(
except KeyError:
raise ValueError(f"unsupported mode: {mode!r}")
ftp_mode, file_obj_mode = mode_to_ftp_cmds[mode]
conn.voidcmd("TYPE I")
socket = conn.transfercmd(f"{ftp_mode} {path}")
fobj = socket.makefile(file_obj_mode)

def full_close(self):
self.orig_close()
self.socket.close()
self.conn.close()

fobj.orig_close = fobj.close
fobj.socket = socket
fobj.conn = conn
Expand Down

0 comments on commit a0c870a

Please sign in to comment.