You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
874 B

  1. from typing import List
  2. class FileParseException(Exception):
  3. def __init__(self, filename: str, line_num: int, message: str):
  4. self.filename = filename
  5. self.line_num = line_num
  6. self.message = message
  7. def __str__(self):
  8. return f'ParseError: You cannot parse line {self.line_num} in {self.filename}: {self.message}'
  9. def dict(self):
  10. return {
  11. 'filename': self.filename,
  12. 'line': self.line_num,
  13. 'message': self.message
  14. }
  15. class FileParseExceptions(Exception):
  16. def __init__(self, exceptions: List[FileParseException]):
  17. self.exceptions = exceptions
  18. def __str__(self) -> str:
  19. return f'ParseErrors: you failed to parse {len(self.exceptions)} lines.'
  20. def __iter__(self) -> FileParseException:
  21. for e in self.exceptions:
  22. yield e.dict()