From 29eea3855c39da7b55e42e8a915ad6e1649adabf Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 6 Aug 2019 23:37:14 -0700 Subject: [PATCH] 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. --- epubfile.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/epubfile.py b/epubfile.py index a5d7008..616e022 100644 --- a/epubfile.py +++ b/epubfile.py @@ -294,6 +294,10 @@ class Epub: existing = filepath.relative_to(self.opf_filepath.parent) raise FileExists(existing) + def assert_id_not_exists(self, id): + if self.opf.manifest.find('item', {'id': id}): + raise IDExists(id) + # LOADING AND SAVING ############################################################################ @classmethod @@ -379,8 +383,7 @@ class Epub: # FILE OPERATIONS ############################################################################ def add_file(self, id, basename, content): - if self.opf.manifest.find('item', {'id': id}): - raise IDExists(id) + self.assert_id_not_exists(id) basename = os.path.basename(basename) mime = get_mimetype_for_basename(basename)