Skip to content

Commit 0513895

Browse files
author
Martin Larralde
committed
Fix Python2 not accepting unicode paths
1 parent a1d5929 commit 0513895

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

webdavfs/webdavfs.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _create_resource(self, path):
167167
return res
168168

169169
def get_resource(self, path):
170-
return self._create_resource(path)
170+
return self._create_resource(path.encode('utf-8'))
171171

172172
@staticmethod
173173
def _create_info_dict(info):
@@ -177,19 +177,26 @@ def _create_info_dict(info):
177177
'access': {}
178178
}
179179

180+
if six.PY2:
181+
def decode(s):
182+
return s.decode('utf-8') if isinstance(s, bytes) else s
183+
else:
184+
def decode(s):
185+
return s
186+
180187
for key, val in six.iteritems(info):
181188
if key in basics:
182-
info_dict['basic'][key] = six.u(val)
189+
info_dict['basic'][key] = decode(val)
183190
elif key in details:
184191
if key == 'size' and val:
185192
val = int(val)
186193
elif val:
187-
val = six.u(val)
188-
info_dict['details'][key] = val
194+
val = decode(val)
195+
info_dict['details'][key] = decode(val)
189196
elif key in access:
190-
info_dict['access'][key] = six.u(val)
197+
info_dict['access'][key] = decode(val)
191198
else:
192-
info_dict['other'][key] = six.u(val)
199+
info_dict['other'][key] = decode(val)
193200

194201
return info_dict
195202

@@ -203,7 +210,7 @@ def create(self, path, wipe=False):
203210

204211
def exists(self, path):
205212
_path = self.validatepath(path)
206-
return self.client.check(_path)
213+
return self.client.check(_path.encode('utf-8'))
207214

208215
def getinfo(self, path, namespaces=None):
209216
_path = self.validatepath(path)
@@ -222,9 +229,9 @@ def getinfo(self, path, namespaces=None):
222229

223230
else:
224231
try:
225-
info = self.client.info(_path)
232+
info = self.client.info(_path.encode('utf-8'))
226233
info_dict = self._create_info_dict(info)
227-
if self.client.is_dir(_path):
234+
if self.client.is_dir(_path.encode('utf-8')):
228235
info_dict['basic']['is_dir'] = True
229236
info_dict['details']['type'] = ResourceType.directory
230237
except we.RemoteResourceNotFound as exc:
@@ -238,9 +245,9 @@ def listdir(self, path):
238245
if not self.getinfo(_path).is_dir:
239246
raise errors.DirectoryExpected(path)
240247

241-
dir_list = self.client.list(_path)
248+
dir_list = self.client.list(_path.encode('utf-8'))
242249
if six.PY2:
243-
dir_list = map(operator.methodcaller('decode'), dir_list)
250+
dir_list = map(operator.methodcaller('decode', 'utf-8'), dir_list)
244251

245252
return list(map(operator.methodcaller('rstrip', '/'), dir_list))
246253

@@ -255,7 +262,7 @@ def makedir(self, path, permissions=None, recreate=False):
255262
if self.exists(_path):
256263
raise errors.DirectoryExists(path)
257264
try:
258-
self.client.mkdir(_path)
265+
self.client.mkdir(_path.encode('utf-8'))
259266
except we.RemoteParentNotFound as exc:
260267
raise errors.ResourceNotFound(path, exc=exc)
261268

@@ -285,7 +292,7 @@ def remove(self, path):
285292
_path = self.validatepath(path)
286293
if self.getinfo(path).is_dir:
287294
raise errors.FileExpected(path)
288-
self.client.clean(_path)
295+
self.client.clean(_path.encode('utf-8'))
289296

290297
def removedir(self, path):
291298
_path = self.validatepath(path)
@@ -295,15 +302,15 @@ def removedir(self, path):
295302
raise errors.DirectoryExpected(path)
296303
if not self.isempty(_path):
297304
raise errors.DirectoryNotEmpty(path)
298-
self.client.clean(_path)
305+
self.client.clean(_path.encode('utf-8'))
299306

300307
def setbytes(self, path, contents):
301308
if not isinstance(contents, bytes):
302309
raise ValueError('contents must be bytes')
303310
_path = self.validatepath(path)
304311
bin_file = io.BytesIO(contents)
305312
with self._lock:
306-
resource = self._create_resource(_path)
313+
resource = self._create_resource(_path.encode('utf-8'))
307314
resource.read_from(bin_file)
308315

309316
def setinfo(self, path, info):
@@ -321,7 +328,7 @@ def copy(self, src_path, dst_path, overwrite=False):
321328
if not overwrite and self.exists(_dst_path):
322329
raise errors.DestinationExists(dst_path)
323330
try:
324-
self.client.copy(_src_path, _dst_path)
331+
self.client.copy(_src_path.encode('utf-8'), _dst_path.encode('utf-8'))
325332
except we.RemoteResourceNotFound as exc:
326333
raise errors.ResourceNotFound(src_path, exc=exc)
327334
except we.RemoteParentNotFound as exc:
@@ -337,7 +344,7 @@ def move(self, src_path, dst_path, overwrite=False):
337344
raise errors.DestinationExists(dst_path)
338345
with self._lock:
339346
try:
340-
self.client.move(_src_path, _dst_path, overwrite=overwrite)
347+
self.client.move(_src_path.encode('utf-8'), _dst_path.encode('utf-8'), overwrite=overwrite)
341348
except we.RemoteResourceNotFound as exc:
342349
raise errors.ResourceNotFound(src_path, exc=exc)
343350
except we.RemoteParentNotFound as exc:

0 commit comments

Comments
 (0)