From c87780455349a9bdad9e902ede2bfc681d12a142 Mon Sep 17 00:00:00 2001 From: KaratekHD <38097062+KaratekHD@users.noreply.github.com> Date: Fri, 18 Sep 2020 14:24:45 +0200 Subject: [PATCH 1/2] Made the fix mentioned by @deidyomega in #48 --- easywebdav/client.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/easywebdav/client.py b/easywebdav/client.py index 4003198..b900efd 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): From acd7271e276b4e9ad6794c120872a12d6c56c90f Mon Sep 17 00:00:00 2001 From: KaratekHD <38097062+KaratekHD@users.noreply.github.com> Date: Fri, 18 Sep 2020 15:04:57 +0200 Subject: [PATCH 2/2] Added a second fix described in https://stackoverflow.com/questions/26130644/how-to-overcome-python-3-4-nameerror-name-basestring-is-not-defined --- easywebdav/client.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/easywebdav/client.py b/easywebdav/client.py index b900efd..d3529ac 100644 --- a/easywebdav/client.py +++ b/easywebdav/client.py @@ -159,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: @@ -170,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)