FileExporter¶
-
class
resilient_exporters.exporters.FileExporter(target_file: str, max_lines: Optional[int] = None, append: bool = True, **kwargs)[source]¶ Exporter for a text file.
- Parameters
target_file (str) – the path of the file. The user must have write rights on the file.
max_lines (int) – the maximum number of lines in the file, incl. the lines already present. If None, there’s no limit.
append (bool) – if True, will append the data to the file. Otherwise, it will overwrite the file.
**kwargs – the keyword arguments to pass down to parent’s class Exporter
Example
import os from resilient_exporters.exporters import FileExporter exporter = FileExporter("local_file.txt", max_lines=1000, append=False, use_memory=False, # see exporters.Exporter save_unsent_data=True) data = {"name": "Richard Feynman", "age": 69} exporter.send(data)
-
property
remaining_lines¶ It is the amount of lines the file can still contain before reaching the limit passed at initialisation (cf max_lines keyword argument at init). Returns 0 if there’s no limit or the limit has been reached.
-
send(data: Union[str, dict]) → resilient_exporters.exporters.ExportResult[source]¶ Writes the data into the file. Each call adds a new line in the file.
- Parameters
data (Union[Text, dict]) – a string or dict with the data to write into the file. If a dict, it will be converted into a json document.
- Returns
- if the operation was successful, returns (None, True),
otherwise (None, False)
- Return type
ExportResult
- Raises
ExportError – if the IO stream is closed.
-
start(append: bool = True)[source]¶ Restarts the exporter by reopening an IO stream to the file. If there were no stream yet, it will create one.
- Parameters
append (bool) – if True, opens the file in ‘append’ mode, else in “write” mode. Default is True.
-
stop()[source]¶ Stops the exporter, by closing the IO stream. The exporter must be stopped for another process to be able to read the file.
-
has_unsent_data() → bool¶ Assesses if there’s saved, unsent data.
- Returns
True if there’s saved, unsent data. False otherwise.
- Return type
bool
-
save_unsent_data(data: Any, kwargs: dict, exporter_name: str) → None¶ Saves the data in memory or disk, depending on the value of use_memory of the instance, so it can be sent later.
- Parameters
data (Any) – the core data.
kwargs (dict) – the keyword arguments to pass to send for the given data.
exporter_name (str) – the name of the exporter.
- Returns
None
-
send_unsent_data() → List[resilient_exporters.exporters.ExportResult]¶ Tries to send the previously saved, unsent data.
- Returns
list of the results of the export jobs.
- Return type
List[ExportResult]
-
transform(data: Any) → Any¶ Applies transform to the given data.
- Parameters
data (Any) – the data to pass to transform.
- Returns
the output of transform
- Return type
Any
- Raises
Exception – if the output of transform(data) is None.
-
property
use_memory¶ The value use_memory of the instance.
When set to
True, it loads the data into memory if it was previously using a file (previous value wasFalse), or vice versa.- Parameters
new_val (bool) – new value for use_memory.
- Returns
its value
- Return type
bool