|
17 | 17 | )
|
18 | 18 |
|
19 | 19 |
|
| 20 | +@contextmanager |
| 21 | +def tempzip(data={}): |
| 22 | + f = tempfile.mkstemp(suffix="zip")[1] |
| 23 | + with zipfile.ZipFile(f, mode="w") as z: |
| 24 | + for k, v in data.items(): |
| 25 | + z.writestr(k, v) |
| 26 | + try: |
| 27 | + yield f |
| 28 | + finally: |
| 29 | + try: |
| 30 | + os.remove(f) |
| 31 | + except (IOError, OSError): |
| 32 | + pass |
| 33 | + |
| 34 | + |
20 | 35 | @pytest.mark.parametrize(
|
21 | 36 | "path, name_function, num, out",
|
22 | 37 | [
|
@@ -196,42 +211,25 @@ def test_url_to_fs(ftp_writable):
|
196 | 211 |
|
197 | 212 | def test_target_protocol_options(ftp_writable):
|
198 | 213 | host, port, username, password = ftp_writable
|
199 |
| - data = b"hello_protocol" |
| 214 | + data = {"afile": b"hello"} |
200 | 215 | options = {"host": host, "port": port, "username": username, "password": password}
|
201 |
| - with fsspec.open(f"ftp://{username}:{password}@{host}:{port}/afile", "wb") as f: |
202 |
| - f.write(data) |
203 |
| - with fsspec.open(f"ftp://{username}:{password}@{host}:{port}/afile", "rb") as f: |
204 |
| - assert f.read() == data |
205 |
| - with fsspec.open("ftp:///afile", "rb", **options) as f: |
206 |
| - assert f.read() == data |
| 216 | + with tempzip(data) as lfile, fsspec.open( |
| 217 | + "ftp:///archive.zip", "wb", **options |
| 218 | + ) as f: |
| 219 | + f.write(open(lfile, "rb").read()) |
207 | 220 | with fsspec.open(
|
208 |
| - "simplecache://afile", |
| 221 | + "zip://afile", |
209 | 222 | "rb",
|
210 | 223 | target_protocol="ftp",
|
211 | 224 | target_options=options,
|
| 225 | + fo="archive.zip", |
212 | 226 | ) as f:
|
213 |
| - assert f.read() == data |
| 227 | + assert f.read() == data["afile"] |
214 | 228 |
|
215 | 229 |
|
216 | 230 | def test_chained_url(ftp_writable):
|
217 | 231 | host, port, username, password = ftp_writable
|
218 |
| - |
219 |
| - @contextmanager |
220 |
| - def tempzip(data={}): |
221 |
| - f = tempfile.mkstemp(suffix="zip")[1] |
222 |
| - with zipfile.ZipFile(f, mode="w") as z: |
223 |
| - for k, v in data.items(): |
224 |
| - z.writestr(k, v) |
225 |
| - try: |
226 |
| - yield f |
227 |
| - finally: |
228 |
| - try: |
229 |
| - os.remove(f) |
230 |
| - except (IOError, OSError): |
231 |
| - pass |
232 |
| - |
233 | 232 | data = {"afile": b"hello"}
|
234 |
| - |
235 | 233 | cls = fsspec.get_filesystem_class("ftp")
|
236 | 234 | fs = cls(host=host, port=port, username=username, password=password)
|
237 | 235 | with tempzip(data) as lfile:
|
|
0 commit comments