Changeset View
Changeset View
Standalone View
Standalone View
swh/journal/writer/__init__.py
# Copyright (C) 2019-2022 The Software Heritage developers | # Copyright (C) 2019-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Any, Dict, Type | import os | ||||
import sys | |||||
from typing import Any, BinaryIO, Dict, Type | |||||
import warnings | import warnings | ||||
from .interface import JournalWriterInterface | from .interface import JournalWriterInterface | ||||
def model_object_dict_sanitizer( | def model_object_dict_sanitizer( | ||||
object_type: str, object_dict: Dict[str, Any] | object_type: str, object_dict: Dict[str, Any] | ||||
) -> Dict[str, str]: | ) -> Dict[str, str]: | ||||
Show All 30 Lines | elif cls == "kafka": | ||||
JournalWriter = KafkaJournalWriter | JournalWriter = KafkaJournalWriter | ||||
elif cls == "stream": | elif cls == "stream": | ||||
from .stream import StreamJournalWriter | from .stream import StreamJournalWriter | ||||
JournalWriter = StreamJournalWriter | JournalWriter = StreamJournalWriter | ||||
assert "output_stream" in kwargs | assert "output_stream" in kwargs | ||||
outstream: BinaryIO | |||||
if kwargs["output_stream"] in ("-", b"-"): | |||||
outstream = os.fdopen(sys.stdout.fileno(), "wb", closefd=False) | |||||
elif isinstance(kwargs["output_stream"], (str, bytes)): | |||||
outstream = open(kwargs["output_stream"], "wb") | |||||
else: | |||||
outstream = kwargs["output_stream"] | |||||
kwargs["output_stream"] = outstream | |||||
else: | else: | ||||
raise ValueError("Unknown journal writer class `%s`" % cls) | raise ValueError("Unknown journal writer class `%s`" % cls) | ||||
return JournalWriter(**kwargs) | return JournalWriter(**kwargs) |