import io
import logging
from backports import csv
from normality import safe_filename

from ingestors.support.temp import TempFileSupport
from ingestors.util import join_path

log = logging.getLogger(__name__)


class CSVEmitterSupport(TempFileSupport):
    """Generate a CSV file from a generator of rows."""

    def csv_child_iter(self, iter, name):
        with self.create_temp_dir() as temp_dir:
            out_path = safe_filename(name, extension='csv')
            out_path = join_path(temp_dir, out_path)
            with io.open(out_path, 'w', newline='', encoding='utf-8') as fh:
                writer = csv.writer(fh, quoting=csv.QUOTE_ALL)
                writer.writerows(iter)

            child_id = join_path(self.result.id, name)
            self.manager.handle_child(self.result, out_path,
                                      id=child_id,
                                      title=name,
                                      mime_type='text/csv')

@@ -412,20 +412,20 @@
     out_
-path
+name
  = safe_
@@ -507,13 +507,39 @@
 out_
-path)
+name)%0A            row_count = 0
 %0A   
@@ -695,29 +695,179 @@
     
-writer.writerows(iter
+for row in iter:%0A                    writer.writerow(row)%0A                    row_count += 1%0A%0A            log.info(%22Generated %5B%25s%5D: %25s, %25s rows%22, name, out_name, row_count
 )%0A%0A 

output row number