Reader.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from jinja2 import Environment, Template
  2. import csv
  3. def split_at(value, sep, idx):
  4. try:
  5. return value.split(sep)[idx]
  6. except Exception:
  7. return ''
  8. class Reader:
  9. """
  10. Reader class to handle reading and processing of CSV and JSON files for mailcow.
  11. """
  12. def __init__(self):
  13. pass
  14. def read_csv(self, file_path, delimiter=',', encoding='iso-8859-1'):
  15. """
  16. Read a CSV file and return a list of dictionaries.
  17. Each dictionary represents a row in the CSV file.
  18. :param file_path: Path to the CSV file.
  19. :param delimiter: Delimiter used in the CSV file (default: ',').
  20. """
  21. with open(file_path, mode='r', encoding=encoding) as file:
  22. reader = csv.DictReader(file, delimiter=delimiter)
  23. reader.fieldnames = [h.replace(" ", "_") if h else h for h in reader.fieldnames]
  24. return [row for row in reader]
  25. def map_csv_data(self, data, mapping_file_path, encoding='iso-8859-1'):
  26. """
  27. Map CSV data to a specific structure based on the provided Jinja2 template file.
  28. :param data: List of dictionaries representing CSV rows.
  29. :param mapping_file_path: Path to the Jinja2 template file.
  30. :return: List of dictionaries with mapped data.
  31. """
  32. with open(mapping_file_path, 'r', encoding=encoding) as tpl_file:
  33. template_content = tpl_file.read()
  34. env = Environment()
  35. env.filters['split_at'] = split_at
  36. template = env.from_string(template_content)
  37. mapped_data = []
  38. for row in data:
  39. rendered = template.render(**row)
  40. try:
  41. mapped_row = eval(rendered)
  42. except Exception:
  43. mapped_row = rendered
  44. mapped_data.append(mapped_row)
  45. return mapped_data