cornflex 1.1.5__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cornflex-1.1.5 → cornflex-1.2.0}/PKG-INFO +1 -1
- {cornflex-1.1.5 → cornflex-1.2.0}/cornflex/reader.py +54 -1
- {cornflex-1.1.5 → cornflex-1.2.0}/pyproject.toml +1 -1
- {cornflex-1.1.5 → cornflex-1.2.0}/tests/test_reader.py +94 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/uv.lock +1 -1
- {cornflex-1.1.5 → cornflex-1.2.0}/.github/workflows/pr.yml +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/.github/workflows/publish.yml +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/.gitignore +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/LICENSE +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/README.md +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/conftest.py +0 -0
- {cornflex-1.1.5 → cornflex-1.2.0}/cornflex/__init__.py +0 -0
|
@@ -5,7 +5,7 @@ import shutil
|
|
|
5
5
|
import tempfile
|
|
6
6
|
import zipfile
|
|
7
7
|
from dataclasses import dataclass
|
|
8
|
-
from typing import Any, Generator, List, Optional
|
|
8
|
+
from typing import Any, Callable, Generator, List, Optional
|
|
9
9
|
|
|
10
10
|
import chardet
|
|
11
11
|
import paramiko
|
|
@@ -218,3 +218,56 @@ class SFTPReader:
|
|
|
218
218
|
except Exception as e:
|
|
219
219
|
print(f"Error getting {file_name}: {e}")
|
|
220
220
|
return None
|
|
221
|
+
|
|
222
|
+
def _upload_and_finalize(
|
|
223
|
+
self,
|
|
224
|
+
write_to_temp: Callable[[str], None],
|
|
225
|
+
remote_path: str,
|
|
226
|
+
file_name: str,
|
|
227
|
+
) -> paramiko.SFTPAttributes:
|
|
228
|
+
if not self._sftp:
|
|
229
|
+
raise RuntimeError("Not connected. Call connect() first.")
|
|
230
|
+
|
|
231
|
+
final_path = f"{remote_path.rstrip('/')}/{file_name}"
|
|
232
|
+
tmp_path = f"{final_path}.part"
|
|
233
|
+
write_to_temp(tmp_path)
|
|
234
|
+
self._sftp.rename(tmp_path, final_path)
|
|
235
|
+
return self._sftp.stat(final_path)
|
|
236
|
+
|
|
237
|
+
def put_bytes(
|
|
238
|
+
self,
|
|
239
|
+
content: bytes,
|
|
240
|
+
file_name: str,
|
|
241
|
+
remote_path: str = ".",
|
|
242
|
+
) -> paramiko.SFTPAttributes:
|
|
243
|
+
def _write(tmp_path: str) -> None:
|
|
244
|
+
with io.BytesIO(content) as buffer:
|
|
245
|
+
self._sftp.putfo(buffer, tmp_path)
|
|
246
|
+
|
|
247
|
+
return self._upload_and_finalize(_write, remote_path, file_name)
|
|
248
|
+
|
|
249
|
+
def put_string(
|
|
250
|
+
self,
|
|
251
|
+
content: str,
|
|
252
|
+
file_name: str,
|
|
253
|
+
remote_path: str = ".",
|
|
254
|
+
encoding: str = "utf-8",
|
|
255
|
+
) -> paramiko.SFTPAttributes:
|
|
256
|
+
return self.put_bytes(
|
|
257
|
+
content=content.encode(encoding),
|
|
258
|
+
file_name=file_name,
|
|
259
|
+
remote_path=remote_path,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
def put_file(
|
|
263
|
+
self,
|
|
264
|
+
local_path: str,
|
|
265
|
+
remote_path: str = ".",
|
|
266
|
+
file_name: Optional[str] = None,
|
|
267
|
+
) -> paramiko.SFTPAttributes:
|
|
268
|
+
remote_file_name = file_name or os.path.basename(local_path)
|
|
269
|
+
|
|
270
|
+
def _write(tmp_path: str) -> None:
|
|
271
|
+
self._sftp.put(local_path, tmp_path)
|
|
272
|
+
|
|
273
|
+
return self._upload_and_finalize(_write, remote_path, remote_file_name)
|
|
@@ -223,3 +223,97 @@ def test_file_to_string_returns_none_on_error(connected_reader: SFTPReader) -> N
|
|
|
223
223
|
connected_reader._sftp.file.side_effect = Exception("boom")
|
|
224
224
|
result = connected_reader.file_to_string("bad.txt")
|
|
225
225
|
assert result is None
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
# --- put_bytes ---
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def test_put_bytes_raises_when_not_connected(reader_password: SFTPReader) -> None:
|
|
232
|
+
with pytest.raises(RuntimeError, match="Not connected"):
|
|
233
|
+
reader_password.put_bytes(b"hello", "file.txt")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def test_put_bytes_writes_to_temp_then_renames(connected_reader: SFTPReader) -> None:
|
|
237
|
+
captured: dict = {}
|
|
238
|
+
|
|
239
|
+
def _capture_putfo(buffer, tmp_path) -> None:
|
|
240
|
+
captured["content"] = buffer.read()
|
|
241
|
+
captured["tmp_path"] = tmp_path
|
|
242
|
+
|
|
243
|
+
connected_reader._sftp.putfo.side_effect = _capture_putfo
|
|
244
|
+
connected_reader._sftp.stat.return_value = "final-attrs"
|
|
245
|
+
|
|
246
|
+
result = connected_reader.put_bytes(b"hello", "orders.csv", remote_path="/out")
|
|
247
|
+
|
|
248
|
+
assert captured == {"content": b"hello", "tmp_path": "/out/orders.csv.part"}
|
|
249
|
+
connected_reader._sftp.rename.assert_called_once_with(
|
|
250
|
+
"/out/orders.csv.part", "/out/orders.csv"
|
|
251
|
+
)
|
|
252
|
+
connected_reader._sftp.stat.assert_called_once_with("/out/orders.csv")
|
|
253
|
+
assert result == "final-attrs"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
# --- put_string ---
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def test_put_string_encodes_and_delegates_to_put_bytes(
|
|
260
|
+
connected_reader: SFTPReader,
|
|
261
|
+
) -> None:
|
|
262
|
+
connected_reader.put_bytes = MagicMock(return_value="attrs")
|
|
263
|
+
|
|
264
|
+
result = connected_reader.put_string("héllo", "orders.csv", remote_path="/out")
|
|
265
|
+
|
|
266
|
+
connected_reader.put_bytes.assert_called_once_with(
|
|
267
|
+
content="héllo".encode("utf-8"),
|
|
268
|
+
file_name="orders.csv",
|
|
269
|
+
remote_path="/out",
|
|
270
|
+
)
|
|
271
|
+
assert result == "attrs"
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def test_put_string_respects_custom_encoding(connected_reader: SFTPReader) -> None:
|
|
275
|
+
connected_reader.put_bytes = MagicMock(return_value="attrs")
|
|
276
|
+
|
|
277
|
+
connected_reader.put_string("héllo", "orders.csv", encoding="latin-1")
|
|
278
|
+
|
|
279
|
+
connected_reader.put_bytes.assert_called_once_with(
|
|
280
|
+
content="héllo".encode("latin-1"),
|
|
281
|
+
file_name="orders.csv",
|
|
282
|
+
remote_path=".",
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
# --- put_file ---
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def test_put_file_raises_when_not_connected(reader_password: SFTPReader) -> None:
|
|
290
|
+
with pytest.raises(RuntimeError, match="Not connected"):
|
|
291
|
+
reader_password.put_file("/local/orders.csv")
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def test_put_file_streams_from_disk_then_renames(connected_reader: SFTPReader) -> None:
|
|
295
|
+
connected_reader._sftp.stat.return_value = "final-attrs"
|
|
296
|
+
|
|
297
|
+
result = connected_reader.put_file("/local/orders.csv", remote_path="/out")
|
|
298
|
+
|
|
299
|
+
connected_reader._sftp.put.assert_called_once_with(
|
|
300
|
+
"/local/orders.csv", "/out/orders.csv.part"
|
|
301
|
+
)
|
|
302
|
+
connected_reader._sftp.rename.assert_called_once_with(
|
|
303
|
+
"/out/orders.csv.part", "/out/orders.csv"
|
|
304
|
+
)
|
|
305
|
+
connected_reader._sftp.stat.assert_called_once_with("/out/orders.csv")
|
|
306
|
+
assert result == "final-attrs"
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def test_put_file_uses_explicit_file_name(connected_reader: SFTPReader) -> None:
|
|
310
|
+
connected_reader.put_file(
|
|
311
|
+
"/local/orders.csv", remote_path="/out", file_name="custom.csv"
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
connected_reader._sftp.put.assert_called_once_with(
|
|
315
|
+
"/local/orders.csv", "/out/custom.csv.part"
|
|
316
|
+
)
|
|
317
|
+
connected_reader._sftp.rename.assert_called_once_with(
|
|
318
|
+
"/out/custom.csv.part", "/out/custom.csv"
|
|
319
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|