# -*- coding: utf-8 -*-"""Implements :class:`SimpleProducer`, a file-based producer for local developmentand testing."""importdataclassesfrompathlibimportPathfromfunc_args.apiimportREQfrom..abstractionimportAbcRecord,AbcBufferfrom..producerimportBaseProducer,RetryConfig
[docs]@dataclasses.dataclassclassSimpleProducer(BaseProducer):""" A simple producer that write data to a local, append-only file. It is a good example to show how to implement a producer and understand the behavior of the producer. You should use this producer along with :class:`~unistream.consumers.simple.SimpleConsumer` .. note:: Don't initialize this class directly, use the :meth:`SimpleProducer.new` method :param path_sink: the path of the file you want to write data to. """path_sink:Path=dataclasses.field(default=REQ)
[docs]@classmethoddefnew(cls,buffer:AbcBuffer,retry_config:RetryConfig,path_sink:Path,):""" Create a :class:`SimpleProducer` instance. :param record_class: the record class. :param path_sink: the path of the file you want to write data to. :param buffer: the buffer you want to use. :param retry_config: the retry configuration. """returncls(buffer=buffer,retry_config=retry_config,path_sink=path_sink,)
[docs]defsend(self,records:list[AbcRecord]):""" Send records to the sink, which is an append-only file """withself.path_sink.open("a")asf:forrecordinrecords:f.write(record.serialize()+"\n")