From ddf1e357b43a8ca69848d550021f269b48361214 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 12:23:33 -0400 Subject: Additional PEP8 fixes. --- paramiko/sftp.py | 20 +++++------ paramiko/sftp_client.py | 79 ++++++++++++++++++++++++++---------------- paramiko/sftp_server.py | 92 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 119 insertions(+), 72 deletions(-) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index f44a804d..e6786d10 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -26,15 +26,17 @@ from paramiko.message import Message from paramiko.py3compat import byte_chr, byte_ord -CMD_INIT, CMD_VERSION, CMD_OPEN, CMD_CLOSE, CMD_READ, CMD_WRITE, CMD_LSTAT, CMD_FSTAT, \ - CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, CMD_REMOVE, CMD_MKDIR, \ - CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, CMD_READLINK, CMD_SYMLINK = range(1, 21) +CMD_INIT, CMD_VERSION, CMD_OPEN, CMD_CLOSE, CMD_READ, CMD_WRITE, CMD_LSTAT, \ + CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, \ + CMD_REMOVE, CMD_MKDIR, CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, \ + CMD_READLINK, CMD_SYMLINK = range(1, 21) CMD_STATUS, CMD_HANDLE, CMD_DATA, CMD_NAME, CMD_ATTRS = range(101, 106) CMD_EXTENDED, CMD_EXTENDED_REPLY = range(200, 202) SFTP_OK = 0 -SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, SFTP_BAD_MESSAGE, \ - SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED = range(1, 9) +SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, \ + SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, \ + SFTP_OP_UNSUPPORTED = range(1, 9) SFTP_DESC = ['Success', 'End of file', @@ -98,7 +100,7 @@ class BaseSFTP (object): self.sock = None self.ultra_debug = False - ### internals... + # ...internals... def _send_version(self): self._send_packet(CMD_INIT, struct.pack('>I', _VERSION)) @@ -124,7 +126,7 @@ class BaseSFTP (object): msg.add(*extension_pairs) self._send_packet(CMD_VERSION, msg) return version - + def _log(self, level, msg, *args): self.logger.log(level, msg, *args) @@ -154,7 +156,7 @@ class BaseSFTP (object): break else: x = self.sock.recv(n) - + if len(x) == 0: raise EOFError() out += x @@ -162,7 +164,6 @@ class BaseSFTP (object): return out def _send_packet(self, t, packet): - #self._log(DEBUG2, 'write: %s (len=%d)' % (CMD_NAMES.get(t, '0x%02x' % t), len(packet))) packet = asbytes(packet) out = struct.pack('>I', len(packet) + 1) + byte_chr(t) + packet if self.ultra_debug: @@ -181,6 +182,5 @@ class BaseSFTP (object): self._log(DEBUG, util.format_binary(data, 'IN: ')) if size > 0: t = byte_ord(data[0]) - #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) return t, data[1:] return 0, bytes() diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 12a9506f..245e5072 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -28,13 +28,15 @@ from paramiko import util from paramiko.channel import Channel from paramiko.message import Message from paramiko.common import INFO, DEBUG, o777 -from paramiko.py3compat import bytestring, b, u, long, string_types, bytes_types -from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READDIR, \ - CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_CREATE, \ - SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, CMD_OPEN, CMD_REMOVE, \ - CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, \ - CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, SFTP_OK, \ - SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED +from paramiko.py3compat import bytestring, b, u, long, string_types, \ + bytes_types +from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, \ + CMD_READDIR, CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ + SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, \ + CMD_OPEN, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, \ + CMD_ATTRS, CMD_LSTAT, CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, \ + CMD_REALPATH, CMD_STATUS, SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, \ + SFTP_PERMISSION_DENIED from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException @@ -56,6 +58,7 @@ def _to_unicode(s): except UnicodeError: return s + b_slash = b'/' @@ -93,13 +96,16 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if type(sock) is Channel: # override default logger transport = self.sock.get_transport() - self.logger = util.get_logger(transport.get_log_channel() + '.sftp') + self.logger = util.get_logger( + transport.get_log_channel() + '.sftp') self.ultra_debug = transport.get_hexdump() try: server_version = self._send_version() except EOFError: raise SSHException('EOF during negotiation') - self._log(INFO, 'Opened sftp connection (server version %d)' % server_version) + self._log( + INFO, + 'Opened sftp connection (server version %d)' % server_version) @classmethod def from_transport(cls, t, window_size=None, max_packet_size=None): @@ -111,7 +117,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): OpenSSH and should work adequately for both files transfers and interactive sessions. - :param .Transport t: an open `.Transport` which is already authenticated + :param .Transport t: an open `.Transport` which is already + authenticated :param int window_size: optional window size for the `.SFTPClient` session. :param int max_packet_size: @@ -136,9 +143,12 @@ class SFTPClient(BaseSFTP, ClosingContextManager): for m in msg: self._log(level, m, *args) else: - # escape '%' in msg (they could come from file or directory names) before logging - msg = msg.replace('%','%%') - super(SFTPClient, self)._log(level, "[chan %s] " + msg, *([self.sock.get_name()] + list(args))) + # escape '%' in msg (they could come from file or directory names) + # before logging + msg = msg.replace('%', '%%') + super(SFTPClient, self)._log( + level, + "[chan %s] " + msg, *([self.sock.get_name()] + list(args))) def close(self): """ @@ -160,7 +170,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): def listdir(self, path='.'): """ - Return a list containing the names of the entries in the given ``path``. + Return a list containing the names of the entries in the given + ``path``. The list is in arbitrary order. It does not include the special entries ``'.'`` and ``'..'`` even if they are present in the folder. @@ -328,7 +339,9 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if t != CMD_HANDLE: raise SFTPError('Expected handle') handle = msg.get_binary() - self._log(DEBUG, 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle))) + self._log( + DEBUG, + 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle))) return SFTPFile(self, handle, mode, bufsize) # Python continues to vacillate about "open" vs "file"... @@ -480,12 +493,12 @@ class SFTPClient(BaseSFTP, ClosingContextManager): def utime(self, path, times): """ - Set the access and modified times of the file specified by ``path``. If - ``times`` is ``None``, then the file's access and modified times are set - to the current time. Otherwise, ``times`` must be a 2-tuple of numbers, - of the form ``(atime, mtime)``, which is used to set the access and - modified times, respectively. This bizarre API is mimicked from Python - for the sake of consistency -- I apologize. + Set the access and modified times of the file specified by ``path``. + If ``times`` is ``None``, then the file's access and modified times + are set to the current time. Otherwise, ``times`` must be a 2-tuple + of numbers, of the form ``(atime, mtime)``, which is used to set the + access and modified times, respectively. This bizarre API is mimicked + from Python for the sake of consistency -- I apologize. :param str path: path of the file to modify :param tuple times: @@ -578,7 +591,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self._cwd = None return if not stat.S_ISDIR(self.stat(path).st_mode): - raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) + raise SFTPError( + errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) self._cwd = b(self.normalize(path)) def getcwd(self): @@ -639,7 +653,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if confirm: s = self.stat(remotepath) if s.st_size != size: - raise IOError('size mismatch in put! %d != %d' % (s.st_size, size)) + raise IOError( + 'size mismatch in put! %d != %d' % (s.st_size, size)) else: s = SFTPAttributes() return s @@ -663,7 +678,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): whether to do a stat() on the file afterwards to confirm the file size - :return: an `.SFTPAttributes` object containing attributes about the given file + :return: an `.SFTPAttributes` object containing attributes about the + given file .. versionadded:: 1.4 .. versionchanged:: 1.7.4 @@ -699,7 +715,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager): reader=fr, writer=fl, file_size=file_size, callback=callback ) - return size + return file_size def get(self, remotepath, localpath, callback=None): """ @@ -721,9 +737,10 @@ class SFTPClient(BaseSFTP, ClosingContextManager): size = self.getfo(remotepath, fl, callback) s = os.stat(localpath) if s.st_size != size: - raise IOError('size mismatch in get! %d != %d' % (s.st_size, size)) + raise IOError( + 'size mismatch in get! %d != %d' % (s.st_size, size)) - ### internals... + # ...internals... def _request(self, t, *arg): num = self._async_request(type(None), t, *arg) @@ -745,7 +762,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): elif isinstance(item, SFTPAttributes): item._pack(msg) else: - raise Exception('unknown type for %r type %r' % (item, type(item))) + raise Exception( + 'unknown type for %r type %r' % (item, type(item))) num = self.request_number self._expecting[num] = fileobj self.request_number += 1 @@ -765,7 +783,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self._lock.acquire() try: if num not in self._expecting: - # might be response for a file that was closed before responses came back + # might be response for a file that was closed before + # responses came back self._log(DEBUG, 'Unexpected response #%d' % (num,)) if waitfor is None: # just doing a single check @@ -780,7 +799,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if t == CMD_STATUS: self._convert_status(msg) return t, msg - if fileobj is not type(None): + if fileobj is not None: fileobj._async_response(t, msg, num) if waitfor is None: # just doing a single check diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index ce287e8f..6c59b2fa 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -36,13 +36,14 @@ from paramiko.server import SubsystemHandler # known hash algorithms for the "check-file" extension -from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, CMD_NAME, \ - SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ - SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_EXCL, \ - CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, CMD_DATA, CMD_WRITE, \ - CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_OPENDIR, CMD_READDIR, \ - CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, \ - CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, CMD_EXTENDED, SFTP_OP_UNSUPPORTED +from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, \ + CMD_NAME, SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, \ + SFTP_FLAG_WRITE, SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, \ + SFTP_FLAG_EXCL, CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, \ + CMD_DATA, CMD_WRITE, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, \ + CMD_OPENDIR, CMD_READDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, \ + CMD_SETSTAT, CMD_FSETSTAT, CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, \ + CMD_EXTENDED, SFTP_OP_UNSUPPORTED _hash_class = { 'sha1': sha1, @@ -57,7 +58,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): Use `.Transport.set_subsystem_handler` to activate this class. """ - def __init__(self, channel, name, server, sftp_si=SFTPServerInterface, *largs, **kwargs): + def __init__(self, channel, name, server, sftp_si=SFTPServerInterface, + *largs, **kwargs): """ The constructor for SFTPServer is meant to be called from within the `.Transport` as a subsystem handler. ``server`` and any additional @@ -86,9 +88,13 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def _log(self, level, msg): if issubclass(type(msg), list): for m in msg: - super(SFTPServer, self)._log(level, "[chan " + self.sock.get_name() + "] " + m) + super(SFTPServer, self)._log( + level, + "[chan " + self.sock.get_name() + "] " + m) else: - super(SFTPServer, self)._log(level, "[chan " + self.sock.get_name() + "] " + msg) + super(SFTPServer, self)._log( + level, + "[chan " + self.sock.get_name() + "] " + msg) def start_subsystem(self, name, transport, channel): self.sock = channel @@ -121,7 +127,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def finish_subsystem(self): self.server.session_ended() super(SFTPServer, self).finish_subsystem() - # close any file handles that were left open (so we can return them to the OS quickly) + # close any file handles that were left open + # (so we can return them to the OS quickly) for f in self.file_table.values(): f.close() for f in self.folder_table.values(): @@ -175,7 +182,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): with open(filename, 'w+') as f: f.truncate(attr.st_size) - ### internals... + # ...internals... def _response(self, request_number, t, *arg): msg = Message() @@ -190,7 +197,9 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif type(item) is SFTPAttributes: item._pack(msg) else: - raise Exception('unknown type for ' + repr(item) + ' type ' + repr(type(item))) + raise Exception( + 'unknown type for {!r} type {!r}'.format( + item, type(item))) self._send_packet(t, msg) def _send_handle_response(self, request_number, handle, folder=False): @@ -212,7 +221,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): desc = SFTP_DESC[code] except IndexError: desc = 'Unknown' - # some clients expect a "langauge" tag at the end (but don't mind it being blank) + # some clients expect a "langauge" tag at the end + # (but don't mind it being blank) self._response(request_number, CMD_STATUS, code, desc, '') def _open_folder(self, request_number, path): @@ -251,7 +261,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): length = msg.get_int64() block_size = msg.get_int() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return f = self.file_table[handle] for x in alg_list: @@ -260,7 +271,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): alg = _hash_class[x] break else: - self._send_status(request_number, SFTP_FAILURE, 'No supported hash types found') + self._send_status( + request_number, SFTP_FAILURE, 'No supported hash types found') return if length == 0: st = f.stat() @@ -271,7 +283,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): if block_size == 0: block_size = length if block_size < 256: - self._send_status(request_number, SFTP_FAILURE, 'Block size too small') + self._send_status( + request_number, SFTP_FAILURE, 'Block size too small') return sum_out = bytes() @@ -285,7 +298,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): while count < blocklen: data = f.read(offset, chunklen) if not isinstance(data, bytes_types): - self._send_status(request_number, data, 'Unable to hash file') + self._send_status( + request_number, data, 'Unable to hash file') return hash_obj.update(data) count += len(data) @@ -323,7 +337,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): path = msg.get_text() flags = self._convert_pflags(msg.get_int()) attr = SFTPAttributes._from_msg(msg) - self._send_handle_response(request_number, self.server.open(path, flags, attr)) + self._send_handle_response( + request_number, self.server.open(path, flags, attr)) elif t == CMD_CLOSE: handle = msg.get_binary() if handle in self.folder_table: @@ -335,13 +350,15 @@ class SFTPServer (BaseSFTP, SubsystemHandler): del self.file_table[handle] self._send_status(request_number, SFTP_OK) return - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') elif t == CMD_READ: handle = msg.get_binary() offset = msg.get_int64() length = msg.get_int() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return data = self.file_table[handle].read(offset, length) if isinstance(data, (bytes_types, string_types)): @@ -356,16 +373,19 @@ class SFTPServer (BaseSFTP, SubsystemHandler): offset = msg.get_int64() data = msg.get_binary() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return - self._send_status(request_number, self.file_table[handle].write(offset, data)) + self._send_status( + request_number, self.file_table[handle].write(offset, data)) elif t == CMD_REMOVE: path = msg.get_text() self._send_status(request_number, self.server.remove(path)) elif t == CMD_RENAME: oldpath = msg.get_text() newpath = msg.get_text() - self._send_status(request_number, self.server.rename(oldpath, newpath)) + self._send_status( + request_number, self.server.rename(oldpath, newpath)) elif t == CMD_MKDIR: path = msg.get_text() attr = SFTPAttributes._from_msg(msg) @@ -380,7 +400,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_READDIR: handle = msg.get_binary() if handle not in self.folder_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return folder = self.folder_table[handle] self._read_folder(request_number, folder) @@ -401,7 +422,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_FSTAT: handle = msg.get_binary() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return resp = self.file_table[handle].stat() if issubclass(type(resp), SFTPAttributes): @@ -416,25 +438,31 @@ class SFTPServer (BaseSFTP, SubsystemHandler): handle = msg.get_binary() attr = SFTPAttributes._from_msg(msg) if handle not in self.file_table: - self._response(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._response( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return - self._send_status(request_number, self.file_table[handle].chattr(attr)) + self._send_status( + request_number, self.file_table[handle].chattr(attr)) elif t == CMD_READLINK: path = msg.get_text() resp = self.server.readlink(path) if isinstance(resp, (bytes_types, string_types)): - self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) + self._response( + request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) else: self._send_status(request_number, resp) elif t == CMD_SYMLINK: - # the sftp 2 draft is incorrect here! path always follows target_path + # the sftp 2 draft is incorrect here! + # path always follows target_path target_path = msg.get_text() path = msg.get_text() - self._send_status(request_number, self.server.symlink(target_path, path)) + self._send_status( + request_number, self.server.symlink(target_path, path)) elif t == CMD_REALPATH: path = msg.get_text() rpath = self.server.canonicalize(path) - self._response(request_number, CMD_NAME, 1, rpath, '', SFTPAttributes()) + self._response( + request_number, CMD_NAME, 1, rpath, '', SFTPAttributes()) elif t == CMD_EXTENDED: tag = msg.get_text() if tag == 'check-file': -- cgit v1.2.3