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.

53 lines
1.6 KiB

2 years ago
2 years ago
  1. from typing import Any, Dict
  2. class FileImportException(Exception):
  3. def dict(self) -> Dict[str, Any]:
  4. raise NotImplementedError()
  5. class FileParseException(FileImportException):
  6. def __init__(self, filename: str, line_num: int, message: str):
  7. self.filename = filename
  8. self.line_num = line_num
  9. self.message = message
  10. def __str__(self):
  11. return f"ParseError: You cannot parse line {self.line_num} in {self.filename}: {self.message}"
  12. def dict(self):
  13. return {"filename": self.filename, "line": self.line_num, "message": self.message}
  14. class MaximumFileSizeException(FileImportException):
  15. def __init__(self, filename: str, max_size: int):
  16. self.filename = filename
  17. self.max_size = max_size
  18. def __str__(self):
  19. return f"The maximum file size that can be uploaded is {self.max_size/1024/1024} MB"
  20. def dict(self):
  21. return {"filename": self.filename, "line": -1, "message": str(self)}
  22. class FileTypeException(FileImportException):
  23. def __init__(self, filename: str, filetype: str, allowed_types=None):
  24. self.filename = filename
  25. self.filetype = filetype
  26. self.allowed_types = allowed_types
  27. def __str__(self):
  28. return f"The file type {self.filetype} is unexpected. Expected: {self.allowed_types}"
  29. def dict(self):
  30. return {"filename": self.filename, "line": -1, "message": str(self)}
  31. class FileFormatException(FileImportException):
  32. def __init__(self, file_format: str):
  33. self.file_format = file_format
  34. def dict(self):
  35. message = f"Unknown file format: {self.file_format}"
  36. return {"message": message}