datalogd.plugins.file_datasink module

class datalogd.plugins.file_datasink.FileDataSink(filename, filename_timestamp=False, mode='w', header='', terminator='n', flush_interval=10)[source]

Bases: datalogd.DataSink

Write data to a file.

By default, any existing contents of filename will be overwritten without prompting. To instead raise an error if the file exists, set the mode parameter to ‘x’. The contents of any existing file will be appended to by setting mode='a'.

To automatically prepend a date and time stamp to the given filename, set filename_timestamp=True. In general, this should create a unique filename and prevent overwriting when filename already exists.

The flush_interval parameter controls the behaviour of the file writes. It describes how often, in seconds, the operating system’s buffers should be flushed to disk, updating the file contents:

  • flush_interval > 0 causes the flush to occur at the given time interval, in seconds. More frequent flushes will keep the contents of the file updated, but put more strain on the machines I/O systems.
  • flush_interval = 0 will flush immediately after each receipt of data.
  • flush_interval < 0 will not automatically flush, leaving this to the operating system. The contents of the file may not update until the program closes.
  • flush_interval == None will perform a file open, write, and close operation on each receipt of data. This may be desired if the contents of the file should only contain the latest received data (and should be used in conjunction with the mode='w' parameter).
Parameters:
  • filename – File name to write data to.
  • filename_timestamp – Prepend a timestamp to the filename.
  • mode – Mode in which to open the file. One of ‘w’ (write), ‘a’ (append), ‘x’ (exclusive creation).
  • header – Header to write to file after plugin initialisation.
  • terminator – Separator written to file after each receipt of data.
  • flush_interval – Interval, in seconds, between flushes to disk.
close()[source]

Close the connection to the file.

receive(data)[source]

Accept data and write it out to the file.

The terminator specified in the constructor will be appended to the file after each call of this method.

Parameters:data – Data to write to file.