Commit 6317f21f authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

SFTPConnection.readBinaryFile now returns binary str, instead of StringIO object.

also readAsciiFile normilises CRLF/CR/LF.
parent 4a5fa825
...@@ -94,28 +94,26 @@ class SFTPConnection: ...@@ -94,28 +94,26 @@ class SFTPConnection:
except error, msg: except error, msg:
raise SFTPError(str(msg) + ' while writing file %s on %s' % (filepath, path, self.url)) raise SFTPError(str(msg) + ' while writing file %s on %s' % (filepath, path, self.url))
def _getFile(self, filepath, binary=True): def _getFile(self, filepath):
"""Retrieve the file""" """Retrieve the file"""
try: try:
if binary: # always open with binary mode, otherwise paramiko will raise
tmp_file = self.conn.file(filepath, 'rb') # UnicodeDecodeError for non-utf8 data. also SFTP has no ASCII
else: # mode like FTP that normalises CRLF/CR/LF.
tmp_file = self.conn.file(filepath, 'r') tmp_file = self.conn.file(filepath, 'rb')
tmp_file.seek(0) tmp_file.seek(0)
return Binary(tmp_file.read()) return tmp_file.read()
except error, msg: except error, msg:
raise SFTPError(str(msg) + ' while retrieving file %s from %s' % (filepath, self.url)) raise SFTPError(str(msg) + ' while retrieving file %s from %s' % (filepath, self.url))
def readBinaryFile(self, filepath): def readBinaryFile(self, filepath):
"""Retrieve the file in binary mode""" """Retrieve the file in binary mode"""
return StringIO(str(self._getFile(filepath, binary=True))) return self._getFile(filepath)
def readAsciiFile(self, filepath): def readAsciiFile(self, filepath):
"""Retrieve the file in ASCII mode""" """Retrieve the file in ASCII mode"""
binary = self._getFile(filepath, binary=False) # normalise CRLF/CR/LF like FTP's ASCII mode transfer.
if binary: return os.linesep.join(self._getFile(filepath).splitlines())
return binary.data
return None
def getDirectoryContent(self, path): def getDirectoryContent(self, path):
"""retrieve all entries in a givan path as a list""" """retrieve all entries in a givan path as a list"""
...@@ -133,7 +131,7 @@ class SFTPConnection: ...@@ -133,7 +131,7 @@ class SFTPConnection:
try: try:
self.conn.unlink(filepath) self.conn.unlink(filepath)
except error, msg: except error, msg:
raise SFTPError(str(msg) + 'while trying to delete %s on %s' % (filename, self.url)) raise SFTPError(str(msg) + 'while trying to delete %s on %s' % (filepath, self.url))
def renameFile(self, old_path, new_path): def renameFile(self, old_path, new_path):
"""Rename a file""" """Rename a file"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment