refactor webdav to use client; extract write_text to base (#5411)

This commit is contained in:
Saugat Pachhai
2021-02-04 16:48:25 +05:45
committed by GitHub
parent ce554b6ae7
commit 21200c14df
9 changed files with 29 additions and 67 deletions
-6
View File
@@ -53,12 +53,6 @@ class Azure(Base, CloudURLInfo):
def write_bytes(self, contents):
self.blob_client.upload_blob(contents, overwrite=True)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
stream = self.blob_client.download_blob()
return stream.readall()
+5 -1
View File
@@ -1,3 +1,4 @@
import locale
import pathlib
from funcy import cached_property
@@ -19,7 +20,10 @@ class Base(URLInfo):
raise NotImplementedError
def write_text(self, contents, encoding=None, errors=None):
raise NotImplementedError
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def write_bytes(self, contents):
raise NotImplementedError
-6
View File
@@ -84,12 +84,6 @@ class GCP(Base, CloudURLInfo):
assert isinstance(contents, bytes)
self._blob.upload_from_string(contents)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
return self._blob.download_as_string()
-6
View File
@@ -58,12 +58,6 @@ class HDFS(Base, URLInfo): # pylint: disable=abstract-method
with _hdfs.open_output_stream(self.path) as fobj:
fobj.write(contents)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
with self._hdfs() as _hdfs:
with _hdfs.open_input_stream(self.path) as fobj:
-7
View File
@@ -1,5 +1,4 @@
# pylint:disable=abstract-method
import locale
import os
import uuid
@@ -26,12 +25,6 @@ class HTTP(Base, HTTPURLInfo):
response = requests.post(self.url, data=contents)
assert response.status_code == 200
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
@pytest.fixture(scope="session")
def http_server(test_config, tmp_path_factory):
-6
View File
@@ -80,12 +80,6 @@ class S3(Base, CloudURLInfo):
Bucket=self.bucket, Key=self.path, Body=contents,
)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
data = self._s3.get_object(Bucket=self.bucket, Key=self.path)
return data["Body"].read()
-6
View File
@@ -92,12 +92,6 @@ class SSHMocked(Base, URLInfo):
# NOTE: accepts both str and bytes
fobj.write(contents)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
with self._ssh() as _ssh:
# NOTE: sftp always reads in binary format
+24 -23
View File
@@ -1,12 +1,9 @@
# pylint:disable=abstract-method
import os
import uuid
from pathlib import Path
from tempfile import TemporaryDirectory
from wsgiref.simple_server import make_server
import pytest
from funcy import first
from funcy import cached_property, first
from wsgidav.wsgidav_app import WsgiDAVApp
from dvc.path_info import WebDAVURLInfo
@@ -15,27 +12,34 @@ from tests.utils.httpd import run_server_on_thread
from .base import Base
AUTH = {"user1": {"password": "password1"}}
_WEBDAV_ROOT = TemporaryDirectory()
_WEBDAV_DIR = Path(_WEBDAV_ROOT.name)
class Webdav(Base, WebDAVURLInfo):
@staticmethod
def get_url(port, root_id): # pylint: disable=arguments-differ
return f"webdav://localhost:{port}/{root_id}/"
def get_url(port): # pylint: disable=arguments-differ
return f"webdav://localhost:{port}"
@cached_property
def client(self):
from webdav3.client import Client
user, secrets = first(AUTH.items())
return Client(
{
"webdav_hostname": self.replace(path="").url,
"webdav_login": user,
"webdav_password": secrets["password"],
}
)
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
self.dir_path.mkdir(parents=parents, exist_ok=True)
assert mode == 0o777
parent_dirs = list(reversed(self.parents))[1:] if parents else []
for d in parent_dirs + [self]:
self.client.mkdir(d.path) # pylint: disable=no-member
def write_bytes(self, contents):
self.dir_path.write_bytes(contents)
def write_text(self, contents, encoding=None, errors=None):
self.dir_path.write_text(contents, encoding=encoding, errors=errors)
@property
def dir_path(self):
return _WEBDAV_DIR / self.path[1:]
self.client.upload_to(contents, self.path)
@pytest.fixture
@@ -43,10 +47,8 @@ def webdav_server(test_config, tmp_path_factory):
test_config.requires("webdav")
host, port = "localhost", 0
root_id = str(uuid.uuid4())
root_dir = _WEBDAV_DIR / root_id
root_dir.mkdir()
dirmap = {f"/{root_id}": os.fspath(_WEBDAV_DIR / root_id)}
directory = os.fspath(tmp_path_factory.mktemp("http"))
dirmap = {"/": directory}
app = WsgiDAVApp(
{
@@ -57,14 +59,13 @@ def webdav_server(test_config, tmp_path_factory):
}
)
server = make_server(host, port, app)
server.root_id = root_id
with run_server_on_thread(server) as httpd:
yield httpd
@pytest.fixture
def webdav(webdav_server):
url = Webdav.get_url(webdav_server.server_port, webdav_server.root_id)
url = Webdav.get_url(webdav_server.server_port)
ret = Webdav(url)
user, secrets = first(AUTH.items())
ret.config = {"url": url, "user": user, **secrets}
-6
View File
@@ -45,12 +45,6 @@ class WebHDFS(Base, URLInfo): # pylint: disable=abstract-method
with _hdfs.write(self.path, overwrite=True) as writer:
writer.write(contents)
def write_text(self, contents, encoding=None, errors=None):
if not encoding:
encoding = locale.getpreferredencoding(False)
assert errors is None
self.write_bytes(contents.encode(encoding))
def read_bytes(self):
with self._webhdfs() as _hdfs:
with _hdfs.read(self.path) as reader: