3
3
import logging
4
4
from io import StringIO
5
5
from pathlib import Path
6
- from typing import TYPE_CHECKING , ClassVar , Literal
6
+ from typing import TYPE_CHECKING , overload
7
7
8
8
from typing_extensions import override
9
9
17
17
18
18
if TYPE_CHECKING :
19
19
from collections .abc import AsyncIterator
20
- from typing import Any
20
+ from typing import Any , ClassVar , Literal
21
21
22
22
from typing_extensions import Unpack
23
23
@@ -267,12 +267,33 @@ async def iterate_items(
267
267
):
268
268
yield item
269
269
270
+ @overload
271
+ async def export_to (
272
+ self ,
273
+ key : str ,
274
+ content_type : Literal ['json' ],
275
+ to_key_value_store_id : str | None = None ,
276
+ to_key_value_store_name : str | None = None ,
277
+ ** kwargs : Unpack [ExportDataJsonKwargs ],
278
+ ) -> None : ...
279
+
280
+ @overload
281
+ async def export_to (
282
+ self ,
283
+ key : str ,
284
+ content_type : Literal ['csv' ],
285
+ to_key_value_store_id : str | None = None ,
286
+ to_key_value_store_name : str | None = None ,
287
+ ** kwargs : Unpack [ExportDataCsvKwargs ],
288
+ ) -> None : ...
289
+
270
290
async def export_to (
271
291
self ,
272
292
key : str ,
273
293
content_type : Literal ['json' , 'csv' ] = 'json' ,
274
294
to_key_value_store_id : str | None = None ,
275
295
to_key_value_store_name : str | None = None ,
296
+ ** kwargs : Any ,
276
297
) -> None :
277
298
"""Export the entire dataset into a specified file stored under a key in a key-value store.
278
299
@@ -288,42 +309,16 @@ async def export_to(
288
309
Specify only one of ID or name.
289
310
to_key_value_store_name: Name of the key-value store to save the exported file.
290
311
Specify only one of ID or name.
312
+ kwargs: Additional parameters for the export operation, specific to the chosen content type.
291
313
"""
314
+ kvs = await KeyValueStore .open (id = to_key_value_store_id , name = to_key_value_store_name )
315
+ dst = StringIO ()
316
+
292
317
if content_type == 'csv' :
293
- await self .export_to_csv (
294
- key ,
295
- to_key_value_store_id ,
296
- to_key_value_store_name ,
297
- )
318
+ await export_csv_to_stream (self .iterate_items (), dst , ** kwargs )
319
+ await kvs .set_value (key , dst .getvalue (), 'text/csv' )
298
320
elif content_type == 'json' :
299
- await self .export_to_json (
300
- key ,
301
- to_key_value_store_id ,
302
- to_key_value_store_name ,
303
- )
321
+ await export_json_to_stream (self .iterate_items (), dst , ** kwargs )
322
+ await kvs .set_value (key , dst .getvalue (), 'application/json' )
304
323
else :
305
324
raise ValueError ('Unsupported content type, expecting CSV or JSON' )
306
-
307
- async def export_to_json (
308
- self ,
309
- key : str ,
310
- to_key_value_store_id : str | None = None ,
311
- to_key_value_store_name : str | None = None ,
312
- ** kwargs : Unpack [ExportDataJsonKwargs ],
313
- ) -> None :
314
- kvs = await KeyValueStore .open (id = to_key_value_store_id , name = to_key_value_store_name )
315
- dst = StringIO ()
316
- await export_json_to_stream (self .iterate_items (), dst , ** kwargs )
317
- await kvs .set_value (key , dst .getvalue (), 'application/json' )
318
-
319
- async def export_to_csv (
320
- self ,
321
- key : str ,
322
- to_key_value_store_id : str | None = None ,
323
- to_key_value_store_name : str | None = None ,
324
- ** kwargs : Unpack [ExportDataCsvKwargs ],
325
- ) -> None :
326
- kvs = await KeyValueStore .open (id = to_key_value_store_id , name = to_key_value_store_name )
327
- dst = StringIO ()
328
- await export_csv_to_stream (self .iterate_items (), dst , ** kwargs )
329
- await kvs .set_value (key , dst .getvalue (), 'text/csv' )
0 commit comments