Skip to content

Commit 78f4397

Browse files
accessor: Add mode and kwargs parameters and default to binary mode
Fix issue #19 based on the description and progress from PR xenserver#24. Allows for opening text and binary files in text and binary modes. Mode, encoding and error handling can be set by passing the parameters "encoding" and "errors" using the kwargs parameters from openAddress() and writeFile() to open(mode, **kwargs) and ftp.makefile(mode, **kwargs). Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
1 parent dec8e9d commit 78f4397

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

xcp/accessor.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def access(self, name):
7171

7272
return True
7373

74-
def openAddress(self, address):
74+
def openAddress(self, address, mode="", **kwargs):
7575
"""should be overloaded"""
7676
pass
7777

@@ -99,9 +99,9 @@ def __init__(self, location, ro):
9999
super(FilesystemAccessor, self).__init__(ro)
100100
self.location = location
101101

102-
def openAddress(self, address):
102+
def openAddress(self, address, mode="rb", **kwargs):
103103
try:
104-
filehandle = open(os.path.join(self.location, address), 'r')
104+
filehandle = open(os.path.join(self.location, address), mode, **kwargs)
105105
except OSError as e:
106106
if e.errno == errno.EIO:
107107
self.lastError = 5
@@ -165,9 +165,9 @@ def finish(self):
165165
os.rmdir(self.location)
166166
self.location = None
167167

168-
def writeFile(self, in_fh, out_name):
168+
def writeFile(self, in_fh, out_name, mode="wb", **kwargs):
169169
logger.info("Copying to %s" % os.path.join(self.location, out_name))
170-
out_fh = open(os.path.join(self.location, out_name), 'w')
170+
out_fh = open(os.path.join(self.location, out_name), mode, **kwargs)
171171
return self._writeFile(in_fh, out_fh)
172172

173173
def __del__(self):
@@ -220,9 +220,9 @@ def __init__(self, baseAddress, ro):
220220
super(FileAccessor, self).__init__(ro)
221221
self.baseAddress = baseAddress
222222

223-
def openAddress(self, address):
223+
def openAddress(self, address, mode="rb", **kwargs):
224224
try:
225-
file = open(os.path.join(self.baseAddress, address))
225+
file = open(os.path.join(self.baseAddress, address), mode, **kwargs)
226226
except IOError as e:
227227
if e.errno == errno.EIO:
228228
self.lastError = 5
@@ -240,9 +240,9 @@ def openAddress(self, address):
240240
return False
241241
return file
242242

243-
def writeFile(self, in_fh, out_name):
243+
def writeFile(self, in_fh, out_name, mode="wb", **kwargs):
244244
logger.info("Copying to %s" % os.path.join(self.baseAddress, out_name))
245-
out_fh = open(os.path.join(self.baseAddress, out_name), 'w')
245+
out_fh = open(os.path.join(self.baseAddress, out_name), mode, **kwargs)
246246
return self._writeFile(in_fh, out_fh)
247247

248248
def __repr__(self):
@@ -331,13 +331,13 @@ def access(self, path):
331331
self.lastError = 500
332332
return False
333333

334-
def openAddress(self, address):
334+
def openAddress(self, address, mode="rb", **kwargs):
335335
logger.debug("Opening "+address)
336336
self._cleanup()
337337
url = urllib.parse.unquote(address)
338338

339339
self.ftp.voidcmd('TYPE I')
340-
s = self.ftp.transfercmd('RETR ' + url).makefile('rb')
340+
s = self.ftp.transfercmd('RETR ' + url).makefile(mode, **kwargs)
341341
self.cleanup = True
342342
return s
343343

0 commit comments

Comments
 (0)