mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
PEP8-ify ftplib.py: get rid of multiple statements on one line (if cond: ...)
This commit is contained in:
parent
e8d4d51f59
commit
94c56a172c
1 changed files with 57 additions and 29 deletions
|
@ -184,7 +184,8 @@ class FTP:
|
||||||
# Internal: send one line to the server, appending CRLF
|
# Internal: send one line to the server, appending CRLF
|
||||||
def putline(self, line):
|
def putline(self, line):
|
||||||
line = line + CRLF
|
line = line + CRLF
|
||||||
if self.debugging > 1: print('*put*', self.sanitize(line))
|
if self.debugging > 1:
|
||||||
|
print('*put*', self.sanitize(line))
|
||||||
self.sock.sendall(line.encode(self.encoding))
|
self.sock.sendall(line.encode(self.encoding))
|
||||||
|
|
||||||
# Internal: send one command to the server (through putline())
|
# Internal: send one command to the server (through putline())
|
||||||
|
@ -198,9 +199,12 @@ class FTP:
|
||||||
line = self.file.readline()
|
line = self.file.readline()
|
||||||
if self.debugging > 1:
|
if self.debugging > 1:
|
||||||
print('*get*', self.sanitize(line))
|
print('*get*', self.sanitize(line))
|
||||||
if not line: raise EOFError
|
if not line:
|
||||||
if line[-2:] == CRLF: line = line[:-2]
|
raise EOFError
|
||||||
elif line[-1:] in CRLF: line = line[:-1]
|
if line[-2:] == CRLF:
|
||||||
|
line = line[:-2]
|
||||||
|
elif line[-1:] in CRLF:
|
||||||
|
line = line[:-1]
|
||||||
return line
|
return line
|
||||||
|
|
||||||
# Internal: get a response from the server, which may possibly
|
# Internal: get a response from the server, which may possibly
|
||||||
|
@ -223,7 +227,8 @@ class FTP:
|
||||||
# Raise various errors if the response indicates an error
|
# Raise various errors if the response indicates an error
|
||||||
def getresp(self):
|
def getresp(self):
|
||||||
resp = self.getmultiline()
|
resp = self.getmultiline()
|
||||||
if self.debugging: print('*resp*', self.sanitize(resp))
|
if self.debugging:
|
||||||
|
print('*resp*', self.sanitize(resp))
|
||||||
self.lastresp = resp[:3]
|
self.lastresp = resp[:3]
|
||||||
c = resp[:1]
|
c = resp[:1]
|
||||||
if c in {'1', '2', '3'}:
|
if c in {'1', '2', '3'}:
|
||||||
|
@ -247,7 +252,8 @@ class FTP:
|
||||||
IP and Synch; that doesn't seem to work with the servers I've
|
IP and Synch; that doesn't seem to work with the servers I've
|
||||||
tried. Instead, just send the ABOR command as OOB data.'''
|
tried. Instead, just send the ABOR command as OOB data.'''
|
||||||
line = b'ABOR' + B_CRLF
|
line = b'ABOR' + B_CRLF
|
||||||
if self.debugging > 1: print('*put urgent*', self.sanitize(line))
|
if self.debugging > 1:
|
||||||
|
print('*put urgent*', self.sanitize(line))
|
||||||
self.sock.sendall(line, MSG_OOB)
|
self.sock.sendall(line, MSG_OOB)
|
||||||
resp = self.getmultiline()
|
resp = self.getmultiline()
|
||||||
if resp[:3] not in {'426', '225', '226'}:
|
if resp[:3] not in {'426', '225', '226'}:
|
||||||
|
@ -388,9 +394,12 @@ class FTP:
|
||||||
|
|
||||||
def login(self, user = '', passwd = '', acct = ''):
|
def login(self, user = '', passwd = '', acct = ''):
|
||||||
'''Login, default anonymous.'''
|
'''Login, default anonymous.'''
|
||||||
if not user: user = 'anonymous'
|
if not user:
|
||||||
if not passwd: passwd = ''
|
user = 'anonymous'
|
||||||
if not acct: acct = ''
|
if not passwd:
|
||||||
|
passwd = ''
|
||||||
|
if not acct:
|
||||||
|
acct = ''
|
||||||
if user == 'anonymous' and passwd in {'', '-'}:
|
if user == 'anonymous' and passwd in {'', '-'}:
|
||||||
# If there is no anonymous ftp password specified
|
# If there is no anonymous ftp password specified
|
||||||
# then we'll just use anonymous@
|
# then we'll just use anonymous@
|
||||||
|
@ -401,8 +410,10 @@ class FTP:
|
||||||
# host or country.
|
# host or country.
|
||||||
passwd = passwd + 'anonymous@'
|
passwd = passwd + 'anonymous@'
|
||||||
resp = self.sendcmd('USER ' + user)
|
resp = self.sendcmd('USER ' + user)
|
||||||
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
|
if resp[0] == '3':
|
||||||
if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct)
|
resp = self.sendcmd('PASS ' + passwd)
|
||||||
|
if resp[0] == '3':
|
||||||
|
resp = self.sendcmd('ACCT ' + acct)
|
||||||
if resp[0] != '2':
|
if resp[0] != '2':
|
||||||
raise error_reply(resp)
|
raise error_reply(resp)
|
||||||
return resp
|
return resp
|
||||||
|
@ -442,13 +453,15 @@ class FTP:
|
||||||
Returns:
|
Returns:
|
||||||
The response code.
|
The response code.
|
||||||
"""
|
"""
|
||||||
if callback is None: callback = print_line
|
if callback is None:
|
||||||
|
callback = print_line
|
||||||
resp = self.sendcmd('TYPE A')
|
resp = self.sendcmd('TYPE A')
|
||||||
with self.transfercmd(cmd) as conn, \
|
with self.transfercmd(cmd) as conn, \
|
||||||
conn.makefile('r', encoding=self.encoding) as fp:
|
conn.makefile('r', encoding=self.encoding) as fp:
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if self.debugging > 2: print('*retr*', repr(line))
|
if self.debugging > 2:
|
||||||
|
print('*retr*', repr(line))
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
if line[-2:] == CRLF:
|
if line[-2:] == CRLF:
|
||||||
|
@ -477,9 +490,11 @@ class FTP:
|
||||||
with self.transfercmd(cmd, rest) as conn:
|
with self.transfercmd(cmd, rest) as conn:
|
||||||
while 1:
|
while 1:
|
||||||
buf = fp.read(blocksize)
|
buf = fp.read(blocksize)
|
||||||
if not buf: break
|
if not buf:
|
||||||
|
break
|
||||||
conn.sendall(buf)
|
conn.sendall(buf)
|
||||||
if callback: callback(buf)
|
if callback:
|
||||||
|
callback(buf)
|
||||||
return self.voidresp()
|
return self.voidresp()
|
||||||
|
|
||||||
def storlines(self, cmd, fp, callback=None):
|
def storlines(self, cmd, fp, callback=None):
|
||||||
|
@ -498,12 +513,14 @@ class FTP:
|
||||||
with self.transfercmd(cmd) as conn:
|
with self.transfercmd(cmd) as conn:
|
||||||
while 1:
|
while 1:
|
||||||
buf = fp.readline()
|
buf = fp.readline()
|
||||||
if not buf: break
|
if not buf:
|
||||||
|
break
|
||||||
if buf[-2:] != B_CRLF:
|
if buf[-2:] != B_CRLF:
|
||||||
if buf[-1] in B_CRLF: buf = buf[:-1]
|
if buf[-1] in B_CRLF: buf = buf[:-1]
|
||||||
buf = buf + B_CRLF
|
buf = buf + B_CRLF
|
||||||
conn.sendall(buf)
|
conn.sendall(buf)
|
||||||
if callback: callback(buf)
|
if callback:
|
||||||
|
callback(buf)
|
||||||
return self.voidresp()
|
return self.voidresp()
|
||||||
|
|
||||||
def acct(self, password):
|
def acct(self, password):
|
||||||
|
@ -768,14 +785,16 @@ else:
|
||||||
return self.voidresp()
|
return self.voidresp()
|
||||||
|
|
||||||
def retrlines(self, cmd, callback = None):
|
def retrlines(self, cmd, callback = None):
|
||||||
if callback is None: callback = print_line
|
if callback is None:
|
||||||
|
callback = print_line
|
||||||
resp = self.sendcmd('TYPE A')
|
resp = self.sendcmd('TYPE A')
|
||||||
conn = self.transfercmd(cmd)
|
conn = self.transfercmd(cmd)
|
||||||
fp = conn.makefile('r', encoding=self.encoding)
|
fp = conn.makefile('r', encoding=self.encoding)
|
||||||
with fp, conn:
|
with fp, conn:
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if self.debugging > 2: print('*retr*', repr(line))
|
if self.debugging > 2:
|
||||||
|
print('*retr*', repr(line))
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
if line[-2:] == CRLF:
|
if line[-2:] == CRLF:
|
||||||
|
@ -793,9 +812,11 @@ else:
|
||||||
with self.transfercmd(cmd, rest) as conn:
|
with self.transfercmd(cmd, rest) as conn:
|
||||||
while 1:
|
while 1:
|
||||||
buf = fp.read(blocksize)
|
buf = fp.read(blocksize)
|
||||||
if not buf: break
|
if not buf:
|
||||||
|
break
|
||||||
conn.sendall(buf)
|
conn.sendall(buf)
|
||||||
if callback: callback(buf)
|
if callback:
|
||||||
|
callback(buf)
|
||||||
# shutdown ssl layer
|
# shutdown ssl layer
|
||||||
if isinstance(conn, ssl.SSLSocket):
|
if isinstance(conn, ssl.SSLSocket):
|
||||||
conn.unwrap()
|
conn.unwrap()
|
||||||
|
@ -806,12 +827,15 @@ else:
|
||||||
with self.transfercmd(cmd) as conn:
|
with self.transfercmd(cmd) as conn:
|
||||||
while 1:
|
while 1:
|
||||||
buf = fp.readline()
|
buf = fp.readline()
|
||||||
if not buf: break
|
if not buf:
|
||||||
|
break
|
||||||
if buf[-2:] != B_CRLF:
|
if buf[-2:] != B_CRLF:
|
||||||
if buf[-1] in B_CRLF: buf = buf[:-1]
|
if buf[-1] in B_CRLF:
|
||||||
|
buf = buf[:-1]
|
||||||
buf = buf + B_CRLF
|
buf = buf + B_CRLF
|
||||||
conn.sendall(buf)
|
conn.sendall(buf)
|
||||||
if callback: callback(buf)
|
if callback:
|
||||||
|
callback(buf)
|
||||||
# shutdown ssl layer
|
# shutdown ssl layer
|
||||||
if isinstance(conn, ssl.SSLSocket):
|
if isinstance(conn, ssl.SSLSocket):
|
||||||
conn.unwrap()
|
conn.unwrap()
|
||||||
|
@ -924,7 +948,8 @@ def print_line(line):
|
||||||
|
|
||||||
def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
|
def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
|
||||||
'''Copy file from one FTP-instance to another.'''
|
'''Copy file from one FTP-instance to another.'''
|
||||||
if not targetname: targetname = sourcename
|
if not targetname:
|
||||||
|
targetname = sourcename
|
||||||
type = 'TYPE ' + type
|
type = 'TYPE ' + type
|
||||||
source.voidcmd(type)
|
source.voidcmd(type)
|
||||||
target.voidcmd(type)
|
target.voidcmd(type)
|
||||||
|
@ -934,9 +959,11 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
|
||||||
# transfer request.
|
# transfer request.
|
||||||
# So: STOR before RETR, because here the target is a "user".
|
# So: STOR before RETR, because here the target is a "user".
|
||||||
treply = target.sendcmd('STOR ' + targetname)
|
treply = target.sendcmd('STOR ' + targetname)
|
||||||
if treply[:3] not in {'125', '150'}: raise error_proto # RFC 959
|
if treply[:3] not in {'125', '150'}:
|
||||||
|
raise error_proto # RFC 959
|
||||||
sreply = source.sendcmd('RETR ' + sourcename)
|
sreply = source.sendcmd('RETR ' + sourcename)
|
||||||
if sreply[:3] not in {'125', '150'}: raise error_proto # RFC 959
|
if sreply[:3] not in {'125', '150'}:
|
||||||
|
raise error_proto # RFC 959
|
||||||
source.voidresp()
|
source.voidresp()
|
||||||
target.voidresp()
|
target.voidresp()
|
||||||
|
|
||||||
|
@ -968,7 +995,8 @@ class Netrc:
|
||||||
in_macro = 0
|
in_macro = 0
|
||||||
while 1:
|
while 1:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line: break
|
if not line:
|
||||||
|
break
|
||||||
if in_macro and line.strip():
|
if in_macro and line.strip():
|
||||||
macro_lines.append(line)
|
macro_lines.append(line)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue