Skip to content

Commit b1ef3dc

Browse files
author
Martin Larralde
committed
Implement length_hint for WebDAVFile and remove duplicate code
1 parent 58ef2b4 commit b1ef3dc

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

webdavfs/webdavfs.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def _get_file_data(self):
5757
return data
5858

5959
if six.PY2:
60-
def _get_data_size(self):
60+
def __length_hint__(self):
6161
return len(self.data.getvalue())
6262
else:
63-
def _get_data_size(self):
63+
def __length_hint__(self):
6464
return self.data.getbuffer().nbytes
6565

6666
def __repr__(self):
@@ -89,7 +89,7 @@ def readable(self):
8989
def read(self, size=-1):
9090
if not self._mode.reading:
9191
raise IOError("File is not in read mode")
92-
self.pos = self.pos + size if size != -1 else self._get_data_size()
92+
self.pos = self.pos + size if size != -1 else self.__length_hint__()
9393
return self.data.read(size)
9494

9595
def seekable(self):
@@ -105,7 +105,7 @@ def seek(self, pos, whence=Seek.set):
105105
elif whence == Seek.end:
106106
if pos > 0:
107107
raise ValueError('Positive seek position {}'.format(pos))
108-
self.pos = max(0, self._get_data_size() + pos)
108+
self.pos = max(0, self.__length_hint__() + pos)
109109
else:
110110
raise ValueError('invalid value for whence')
111111

@@ -117,18 +117,21 @@ def tell(self):
117117

118118
def truncate(self, size=None):
119119
self.data.truncate(size)
120-
data_size = self._get_data_size()
120+
data_size = self.__length_hint__()
121121
if size and data_size < size:
122122
self.data.write(b'\0' * (size - data_size))
123+
return size or data_size
123124

124125
def writable(self):
125126
return self._mode.writing
126127

127128
def write(self, data):
128129
if not self._mode.writing:
129130
raise IOError("File is not in write mode")
130-
self.data.write(data)
131-
self.seek(len(data), Seek.current)
131+
bytes_written = self.data.write(data)
132+
self.seek(bytes_written, Seek.current)
133+
return bytes_written
134+
132135

133136

134137
class WebDAVFS(FS):
@@ -199,11 +202,10 @@ def exists(self, path):
199202
return self.client.check(path)
200203

201204
def getinfo(self, path, namespaces=None):
202-
self.check()
203205
_path = self.validatepath(path)
204206
namespaces = namespaces or ()
205207

206-
if _path == '/':
208+
if _path in '/':
207209
return Info({
208210
"basic":
209211
{
@@ -221,7 +223,7 @@ def getinfo(self, path, namespaces=None):
221223
info_dict = self._create_info_dict(info)
222224
if self.isdir(path):
223225
info_dict['basic']['is_dir'] = True
224-
info_dict['details']['type'] = int(ResourceType.directory)
226+
info_dict['details']['type'] = ResourceType.directory
225227
return Info(info_dict)
226228
except we.RemoteResourceNotFound as exc:
227229
raise errors.ResourceNotFound(path, exc=exc)
@@ -230,43 +232,33 @@ def listdir(self, path):
230232
self.check()
231233
_path = self.validatepath(path)
232234

233-
if not self.exists(_path):
234-
raise errors.ResourceNotFound(path)
235-
if not self.isdir(_path):
235+
if not self.getinfo(_path).is_dir:
236236
raise errors.DirectoryExpected(path)
237237

238238
dir_list = self.client.list(_path)
239-
if six.PY2:
240-
dir_list = [six.u(item) for item in dir_list]
241-
242-
return dir_list
239+
return map(six.u, dir_list) if six.PY2 else dir_list
243240

244241
def makedir(self, path, permissions=None, recreate=False):
245-
self.check()
246242
self.validatepath(path)
247243
_path = abspath(normpath(path))
248244

249245
if _path == '/':
250-
if recreate:
251-
return self.opendir(path)
252-
else:
246+
if not recreate:
253247
raise errors.DirectoryExists(path)
254248

255-
if not (recreate and self.isdir(path)):
249+
elif not (recreate and self.isdir(path)):
256250
if self.exists(_path):
257251
raise errors.DirectoryExists(path)
258-
259252
try:
260253
self.client.mkdir(_path)
261-
except we.RemoteParentNotFound:
262-
raise errors.ResourceNotFound(path)
254+
except we.RemoteParentNotFound as exc:
255+
raise errors.ResourceNotFound(path, exc=exc)
263256

264257
return self.opendir(path)
265258

266259
def openbin(self, path, mode='r', buffering=-1, **options):
267260
_mode = Mode(mode)
268261
_mode.validate_bin()
269-
self.check()
270262
self.validatepath(path)
271263

272264
log.debug("openbin: %s, %s", path, mode)
@@ -289,7 +281,7 @@ def remove(self, path):
289281
if not self.exists(path):
290282
raise errors.ResourceNotFound(path)
291283

292-
if self.isdir(path):
284+
if self.getinfo(path).is_dir:
293285
raise errors.FileExpected(path)
294286

295287
self.client.clean(path)

0 commit comments

Comments
 (0)