Skip to content

Commit 28be2cd

Browse files
xcp.accessor: Add the option to pass kwargs like encoding and errors for de/encoding
1 parent ce3cb86 commit 28be2cd

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

xcp/accessor.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ 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, **kwargs):
103103
try:
104-
filehandle = open(os.path.join(self.location, address), 'rb')
104+
kwargs["mode"] = "r" if "encoding" in kwargs else "rb"
105+
filehandle = open(os.path.join(self.location, address), **kwargs)
105106
except OSError as e:
106107
if e.errno == errno.EIO:
107108
self.lastError = 5
@@ -165,9 +166,10 @@ def finish(self):
165166
os.rmdir(self.location)
166167
self.location = None
167168

168-
def writeFile(self, in_fh, out_name):
169+
def writeFile(self, in_fh, out_name, **kwargs):
169170
logger.info("Copying to %s" % os.path.join(self.location, out_name))
170-
out_fh = open(os.path.join(self.location, out_name), "wb")
171+
kwargs["mode"] = "w" if "encoding" in kwargs else "wb"
172+
out_fh = open(os.path.join(self.location, out_name), **kwargs)
171173
return self._writeFile(in_fh, out_fh)
172174

173175
def __del__(self):
@@ -220,9 +222,10 @@ def __init__(self, baseAddress, ro):
220222
super(FileAccessor, self).__init__(ro)
221223
self.baseAddress = baseAddress
222224

223-
def openAddress(self, address):
225+
def openAddress(self, address, **kwargs):
224226
try:
225-
file = open(os.path.join(self.baseAddress, address), "rb")
227+
kwargs["mode"] = "r" if "encoding" in kwargs else "rb"
228+
file = open(os.path.join(self.baseAddress, address), **kwargs)
226229
except IOError as e:
227230
if e.errno == errno.EIO:
228231
self.lastError = 5
@@ -240,9 +243,10 @@ def openAddress(self, address):
240243
return False
241244
return file
242245

243-
def writeFile(self, in_fh, out_name):
246+
def writeFile(self, in_fh, out_name, **kwargs):
244247
logger.info("Copying to %s" % os.path.join(self.baseAddress, out_name))
245-
out_fh = open(os.path.join(self.baseAddress, out_name), "wb" )
248+
kwargs["mode"] = "w" if "encoding" in kwargs else "wb"
249+
out_fh = open(os.path.join(self.baseAddress, out_name), **kwargs)
246250
return self._writeFile(in_fh, out_fh)
247251

248252
def __repr__(self):
@@ -331,13 +335,14 @@ def access(self, path):
331335
self.lastError = 500
332336
return False
333337

334-
def openAddress(self, address):
338+
def openAddress(self, address, **kwargs):
335339
logger.debug("Opening "+address)
336340
self._cleanup()
337341
url = urllib.parse.unquote(address)
338342

339343
self.ftp.voidcmd('TYPE I')
340-
s = self.ftp.transfercmd('RETR ' + url).makefile('rb')
344+
kwargs["mode"] = "r" if "encoding" in kwargs else "rb"
345+
s = self.ftp.transfercmd('RETR ' + url).makefile(**kwargs)
341346
self.cleanup = True
342347
return s
343348

0 commit comments

Comments
 (0)