From b749855a800080c42845164849083049fbb49e6f Mon Sep 17 00:00:00 2001 From: "evgeny.ezhov" Date: Sun, 10 Sep 2017 11:51:10 +0300 Subject: [PATCH] =?UTF-8?q?fixes=20#2=20=E2=80=93=20client=20method=20uplo?= =?UTF-8?q?ad=5Ffile=20won't=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/res/test.txt | 1 + tests/test_client.py | 26 ++++++++++++++++++++++++++ webdav2/client.py | 31 ++++++++----------------------- webdav2/exceptions.py | 18 ++++++++++++++++-- 4 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 tests/res/test.txt create mode 100644 tests/test_client.py diff --git a/tests/res/test.txt b/tests/res/test.txt new file mode 100644 index 0000000..1cc4810 --- /dev/null +++ b/tests/res/test.txt @@ -0,0 +1 @@ +test content for testing of webdav client \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 0000000..ca98aae --- /dev/null +++ b/tests/test_client.py @@ -0,0 +1,26 @@ +from unittest import TestCase + +from webdav2.client import Client + + +class ClientTestCase(TestCase): + def setUp(self): + options = { + 'webdav_hostname': 'https://webdav.yandex.ru', + 'webdav_login': 'webdavclient.test', + 'webdav_password': 'Qwerty123!' + } + self.client = Client(options) + + def test_list(self): + file_list = self.client.list() + self.assertIsNotNone(file_list, 'List of files should not be None') + self.assertGreater(file_list.__len__(), 0, 'Expected that amount of files more then 0') + + def test_upload_file(self): + remote_path = 'test.txt' + local_path = './res/test.txt' + if self.client.check(remote_path=remote_path): + self.client.clean(remote_path=remote_path) + self.client.upload_file(remote_path=remote_path, local_path=local_path) + self.assertTrue(self.client.check(remote_path=remote_path)) diff --git a/webdav2/client.py b/webdav2/client.py index 088e762..f35d077 100644 --- a/webdav2/client.py +++ b/webdav2/client.py @@ -371,7 +371,7 @@ class Client(object): if buff.tell() == 0: data = buff.read() else: - data = buff + data = buff response = requests.request( 'PUT', options["URL"], @@ -388,7 +388,7 @@ class Client(object): if os.path.isdir(local_path): self.upload_directory(local_path=local_path, remote_path=remote_path, progress=progress) else: - self.upload_file(local_path=local_path, remote_path=remote_path, progress=progress) + self.upload_file(local_path=local_path, remote_path=remote_path) def upload_directory(self, remote_path, local_path, progress=None): @@ -414,7 +414,7 @@ class Client(object): self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress) @wrap_connection_error - def upload_file(self, remote_path, local_path, progress=None): + def upload_file(self, remote_path, local_path): if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) @@ -431,37 +431,22 @@ class Client(object): raise RemoteParentNotFound(urn.path()) with open(local_path, "rb") as local_file: - url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} - options = { - 'URL': "{hostname}{root}{path}".format(**url), - 'HTTPHEADER': self.get_header('upload_file'), - 'UPLOAD': 1, - 'READFUNCTION': local_file.read, - # 'NOPROGRESS': 0 if progress else 1 - } - - # if progress: - # options["PROGRESSFUNCTION"] = progress file_size = os.path.getsize(local_path) if file_size > self.large_size: - options['INFILESIZE_LARGE'] = file_size - else: - options['INFILESIZE'] = file_size + raise ResourceTooBig(path=local_path, size=file_size) response = requests.request( - 'PUT', - options["URL"], + method='PUT', + url="{hostname}{root}{path}".format(**url), auth=(self.webdav.login, self.webdav.password), headers=self.get_header('upload_file'), - data=buff + data=local_file ) - if request.status_code == 507: + if response.status_code == 507: raise NotEnoughSpace() - # request.close() - def upload_sync(self, remote_path, local_path, callback=None): self.upload(local_path=local_path, remote_path=remote_path) diff --git a/webdav2/exceptions.py b/webdav2/exceptions.py index 571ba1d..4318d84 100644 --- a/webdav2/exceptions.py +++ b/webdav2/exceptions.py @@ -13,7 +13,8 @@ class OptionNotValid(NotValid): self.ns = ns def __str__(self): - return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name, value=self.value) + return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name, + value=self.value) class CertificateNotValid(NotValid): @@ -48,6 +49,19 @@ class RemoteParentNotFound(NotFound): return "Remote parent for: {path} not found".format(path=self.path) +class ResourceTooBig(WebDavException): + def __init__(self, path, size, max_size): + self.path = path + self.size = size + self.max_size = max_size + + def __str__(self): + return "Resource {path} is too big, it should be less then {max_size} but actually: {size}".format( + path=self.path, + max_size=self.max_size, + size=self.size) + + class MethodNotSupported(WebDavException): def __init__(self, name, server): self.name = name @@ -70,4 +84,4 @@ class NotEnoughSpace(WebDavException): pass def __str__(self): - return "Not enough space on the server" \ No newline at end of file + return "Not enough space on the server"