Skip to content

Commit

Permalink
Fix style check issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Cobden committed Jan 18, 2017
1 parent e3e81a9 commit bf533d9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
21 changes: 20 additions & 1 deletion src/twisted/protocols/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,16 @@ def reply(self, key, *args):
msg = RESPONSE[key] % args
self.sendEncodedLine(msg)


def sendEncodedLine(self, line):
"""
(Private) Encodes and sends a line
@param line: C{str}
"""
self.sendLine(line.encode(self._encoding))


def connectionMade(self):
self.state = self.UNAUTH
self.setTimeout(self.timeOut)
Expand Down Expand Up @@ -949,7 +956,7 @@ def ftp_PASV(self):

def ftp_PORT(self, address):
addr = tuple(map(int, address.split(',')))
ip = '%d.%d.%d.%d' % addr[:4]
ip = '%d.%d.%d.%d' % tuple(addr[:4])
port = addr[4] << 8 | addr[5]

# if we have a DTP port set up, lose it.
Expand Down Expand Up @@ -2451,6 +2458,7 @@ def buildProtocol(self, addr):
return self.protocol



class FTPClientBasic(basic.LineReceiver):
"""
Foundations of an FTP client.
Expand Down Expand Up @@ -2495,16 +2503,27 @@ def _cb_greeting(self, greeting):
def sendLine(self, line):
"""
(Private) Sends a line, unless line is None.
@param line: Line to send
@type line: bytes
"""
if line is None:
return
basic.LineReceiver.sendLine(self, line)


def sendEncodedLine(self, line):
"""
(Private) Encodes and sends a line, unless line is None.
@param line: Line to send
@type line: str
"""
if line is None:
return
self.sendLine(line.encode(self._encoding))


def sendNextCommand(self):
"""
(Private) Processes the next command in the queue.
Expand Down
58 changes: 38 additions & 20 deletions src/twisted/test/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ def connectionLost(self, reason):
self.d.callback(self)


def passive_mode_msg(protocol, host='127.0.0.1', port=12345):

def passivemode_msg(protocol, host='127.0.0.1', port=12345):
"""
Construct a passive mode message with the correct encoding
@param protocol: the FTP protocol from which to base the encoding
@param host: the hostname
@param port: the port
@return: the passive mode message
"""
msg = '227 Entering Passive Mode (%s).' % (ftp.encodeHostPort(host, port),)
return msg.encode(protocol._encoding)

Expand Down Expand Up @@ -759,6 +768,7 @@ def downloadDone(result):
return downloader.buffer
return chainDeferred.addCallback(downloadDone)


def test_LISTEmpty(self):
"""
When listing empty folders, LIST returns an empty response.
Expand Down Expand Up @@ -1669,7 +1679,7 @@ def cbConnect(host, port, factory):
d.addCallback(cbRetr, proto)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'RETR spam\r\n')
self.transport.clear()
self.client.lineReceived(b'226 Transfer Complete.')
Expand Down Expand Up @@ -1738,7 +1748,7 @@ def cbConnect(host, port, factory):
self.assertFailure(d, ftp.CommandFailed)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'RETR spam\r\n')
self.transport.clear()
self.client.lineReceived(b'550 spam: No such file or directory')
Expand Down Expand Up @@ -1819,7 +1829,7 @@ def cbConnect(host, port, factory):
d2.addCallback(cbFinish)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'STOR spam\r\n')
self.transport.clear()
self.client.lineReceived(b'226 Transfer Complete.')
Expand Down Expand Up @@ -1855,7 +1865,7 @@ def cbConnect(host, port, factory):
self.assertFailure(d2, ftp.CommandFailed)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'STOR spam\r\n')
self.transport.clear()
self.client.lineReceived(
Expand All @@ -1874,7 +1884,7 @@ def test_STOR(self):
tr = proto_helpers.StringTransport()
self.client.passive = False
def generatePort(portCmd):
portCmd.text = 'PORT %s' % ftp.encodeHostPort('127.0.0.1', 9876)
portCmd.text = 'PORT ' + ftp.encodeHostPort('127.0.0.1', 9876)
portCmd.protocol.makeConnection(tr)

def cbStore(sender):
Expand Down Expand Up @@ -1951,7 +1961,7 @@ def cbConnect(host, port, factory):
d = self.client.list('foo/bar', fileList).addCallback(cbList, fileList)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'LIST foo/bar\r\n')
self.client.lineReceived(b'226 Transfer Complete.')
return d
Expand Down Expand Up @@ -2027,7 +2037,7 @@ def cbConnect(host, port, factory):
self.assertFailure(d, ftp.CommandFailed)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'LIST foo/bar\r\n')
self.client.lineReceived(b'550 foo/bar: No such file or directory')
return d
Expand Down Expand Up @@ -2106,7 +2116,7 @@ def cbConnect(host, port, factory):
d = self.client.nlst('foo/bar', lstproto).addCallback(cbList, lstproto)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'NLST foo/bar\r\n')
self.client.lineReceived(b'226 Transfer Complete.')
return d
Expand Down Expand Up @@ -2137,7 +2147,7 @@ def cbConnect(host, port, factory):
self.assertFailure(d, ftp.CommandFailed)
self.assertEqual(self.transport.value(), b'PASV\r\n')
self.transport.clear()
self.client.lineReceived(passive_mode_msg(self.client))
self.client.lineReceived(passivemode_msg(self.client))
self.assertEqual(self.transport.value(), b'NLST foo/bar\r\n')
self.client.lineReceived(b'550 foo/bar: No such file or directory')
return d
Expand Down Expand Up @@ -2215,7 +2225,8 @@ def test_renameFromToFailingOnRenameTo(self):
self.assertEqual(self.transport.value(), b'RNFR /spam\r\n')
self.transport.clear()

self.client.lineReceived(b'350 Requested file action pending further information.\r\n')
self.client.lineReceived(
b'350 Requested file action pending further information.\r\n')
self.assertEqual(self.transport.value(), b'RNTO /ham\r\n')
self.client.lineReceived(b'550 Requested file unavailable.\r\n')
return self.assertFailure(d, ftp.CommandFailed)
Expand Down Expand Up @@ -2430,15 +2441,20 @@ def test_multilineRemoveDirectoryResponse(self):

class FTPClientBasicTests(unittest.TestCase):

def testGreeting(self):
# The first response is captured as a greeting.
def test_greeting(self):
"""
The first response is captured as a greeting.
"""
ftpClient = ftp.FTPClientBasic()
ftpClient.lineReceived(b'220 Imaginary FTP.')
self.assertEqual(['220 Imaginary FTP.'], ftpClient.greeting)

def testResponseWithNoMessage(self):
# Responses with no message are still valid, i.e. three digits followed
# by a space is complete response.

def test_responseWithNoMessage(self):
"""
Responses with no message are still valid, i.e. three digits
followed by a space is complete response.
"""
ftpClient = ftp.FTPClientBasic()
ftpClient.lineReceived(b'220 ')
self.assertEqual(['220 '], ftpClient.greeting)
Expand Down Expand Up @@ -3476,8 +3492,8 @@ def getFileContent(self):
class CloseTestWriter:
closeStarted = False
def receive(self):
self.s = BytesIO()
fc = ftp.FileConsumer(self.s)
self.buffer = BytesIO()
fc = ftp.FileConsumer(self.buffer)
return defer.succeed(fc)
def close(self):
self.closeStarted = True
Expand All @@ -3495,8 +3511,10 @@ class FTPCloseTests(unittest.TestCase):
"""Tests that the server invokes IWriteFile.close"""

def test_write(self):
"""Confirm that FTP uploads (i.e. ftp_STOR) correctly call and wait
upon the IWriteFile object's close() method"""
"""
Confirm that FTP uploads (i.e. ftp_STOR) correctly call and wait
upon the IWriteFile object's close() method
"""
f = ftp.FTP()
f.workingDirectory = ["root"]
f.shell = CloseTestShell()
Expand Down

0 comments on commit bf533d9

Please sign in to comment.