| 6 | log = logging.getLogger(__name__) |
| 7 | |
| 8 | class CSVParser(ABC): |
| 9 | |
| 10 | def parse(self, filename): |
| 11 | records = [] |
| 12 | with open(filename) as f: |
| 13 | rows = csv.reader(f) |
| 14 | headers = next(rows) |
| 15 | for rowno, row in enumerate(rows, start=1): |
| 16 | try: |
| 17 | record = self.make_record(headers, row) |
| 18 | records.append(record) |
| 19 | except ValueError as e: |
| 20 | log.warning('Row %d: Bad row: %s', rowno, row) |
| 21 | log.debug('Row %d: Reason: %s', rowno, e) |
| 22 | |
| 23 | return records |
| 24 | |
| 25 | @abstractmethod |
| 26 | def make_record(self, headers, row): |
| 27 | raise NotImplementedError() |
| 28 | |
| 29 | class DictCSVParser(CSVParser): |
| 30 | def __init__(self, types): |
nothing calls this directly
no outgoing calls
no test coverage detected