Skip to content

Commit

Permalink
add fault tolerance, don't stop running if exception occurs extractin…
Browse files Browse the repository at this point in the history
…g inner zip
  • Loading branch information
ConorSheehan1 committed Mar 21, 2017
1 parent 02a13a8 commit dd14410
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@


def unzip(path, rm_zips=True):
errors = []
# iterate over sub directories of path
for dir in glob.glob(path + "*/"):

# unzip all files in subdirectories
for file in glob.glob(dir + "*.zip"):
# extract zip to path
zipfile.ZipFile(file).extractall(dir)
try:
zipfile.ZipFile(file).extractall(dir)
except:
errors.append(os.path.basename(file))

# remove zip after extraction
if rm_zips:
os.remove(file)
return errors


def remove_empty_folders(path):
Expand Down Expand Up @@ -46,6 +51,8 @@ def unzip_outer(zip_path, names):
# extract file to folder the zipfile is currently in
archive.extract(file, os.path.dirname(zip_path))

archive.close()


def missing_names(path, names):
'''
Expand Down
9 changes: 7 additions & 2 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ def do_work(self):
main.unzip_outer(zip_path, names)

# get directory of zipfile, unzip and move files in subdirectories
main.unzip(cwd, rm_zips=self.rm_zips.get())
extraction_errors = main.unzip(cwd, rm_zips=self.rm_zips.get())
if extraction_errors:
self.error_label.configure(text=self.error_label.cget("text") +
"Exception extracting: {}\n".format(extraction_errors))

missing_names = main.missing_names(cwd, names)
if missing_names:
Expand All @@ -188,6 +191,8 @@ def do_work(self):
self.completion_label.configure(text="Finished!")
print("Finished!")
except:
self.error_label.configure(text=self.error_label.cget("text") + "Exception extracting files\n")
# catch exception to allow prompt within ui, then re-raise exception
self.error_label.configure(text=self.error_label.cget("text") + "Exception extracting files. Check the console\n")
raise

App()

0 comments on commit dd14410

Please sign in to comment.