Skip to content

Commit 7b2f85a

Browse files
Merge branch 'master' into accessor-pylint-remains
2 parents 0105da3 + 25bb3f4 commit 7b2f85a

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

tests/test_accessor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ def test_filesystem_accessor_access(self):
4141

4242
a = xcp.accessor.FilesystemAccessor("tests/data/repo/", True)
4343
self.check_repo_access(a)
44+
45+
46+
def test_access_handles_exception():
47+
class AccessorHandlesException(xcp.accessor.Accessor):
48+
def openAddress(self, address):
49+
raise IOError("Test Accessor.access returning False on Exception to cove code")
50+
51+
assert AccessorHandlesException(True).access("filename") is False

tests/test_ftpaccessor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ def test_download_binary_file(ftp_accessor):
7171
"""Download a binary file and compare the returned file content"""
7272
remote_ftp_filehandle = ftp_accessor.openAddress("filename")
7373
assert remote_ftp_filehandle.read() == binary_data
74+
assert ftp_accessor.access("filename") is True # covers FTPAccessor._cleanup()
7475
ftp_accessor.finish()

xcp/accessor.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
import os
3030
import tempfile
3131
import errno
32+
from typing import cast, TYPE_CHECKING
3233

3334
from six.moves import urllib # pyright: ignore
3435

35-
import xcp.mount as mount
36-
import xcp.logger as logger
36+
from xcp import logger, mount
37+
38+
if TYPE_CHECKING:
39+
from typing import IO
40+
from typing_extensions import Literal
3741

3842
# maps errno codes to HTTP error codes
3943
# needed for error code consistency
@@ -60,15 +64,16 @@ def access(self, name):
6064
f = self.openAddress(name)
6165
if not f:
6266
return False
63-
f.close()
64-
except Exception as e:
67+
f.close() # pylint: disable=no-member
68+
except Exception:
6569
return False
6670

6771
return True
6872

6973
def openAddress(self, address):
70-
"""should be overloaded"""
71-
pass
74+
# type:(str) -> IO[bytes] | Literal[False]
75+
"""must be overloaded"""
76+
return False # pragma: no cover
7277

7378
def canEject(self):
7479
return False
@@ -97,13 +102,7 @@ def __init__(self, location, ro):
97102
def openAddress(self, address):
98103
try:
99104
filehandle = open(os.path.join(self.location, address), "rb")
100-
except OSError as e:
101-
if e.errno == errno.EIO:
102-
self.lastError = 5
103-
else:
104-
self.lastError = mapError(e.errno)
105-
return False
106-
except IOError as e:
105+
except (IOError, OSError) as e:
107106
if e.errno == errno.EIO:
108107
self.lastError = 5
109108
else:
@@ -218,13 +217,7 @@ def __init__(self, baseAddress, ro):
218217
def openAddress(self, address):
219218
try:
220219
reader = open(os.path.join(self.baseAddress, address), "rb")
221-
except IOError as e:
222-
if e.errno == errno.EIO:
223-
self.lastError = 5
224-
else:
225-
self.lastError = mapError(e.errno)
226-
return False
227-
except OSError as e:
220+
except (IOError, OSError) as e:
228221
if e.errno == errno.EIO:
229222
self.lastError = 5
230223
else:
@@ -265,7 +258,7 @@ def __init__(self, baseAddress, ro):
265258
def _cleanup(self):
266259
if self.cleanup:
267260
# clean up after RETR
268-
self.ftp.voidresp()
261+
cast(ftplib.FTP, self.ftp).voidresp()
269262
self.cleanup = False
270263

271264
def start(self):
@@ -275,9 +268,9 @@ def start(self):
275268
port = ftplib.FTP_PORT
276269
if self.url_parts.port:
277270
port = self.url_parts.port
278-
self.ftp.connect(self.url_parts.hostname, port)
279-
username = self.url_parts.username
280-
password = self.url_parts.password
271+
self.ftp.connect(cast(str, self.url_parts.hostname), port)
272+
username = cast(str, self.url_parts.username)
273+
password = cast(str, self.url_parts.password)
281274
if username:
282275
username = urllib.parse.unquote(username)
283276
if password:
@@ -296,7 +289,7 @@ def finish(self):
296289
return
297290
self.start_count -= 1
298291
if self.start_count == 0:
299-
self.ftp.quit()
292+
cast(ftplib.FTP, self.ftp).quit()
300293
self.cleanup = False
301294
self.ftp = None
302295

@@ -306,17 +299,12 @@ def access(self, path): # pylint: disable=arguments-differ,arguments-renamed
306299
self._cleanup()
307300
url = urllib.parse.unquote(path)
308301

302+
assert self.ftp
309303
if self.ftp.size(url) is not None:
310304
return True
311305
lst = self.ftp.nlst(os.path.dirname(url))
312306
return os.path.basename(url) in list(map(os.path.basename, lst))
313-
except IOError as e:
314-
if e.errno == errno.EIO:
315-
self.lastError = 5
316-
else:
317-
self.lastError = mapError(e.errno)
318-
return False
319-
except OSError as e:
307+
except (IOError, OSError) as e:
320308
if e.errno == errno.EIO:
321309
self.lastError = 5
322310
else:
@@ -331,6 +319,7 @@ def openAddress(self, address):
331319
self._cleanup()
332320
url = urllib.parse.unquote(address)
333321

322+
assert self.ftp
334323
self.ftp.voidcmd('TYPE I')
335324
socket = self.ftp.transfercmd('RETR ' + url)
336325
buffered_reader = socket.makefile('rb')
@@ -344,7 +333,7 @@ def writeFile(self, in_fh, out_name):
344333
fname = urllib.parse.unquote(out_name)
345334

346335
logger.debug("Storing as " + fname)
347-
self.ftp.storbinary('STOR ' + fname, in_fh)
336+
cast(ftplib.FTP, self.ftp).storbinary('STOR ' + fname, in_fh)
348337

349338
def __repr__(self):
350339
return "<FTPAccessor: %s>" % self.baseAddress

0 commit comments

Comments
 (0)