def add_record( self, sensor_name: str, from_datetime: datetime, to_datetime: datetime, extension: str, data: bytes, ): """Add the provided data to the tarfile, using associated metadata. If, despite included timestamps, several records end up having the exact same name, the last one will have priority when extracting the tarfile, but all of them will be stored in it anyway. :param sensor_name: slug label for the sensor :param from_datetime: start time of the recording :param to_datetime: end time of the recording :param extension: file extension, starting with a dot :param data: bytestring of audio/video/other data """ assert ( self._current_records_count or not self._current_start_time ) # INVARIANT of our system! assert isinstance(data, bytes), repr(data) # For now, only format supported assert extension.startswith("."), extension assert from_datetime <= to_datetime, (from_datetime, to_datetime) check_datetime_is_tz_aware(from_datetime) check_datetime_is_tz_aware(to_datetime) self._notify_aggregation_operation() filename = self._build_record_filename( sensor_name=sensor_name, from_datetime=from_datetime, to_datetime=to_datetime, extension=extension, ) logger.info("Adding record %r to tarfile builder" % filename) mtime = to_datetime.timestamp() member_metadata = dict(size=len(data), mtime=to_datetime) self._current_metadata["members"][ filename ] = member_metadata # Overridden if existing tarinfo = tarfile.TarInfo(filename) tarinfo.size = len(data) # this is crucial tarinfo.mtime = mtime self._current_tarfile.addfile(tarinfo, io.BytesIO(data)) self._current_records_count += 1
def test_check_datetime_is_tz_aware(): with pytest.raises(ValueError): check_datetime_is_tz_aware(datetime.now()) check_datetime_is_tz_aware(datetime.now(tz=timezone.utc))