Add method assert_id_not_exists for pre-creation checks.

At the moment it's only used once but it looks nice to have it
paired with the other assert also used in the add_file method.
This commit is contained in:
Ethan Dalool 2019-08-06 23:37:14 -07:00
parent 5ba482753c
commit 29eea3855c

View file

@ -294,6 +294,10 @@ class Epub:
existing = filepath.relative_to(self.opf_filepath.parent) existing = filepath.relative_to(self.opf_filepath.parent)
raise FileExists(existing) raise FileExists(existing)
def assert_id_not_exists(self, id):
if self.opf.manifest.find('item', {'id': id}):
raise IDExists(id)
# LOADING AND SAVING # LOADING AND SAVING
############################################################################ ############################################################################
@classmethod @classmethod
@ -379,8 +383,7 @@ class Epub:
# FILE OPERATIONS # FILE OPERATIONS
############################################################################ ############################################################################
def add_file(self, id, basename, content): def add_file(self, id, basename, content):
if self.opf.manifest.find('item', {'id': id}): self.assert_id_not_exists(id)
raise IDExists(id)
basename = os.path.basename(basename) basename = os.path.basename(basename)
mime = get_mimetype_for_basename(basename) mime = get_mimetype_for_basename(basename)