diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2017-10-11 18:23:55 -0700 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2017-10-11 18:23:55 -0700 |
commit | fe9dd1b326464d162141e042d939ba56cfde18d1 (patch) | |
tree | 6752ef97c0699982493b2864cce7b12d9b8c2ec2 | |
parent | 838748dcb61af0a469230690aa369dd1ae8895ab (diff) |
More string format updates
-rw-r--r-- | paramiko/sftp_file.py | 12 | ||||
-rw-r--r-- | paramiko/sftp_server.py | 6 | ||||
-rw-r--r-- | paramiko/ssh_exception.py | 2 | ||||
-rw-r--r-- | paramiko/transport.py | 86 | ||||
-rw-r--r-- | paramiko/util.py | 11 | ||||
-rw-r--r-- | sites/shared_conf.py | 2 |
6 files changed, 61 insertions, 58 deletions
diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 028d894a..be7e6eac 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -83,7 +83,7 @@ class SFTPFile (BufferedFile): # __del__.) if self._closed: return - self.sftp._log(DEBUG, 'close(%s)' % u(hexlify(self.handle))) + self.sftp._log(DEBUG, 'close({})'.format(u(hexlify(self.handle)))) if self.pipelined: self.sftp._finish_responses(self) BufferedFile.close(self) @@ -288,7 +288,7 @@ class SFTPFile (BufferedFile): :param int mode: new permissions """ - self.sftp._log(DEBUG, 'chmod(%s, %r)' % (hexlify(self.handle), mode)) + self.sftp._log(DEBUG, 'chmod({}, {!r})'.format(hexlify(self.handle), mode)) attr = SFTPAttributes() attr.st_mode = mode self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -305,7 +305,7 @@ class SFTPFile (BufferedFile): """ self.sftp._log( DEBUG, - 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid)) + 'chown({}, {!r}, {!r})'.format(hexlify(self.handle), uid, gid)) attr = SFTPAttributes() attr.st_uid, attr.st_gid = uid, gid self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -325,7 +325,7 @@ class SFTPFile (BufferedFile): """ if times is None: times = (time.time(), time.time()) - self.sftp._log(DEBUG, 'utime(%s, %r)' % (hexlify(self.handle), times)) + self.sftp._log(DEBUG, 'utime({}, {!r})'.format(hexlify(self.handle), times)) attr = SFTPAttributes() attr.st_atime, attr.st_mtime = times self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -340,7 +340,7 @@ class SFTPFile (BufferedFile): """ self.sftp._log( DEBUG, - 'truncate(%s, %r)' % (hexlify(self.handle), size)) + 'truncate({}, {!r})'.format(hexlify(self.handle), size)) attr = SFTPAttributes() attr.st_size = size self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -473,7 +473,7 @@ class SFTPFile (BufferedFile): .. versionadded:: 1.5.4 """ - self.sftp._log(DEBUG, 'readv(%s, %r)' % (hexlify(self.handle), chunks)) + self.sftp._log(DEBUG, 'readv({}, {!r})'.format(hexlify(self.handle), chunks)) read_chunks = [] for offset, size in chunks: diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 103081dd..a6f43217 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -100,7 +100,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def start_subsystem(self, name, transport, channel): self.sock = channel - self._log(DEBUG, 'Started sftp server on channel %s' % repr(channel)) + self._log(DEBUG, 'Started sftp server on channel {}'.format(repr(channel))) self._send_server_version() self.server.session_started() while True: @@ -209,7 +209,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): # must be error code self._send_status(request_number, handle) return - handle._set_name(b('hx%d' % self.next_handle)) + handle._set_name(b('hx{:d}'.format(self.next_handle))) self.next_handle += 1 if folder: self.folder_table[handle._get_name()] = handle @@ -334,7 +334,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): return flags def _process(self, t, request_number, msg): - self._log(DEBUG, 'Request: %s' % CMD_NAMES[t]) + self._log(DEBUG, 'Request: {}'.format(CMD_NAMES[t])) if t == CMD_OPEN: path = msg.get_text() flags = self._convert_pflags(msg.get_int()) diff --git a/paramiko/ssh_exception.py b/paramiko/ssh_exception.py index f8405a31..2df84b65 100644 --- a/paramiko/ssh_exception.py +++ b/paramiko/ssh_exception.py @@ -128,7 +128,7 @@ class ProxyCommandFailure (SSHException): """ def __init__(self, command, error): SSHException.__init__(self, - '"ProxyCommand (%s)" returned non-zero exit status: %s' % ( + '"ProxyCommand ({})" returned non-zero exit status: {}'.format( command, error ) ) diff --git a/paramiko/transport.py b/paramiko/transport.py index 544ecf0f..f5178885 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -100,7 +100,7 @@ class Transport(threading.Thread, ClosingContextManager): _DECRYPT = object() _PROTO_ID = '2.0' - _CLIENT_ID = 'paramiko_%s' % paramiko.__version__ + _CLIENT_ID = 'paramiko_{}'.format(paramiko.__version__) # These tuples of algorithm identifiers are in preference order; do not # reorder without reason! @@ -329,7 +329,7 @@ class Transport(threading.Thread, ClosingContextManager): break else: raise SSHException( - 'Unable to connect to %s: %s' % (hostname, reason)) + 'Unable to connect to {}: {}'.format(hostname, reason)) # okay, normal socket-ish flow here... threading.Thread.__init__(self) self.setDaemon(True) @@ -415,17 +415,17 @@ class Transport(threading.Thread, ClosingContextManager): """ Returns a string representation of this object, for debugging. """ - out = '<paramiko.Transport at %s' % hex(long(id(self)) & xffffffff) + out = '<paramiko.Transport at {}'.format(hex(long(id(self)) & xffffffff)) if not self.active: out += ' (unconnected)' else: if self.local_cipher != '': - out += ' (cipher %s, %d bits)' % ( + out += ' (cipher {}, {:d} bits)'.format( self.local_cipher, self._cipher_info[self.local_cipher]['key-size'] * 8 ) if self.is_authenticated(): - out += ' (active; %d open channel(s))' % len(self._channels) + out += ' (active; {:d} open channel(s))'.format(len(self._channels)) elif self.initial_kex_done: out += ' (connected; awaiting auth)' else: @@ -1061,7 +1061,7 @@ class Transport(threading.Thread, ClosingContextManager): m.add_boolean(wait) if data is not None: m.add(*data) - self._log(DEBUG, 'Sending global request "%s"' % kind) + self._log(DEBUG, 'Sending global request "{}"'.format(kind)) self._send_user_message(m) if not wait: return None @@ -1179,14 +1179,14 @@ class Transport(threading.Thread, ClosingContextManager): key.asbytes() != hostkey.asbytes() ): self._log(DEBUG, 'Bad host key from server') - self._log(DEBUG, 'Expected: %s: %s' % ( - hostkey.get_name(), repr(hostkey.asbytes())) - ) - self._log(DEBUG, 'Got : %s: %s' % ( - key.get_name(), repr(key.asbytes())) - ) + self._log(DEBUG, 'Expected: {}: {}'.format( + hostkey.get_name(), repr(hostkey.asbytes()), + )) + self._log(DEBUG, 'Got : {}: {}'.format( + key.get_name(), repr(key.asbytes()), + )) raise SSHException('Bad host key from server') - self._log(DEBUG, 'Host key verified (%s)' % hostkey.get_name()) + self._log(DEBUG, 'Host key verified ({})'.format(hostkey.get_name())) if (pkey is not None) or (password is not None) or gss_auth or gss_kex: if gss_auth: @@ -1746,7 +1746,7 @@ class Transport(threading.Thread, ClosingContextManager): if key is None: raise SSHException('Unknown host key type') if not key.verify_ssh_sig(self.H, Message(sig)): - raise SSHException('Signature verification (%s) failed.' % self.host_key_type) # noqa + raise SSHException('Signature verification ({}) failed.'.format(self.host_key_type)) # noqa self.host_key = key def _compute_key(self, id, nbytes): @@ -1759,8 +1759,8 @@ class Transport(threading.Thread, ClosingContextManager): # Fallback to SHA1 for kex engines that fail to specify a hex # algorithm, or for e.g. transport tests that don't run kexinit. hash_algo = getattr(self.kex_engine, 'hash_algo', None) - hash_select_msg = "kex engine %s specified hash_algo %r" % ( - self.kex_engine.__class__.__name__, hash_algo + hash_select_msg = "kex engine {} specified hash_algo {!r}".format( + self.kex_engine.__class__.__name__, hash_algo, ) if hash_algo is None: hash_algo = sha1 @@ -1844,13 +1844,13 @@ class Transport(threading.Thread, ClosingContextManager): _active_threads.append(self) tid = hex(long(id(self)) & xffffffff) if self.server_mode: - self._log(DEBUG, 'starting thread (server mode): %s' % tid) + self._log(DEBUG, 'starting thread (server mode): {}'.format(tid)) else: - self._log(DEBUG, 'starting thread (client mode): %s' % tid) + self._log(DEBUG, 'starting thread (client mode): {}'.format(tid)) try: try: self.packetizer.write_all(b(self.local_version + '\r\n')) - self._log(DEBUG, 'Local version/idstring: %s' % self.local_version) # noqa + self._log(DEBUG, 'Local version/idstring: {}'.format(self.local_version)) # noqa self._check_banner() # The above is actually very much part of the handshake, but # sometimes the banner can be read but the machine is not @@ -1880,7 +1880,7 @@ class Transport(threading.Thread, ClosingContextManager): continue if len(self._expected_packet) > 0: if ptype not in self._expected_packet: - raise SSHException('Expecting packet from %r, got %d' % (self._expected_packet, ptype)) # noqa + raise SSHException('Expecting packet from {!r}, got {:d}'.format(self._expected_packet, ptype)) # noqa self._expected_packet = tuple() if (ptype >= 30) and (ptype <= 41): self.kex_engine.parse_next(ptype, m) @@ -1894,9 +1894,9 @@ class Transport(threading.Thread, ClosingContextManager): if chan is not None: self._channel_handler_table[ptype](chan, m) elif chanid in self.channels_seen: - self._log(DEBUG, 'Ignoring message for dead channel %d' % chanid) # noqa + self._log(DEBUG, 'Ignoring message for dead channel {:d}'.format(chanid)) # noqa else: - self._log(ERROR, 'Channel request for unknown channel %d' % chanid) # noqa + self._log(ERROR, 'Channel request for unknown channel {:d}'.format(chanid)) # noqa break elif ( self.auth_handler is not None and @@ -1907,7 +1907,7 @@ class Transport(threading.Thread, ClosingContextManager): if len(self._expected_packet) > 0: continue else: - self._log(WARNING, 'Oops, unhandled type %d' % ptype) + self._log(WARNING, 'Oops, unhandled type {:d}'.format(ptype)) msg = Message() msg.add_byte(cMSG_UNIMPLEMENTED) msg.add_int(m.seqno) @@ -1923,7 +1923,7 @@ class Transport(threading.Thread, ClosingContextManager): except socket.error as e: if type(e.args) is tuple: if e.args: - emsg = '%s (%d)' % (e.args[1], e.args[0]) + emsg = '{} ({:d})'.format(e.args[1], e.args[0]) else: # empty tuple, e.g. socket.timeout emsg = str(e) or repr(e) else: @@ -2011,7 +2011,7 @@ class Transport(threading.Thread, ClosingContextManager): raise SSHException('Indecipherable protocol version "' + buf + '"') # save this server version string for later self.remote_version = buf - self._log(DEBUG, 'Remote version/idstring: %s' % buf) + self._log(DEBUG, 'Remote version/idstring: {}'.format(buf)) # pull off any attached comment # NOTE: comment used to be stored in a variable and then...never used. # since 2003. ca 877cd974b8182d26fa76d566072917ea67b64e67 @@ -2130,7 +2130,7 @@ class Transport(threading.Thread, ClosingContextManager): if len(agreed_kex) == 0: raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)') # noqa self.kex_engine = self._kex_info[agreed_kex[0]](self) - self._log(DEBUG, "Kex agreed: %s" % agreed_kex[0]) + self._log(DEBUG, "Kex agreed: {}".format(agreed_kex[0])) if self.server_mode: available_server_keys = list(filter( @@ -2366,16 +2366,16 @@ class Transport(threading.Thread, ClosingContextManager): def _parse_disconnect(self, m): code = m.get_int() desc = m.get_text() - self._log(INFO, 'Disconnect (code %d): %s' % (code, desc)) + self._log(INFO, 'Disconnect (code {:d}): {}'.format(code, desc)) def _parse_global_request(self, m): kind = m.get_text() - self._log(DEBUG, 'Received global request "%s"' % kind) + self._log(DEBUG, 'Received global request "{}"'.format(kind)) want_reply = m.get_boolean() if not self.server_mode: self._log( DEBUG, - 'Rejecting "%s" global request from server.' % kind + 'Rejecting "{}" global request from server.'.format(kind) ) ok = False elif kind == 'tcpip-forward': @@ -2429,7 +2429,7 @@ class Transport(threading.Thread, ClosingContextManager): try: chan._set_remote_channel( server_chanid, server_window_size, server_max_packet_size) - self._log(DEBUG, 'Secsh channel %d opened.' % chanid) + self._log(DEBUG, 'Secsh channel {:d} opened.'.format(chanid)) if chanid in self.channel_events: self.channel_events[chanid].set() del self.channel_events[chanid] @@ -2445,9 +2445,9 @@ class Transport(threading.Thread, ClosingContextManager): reason_text = CONNECTION_FAILED_CODE.get(reason, '(unknown code)') self._log( ERROR, - 'Secsh channel %d open FAILED: %s: %s' % ( - chanid, reason_str, reason_text) - ) + 'Secsh channel {:d} open FAILED: {}: {}'.format( + chanid, reason_str, reason_text, + )) self.lock.acquire() try: self.saved_exception = ChannelException(reason, reason_text) @@ -2481,9 +2481,9 @@ class Transport(threading.Thread, ClosingContextManager): origin_port = m.get_int() self._log( DEBUG, - 'Incoming x11 connection from %s:%d' % ( - origin_addr, origin_port) - ) + 'Incoming x11 connection from {}:{:d}'.format( + origin_addr, origin_port, + )) self.lock.acquire() try: my_chanid = self._next_channel() @@ -2496,9 +2496,9 @@ class Transport(threading.Thread, ClosingContextManager): origin_port = m.get_int() self._log( DEBUG, - 'Incoming tcp forwarded connection from %s:%d' % ( - origin_addr, origin_port) - ) + 'Incoming tcp forwarded connection from {}:{:d}'.format( + origin_addr, origin_port, + )) self.lock.acquire() try: my_chanid = self._next_channel() @@ -2507,7 +2507,7 @@ class Transport(threading.Thread, ClosingContextManager): elif not self.server_mode: self._log( DEBUG, - 'Rejecting "%s" channel request from server.' % kind) + 'Rejecting "{}" channel request from server.'.format(kind)) reject = True reason = OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED else: @@ -2533,7 +2533,7 @@ class Transport(threading.Thread, ClosingContextManager): if reason != OPEN_SUCCEEDED: self._log( DEBUG, - 'Rejecting "%s" channel request from client.' % kind) + 'Rejecting "{}" channel request from client.'.format(kind)) reject = True if reject: msg = Message() @@ -2564,7 +2564,7 @@ class Transport(threading.Thread, ClosingContextManager): m.add_int(self.default_window_size) m.add_int(self.default_max_packet_size) self._send_message(m) - self._log(DEBUG, 'Secsh channel %d (%s) opened.', my_chanid, kind) + self._log(DEBUG, 'Secsh channel {:d} ({}) opened.', my_chanid, kind) if kind == 'auth-agent@openssh.com': self._forward_agent_handler(chan) elif kind == 'x11': @@ -2638,7 +2638,7 @@ class SecurityOptions (object): """ Returns a string representation of this object, for debugging. """ - return '<paramiko.SecurityOptions for %s>' % repr(self._transport) + return '<paramiko.SecurityOptions for {}>'.format(repr(self._transport)) def _set(self, name, orig, x): if type(x) is list: diff --git a/paramiko/util.py b/paramiko/util.py index de099c0c..b056c20a 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -102,9 +102,12 @@ def format_binary(data, prefix=''): def format_binary_line(data): - left = ' '.join(['%02X' % byte_ord(c) for c in data]) - right = ''.join([('.%c..' % c)[(byte_ord(c) + 63) // 95] for c in data]) - return '%-50s %s' % (left, right) + left = ' '.join(['{:02X}'.format(byte_ord(c)) for c in data]) + right = ''.join([ + '.{:c}..'.format(byte_ord(c))[(byte_ord(c) + 63) // 95] + for c in data + ]) + return '{:50s} {}'.format(left, right) def safe_string(s): @@ -114,7 +117,7 @@ def safe_string(s): if 32 <= i <= 127: out += byte_chr(i) else: - out += b('%%%02X' % i) + out += b('%{:02X}'.format(i)) return out diff --git a/sites/shared_conf.py b/sites/shared_conf.py index adc89aa1..cf0d77ff 100644 --- a/sites/shared_conf.py +++ b/sites/shared_conf.py @@ -32,7 +32,7 @@ intersphinx_mapping = { # Regular settings project = 'Paramiko' year = datetime.now().year -copyright = '%d Jeff Forcier' % year +copyright = '{} Jeff Forcier'.format(year) master_doc = 'index' templates_path = ['_templates'] exclude_trees = ['_build'] |