Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions easywebdav/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down