Add some prototypical validation code.
This commit is contained in:
parent
8763cf6c6c
commit
fc07652d16
1 changed files with 19 additions and 0 deletions
19
epubfile.py
19
epubfile.py
|
@ -262,6 +262,9 @@ class EpubfileException(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.error_message
|
return self.error_message
|
||||||
|
|
||||||
|
class InvalidEpub(EpubfileException):
|
||||||
|
error_message = '{} is invalid: {}'
|
||||||
|
|
||||||
class FileExists(EpubfileException):
|
class FileExists(EpubfileException):
|
||||||
error_message = 'There is already a file at {}.'
|
error_message = 'There is already a file at {}.'
|
||||||
|
|
||||||
|
@ -302,6 +305,21 @@ class Epub:
|
||||||
if self.opf.manifest.find('item', {'id': id}):
|
if self.opf.manifest.find('item', {'id': id}):
|
||||||
raise IDExists(id)
|
raise IDExists(id)
|
||||||
|
|
||||||
|
# VALIDATION
|
||||||
|
############################################################################
|
||||||
|
def auto_correct_and_validate(self):
|
||||||
|
# Ensure we have a mimetype file.
|
||||||
|
mimetype_file = self.root_directory.with_child('mimetype')
|
||||||
|
if not mimetype_file.exists:
|
||||||
|
with open(mimetype_file.absolute_path, 'w', encoding='utf-8') as handle:
|
||||||
|
handle.write(MIMETYPE_FILE_TEMPLATE)
|
||||||
|
|
||||||
|
# Assert that all manifest items exist on disk.
|
||||||
|
for item in self.get_manifest_items(soup=True):
|
||||||
|
filepath = self.get_filepath(item['id'])
|
||||||
|
if not filepath.exists:
|
||||||
|
raise InvalidEpub(self._original_path, f'Manifest item {item["id"]} = {item["href"]} does not exist.')
|
||||||
|
|
||||||
# LOADING AND SAVING
|
# LOADING AND SAVING
|
||||||
############################################################################
|
############################################################################
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -331,6 +349,7 @@ class Epub:
|
||||||
|
|
||||||
def save(self, epub_filepath):
|
def save(self, epub_filepath):
|
||||||
self.write_opf()
|
self.write_opf()
|
||||||
|
self.auto_correct_and_validate()
|
||||||
compress_epub(self.root_directory, epub_filepath)
|
compress_epub(self.root_directory, epub_filepath)
|
||||||
|
|
||||||
# CONTAINER & OPF
|
# CONTAINER & OPF
|
||||||
|
|
Loading…
Reference in a new issue