diff --git a/easywebdav/client.py b/easywebdav/client.py index 4003198..d3529ac 100644 --- a/easywebdav/client.py +++ b/easywebdav/client.py @@ -35,13 +35,22 @@ def prop(elem, name, default=None): def elem2file(elem): - return File( - prop(elem, 'href'), - int(prop(elem, 'getcontentlength', 0)), - prop(elem, 'getlastmodified', ''), - prop(elem, 'creationdate', ''), - prop(elem, 'getcontenttype', ''), - ) + try: + return File( + prop(elem, 'href'), + int(prop(elem, 'getcontentlength', 0)), + prop(elem, 'getlastmodified', ''), + prop(elem, 'creationdate', ''), + prop(elem, 'getcontenttype', ''), + ) + except: + return File( + prop(elem, 'href'), + 0, + prop(elem, 'getlastmodified', ''), + prop(elem, 'creationdate', ''), + prop(elem, 'getcontenttype', ''), + ) class OperationFailed(WebdavException): @@ -150,7 +159,7 @@ def delete(self, path): self._send('DELETE', path, 204) def upload(self, local_path_or_fileobj, remote_path): - if isinstance(local_path_or_fileobj, basestring): + if isinstance(local_path_or_fileobj, str): with open(local_path_or_fileobj, 'rb') as f: self._upload(f, remote_path) else: @@ -161,12 +170,21 @@ def _upload(self, fileobj, remote_path): def download(self, remote_path, local_path_or_fileobj): response = self._send('GET', remote_path, 200, stream=True) - if isinstance(local_path_or_fileobj, basestring): + if isinstance(local_path_or_fileobj, str): with open(local_path_or_fileobj, 'wb') as f: self._download(f, response) else: self._download(local_path_or_fileobj, response) + def tree(self, remote_path='.'): + response = self._send('PROPFIND', remote_path, (207, 301)) + # Redirect + if response.status_code == 301: + url = urlparse(response.headers['location']) + return self.ls(url.path) + tree = xml.fromstring(response.content) + return [elem2file(elem) for elem in tree.findall('{DAV:}response')] + def _download(self, fileobj, response): for chunk in response.iter_content(DOWNLOAD_CHUNK_SIZE_BYTES): fileobj.write(chunk)