csv.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Use of this source code is governed by a BSD-style
  2. # license that can be found in the LICENSE file.
  3. # Copyright 2019 The OSArchiver Authors. All rights reserved.
  4. """
  5. Implementation of CSV writer (SQL data -> CSV file)
  6. """
  7. import logging
  8. import csv
  9. from osarchiver.destination.file.base import Formatter
  10. class Csv(Formatter):
  11. """
  12. The class implement a formatter of CSV type which is able to convert a list
  13. of dict of SQL data into one CSV file
  14. """
  15. def write(self, database=None, table=None, data=None):
  16. """
  17. The write method which should be implemented because of ineherited
  18. Formatter class.
  19. The name of the file is of the form <database>.<table>.csv
  20. """
  21. destination_file = '{directory}/{db}.{table}.csv'.format(
  22. directory=self.directory, db=database, table=table,
  23. )
  24. key = '{db}.{table}'.format(db=database, table=table)
  25. writer = None
  26. if key in self.handlers:
  27. writer = self.handlers[key]['csv_writer']
  28. else:
  29. self.handlers[key] = {}
  30. self.handlers[key]['file'] = destination_file
  31. self.handlers[key]['fh'] = open(
  32. destination_file, 'w', encoding='utf-8')
  33. self.handlers[key]['csv_writer'] = \
  34. csv.DictWriter(
  35. self.handlers[key]['fh'],
  36. fieldnames=[h for h in data[0].keys()])
  37. writer = self.handlers[key]['csv_writer']
  38. if not self.dry_run:
  39. logging.debug("It seems this is the first write set, adding "
  40. " headers to CSV file")
  41. writer.writeheader()
  42. else:
  43. logging.debug(
  44. "[DRY RUN] headers not written in %s", destination_file)
  45. logging.info("%s formatter: writing %s line in %s", self.name,
  46. len(data), destination_file)
  47. if not self.dry_run:
  48. writer.writerows(data)
  49. else:
  50. logging.debug("[DRY RUN] No data written in %s", destination_file)