'Update stats in the CSV output file.'

def update(self, stats):

    all_stats = stats.getAllExports()
    plugins = stats.getAllPlugins()
    if self.first_line:
        csv_header = ['timestamp']
    csv_data = [time.strftime('%Y-%m-%d    %H:%M:%S')]
    for (i, plugin) in enumerate(plugins):
        if (plugin in self.plugins_to_export()):
            if isinstance(all_stats[i], list):
                for stat in all_stats[i]:
                    if self.first_line:
                        csv_header += ('{}_{}_{}'.format(plugin, self.get_item_key(stat), item) for item in stat)
                    csv_data += itervalues(stat)
            elif isinstance(all_stats[i], dict):
                if self.first_line:
                    fieldnames = iterkeys(all_stats[i])
                    csv_header += ('{}_{}'.format(plugin, fieldname) for fieldname in fieldnames)
                csv_data += itervalues(all_stats[i])
    if self.first_line:
        self.writer.writerow(csv_header)
        self.first_line = False
    self.writer.writerow(csv_data)
    self.csv_file.flush()
