33import io
44import six
55import threading
6+ import operator
67import logging
78
89import webdav2 .client as wc
@@ -166,7 +167,7 @@ def _create_resource(self, path):
166167 return res
167168
168169 def get_resource (self , path ):
169- return self ._create_resource (path )
170+ return self ._create_resource (path . encode ( 'utf-8' ) )
170171
171172 @staticmethod
172173 def _create_info_dict (info ):
@@ -176,19 +177,26 @@ def _create_info_dict(info):
176177 'access' : {}
177178 }
178179
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+
179187 for key , val in six .iteritems (info ):
180188 if key in basics :
181- info_dict ['basic' ][key ] = six . u (val )
189+ info_dict ['basic' ][key ] = decode (val )
182190 elif key in details :
183191 if key == 'size' and val :
184192 val = int (val )
185193 elif val :
186- val = six . u (val )
187- info_dict ['details' ][key ] = val
194+ val = decode (val )
195+ info_dict ['details' ][key ] = decode ( val )
188196 elif key in access :
189- info_dict ['access' ][key ] = six . u (val )
197+ info_dict ['access' ][key ] = decode (val )
190198 else :
191- info_dict ['other' ][key ] = six . u (val )
199+ info_dict ['other' ][key ] = decode (val )
192200
193201 return info_dict
194202
@@ -202,7 +210,7 @@ def create(self, path, wipe=False):
202210
203211 def exists (self , path ):
204212 _path = self .validatepath (path )
205- return self .client .check (_path )
213+ return self .client .check (_path . encode ( 'utf-8' ) )
206214
207215 def getinfo (self , path , namespaces = None ):
208216 _path = self .validatepath (path )
@@ -221,9 +229,9 @@ def getinfo(self, path, namespaces=None):
221229
222230 else :
223231 try :
224- info = self .client .info (_path )
232+ info = self .client .info (_path . encode ( 'utf-8' ) )
225233 info_dict = self ._create_info_dict (info )
226- if self .client .is_dir (_path ):
234+ if self .client .is_dir (_path . encode ( 'utf-8' ) ):
227235 info_dict ['basic' ]['is_dir' ] = True
228236 info_dict ['details' ]['type' ] = ResourceType .directory
229237 except we .RemoteResourceNotFound as exc :
@@ -237,8 +245,11 @@ def listdir(self, path):
237245 if not self .getinfo (_path ).is_dir :
238246 raise errors .DirectoryExpected (path )
239247
240- dir_list = self .client .list (_path )
241- return map (six .u , dir_list ) if six .PY2 else dir_list
248+ dir_list = self .client .list (_path .encode ('utf-8' ))
249+ if six .PY2 :
250+ dir_list = map (operator .methodcaller ('decode' , 'utf-8' ), dir_list )
251+
252+ return list (map (operator .methodcaller ('rstrip' , '/' ), dir_list ))
242253
243254 def makedir (self , path , permissions = None , recreate = False ):
244255 _path = self .validatepath (path )
@@ -251,7 +262,7 @@ def makedir(self, path, permissions=None, recreate=False):
251262 if self .exists (_path ):
252263 raise errors .DirectoryExists (path )
253264 try :
254- self .client .mkdir (_path )
265+ self .client .mkdir (_path . encode ( 'utf-8' ) )
255266 except we .RemoteParentNotFound as exc :
256267 raise errors .ResourceNotFound (path , exc = exc )
257268
@@ -281,7 +292,7 @@ def remove(self, path):
281292 _path = self .validatepath (path )
282293 if self .getinfo (path ).is_dir :
283294 raise errors .FileExpected (path )
284- self .client .clean (_path )
295+ self .client .clean (_path . encode ( 'utf-8' ) )
285296
286297 def removedir (self , path ):
287298 _path = self .validatepath (path )
@@ -291,15 +302,15 @@ def removedir(self, path):
291302 raise errors .DirectoryExpected (path )
292303 if not self .isempty (_path ):
293304 raise errors .DirectoryNotEmpty (path )
294- self .client .clean (_path )
305+ self .client .clean (_path . encode ( 'utf-8' ) )
295306
296307 def setbytes (self , path , contents ):
297308 if not isinstance (contents , bytes ):
298309 raise ValueError ('contents must be bytes' )
299310 _path = self .validatepath (path )
300311 bin_file = io .BytesIO (contents )
301312 with self ._lock :
302- resource = self ._create_resource (_path )
313+ resource = self ._create_resource (_path . encode ( 'utf-8' ) )
303314 resource .read_from (bin_file )
304315
305316 def setinfo (self , path , info ):
@@ -312,10 +323,12 @@ def copy(self, src_path, dst_path, overwrite=False):
312323 _dst_path = self .validatepath (dst_path )
313324
314325 with self ._lock :
326+ if not self .getinfo (_src_path ).is_file :
327+ raise errors .FileExpected (src_path )
315328 if not overwrite and self .exists (_dst_path ):
316329 raise errors .DestinationExists (dst_path )
317330 try :
318- self .client .copy (_src_path , _dst_path )
331+ self .client .copy (_src_path . encode ( 'utf-8' ) , _dst_path . encode ( 'utf-8' ) )
319332 except we .RemoteResourceNotFound as exc :
320333 raise errors .ResourceNotFound (src_path , exc = exc )
321334 except we .RemoteParentNotFound as exc :
@@ -325,11 +338,13 @@ def move(self, src_path, dst_path, overwrite=False):
325338 _src_path = self .validatepath (src_path )
326339 _dst_path = self .validatepath (dst_path )
327340
341+ if not self .getinfo (_src_path ).is_file :
342+ raise errors .FileExpected (src_path )
328343 if not overwrite and self .exists (_dst_path ):
329344 raise errors .DestinationExists (dst_path )
330345 with self ._lock :
331346 try :
332- 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 )
333348 except we .RemoteResourceNotFound as exc :
334349 raise errors .ResourceNotFound (src_path , exc = exc )
335350 except we .RemoteParentNotFound as exc :
0 commit comments