Coverage for src/epublib/exceptions.py: 92%

12 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-18 16:07 -0300

1import warnings 

2 

3 

4class EPUBWarning(Warning): 

5 """Base warning for EPUBLib.""" 

6 

7 

8class EPUBError(Exception): 

9 """Base error for EPUBLib.""" 

10 

11 @classmethod 

12 def missing_ncx(cls, calling: object, method: str, arg: str = "reset_ncx"): 

13 return cls( 

14 f"'{calling.__class__.__name__}.{method}' received '{arg}=True' " 

15 "but there is no NCX file in the book. Create the NCX " 

16 f"file with '{calling.__class__.__name__}.create_ncx' or use " 

17 f"a falsy value for the '{arg}' parameter" 

18 ) 

19 

20 

21class NotEPUBError(EPUBError): 

22 """The file is not an EPUB file.""" 

23 

24 def __init__(self, msg: str, *args: object) -> None: 

25 super().__init__(f"File is not epub: {msg}", *args) 

26 

27 

28class ClosedEPUBError(EPUBError): 

29 """An operation was attempted on a closed EPUB file.""" 

30 

31 

32def warn(warning: str, cls: type[EPUBWarning] = EPUBWarning): 

33 warnings.warn(warning, cls)