From a0da6a4eb3c8ef5111531ffa81f911924ba2fd3d Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 11 Oct 2019 08:53:55 +0200 Subject: More exception handling. --- epubgrep.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/epubgrep.py b/epubgrep.py index 74a7c8a..b62c67a 100755 --- a/epubgrep.py +++ b/epubgrep.py @@ -3,6 +3,7 @@ import argparse import logging import os.path import re +import sys import zipfile from typing import Any, Dict, List, Optional, Tuple @@ -31,7 +32,10 @@ def grep_book(filename: str, pattern: str, flags: int, counting: bool=False, col mline = flags & re.M == re.M - metadata = epub_meta.get_epub_metadata(filename) + try: + metadata = epub_meta.get_epub_metadata(filename) + except epub_meta.EPubException as ex: + log.exception('Failed to open {}'.format(filename)) book = zipfile.ZipFile(filename) printed_booktitle = False @@ -110,4 +114,7 @@ if __name__ == "__main__": for filename in args.files: book_fname = os.path.realpath(filename) - grep_book(book_fname, args.pattern, search_flags, args.count, args.color) + try: + grep_book(book_fname, args.pattern, search_flags, args.count, args.color) + except BrokenPipeError: + sys.exit() -- cgit