from csv import DictReader

from harvest.exception import NotSupportCharCodeError, MaxImportCountExceededError

MAX_IMPORT_COUNT=300

class CsvDriver():
  
  def __init__(self, filename=None):
    if filename:
      self.items = self.open_csv(filename)

  def open_csv(self, filename):
    try:
      with open(filename, "r", encoding="utf_8_sig") as csv_file:
        lines = DictReader(csv_file, delimiter=",", doublequote=True, quotechar='"', skipinitialspace=True)
        ret = [ line for line in lines ]
        if len(ret) > MAX_IMPORT_COUNT:
          raise MaxImportCountExceededError()
        return ret

    except UnicodeDecodeError as e:
      raise  NotSupportCharCodeError()
