aboutsummaryrefslogtreecommitdiffstats
path: root/epubgrep.py
diff options
context:
space:
mode:
Diffstat (limited to 'epubgrep.py')
-rwxr-xr-xepubgrep.py34
1 files changed, 17 insertions, 17 deletions
diff --git a/epubgrep.py b/epubgrep.py
index e3051bf..578c20b 100755
--- a/epubgrep.py
+++ b/epubgrep.py
@@ -8,19 +8,6 @@ from typing import Any, Dict, List, Optional, Tuple
import epub_meta
-parser = argparse.ArgumentParser(description='Grep through EPub book')
-parser.add_argument('pattern')
-parser.add_argument('filename')
-parser.add_argument('-i', '--ignore-case',
- action='store_true',
- help="make search case insensitive")
-args = parser.parse_args()
-
-search_flags = 0
-if args.ignore_case:
- search_flags |= re.I
-
-
def get_chapter_title(mdata:List[Dict[str, Any]], fname:str) -> Optional[Tuple[str, int]]:
found_list = [(x['title'], x['index']) for x in mdata if x['src'] == fname]
if len(found_list) > 0:
@@ -30,7 +17,8 @@ def get_chapter_title(mdata:List[Dict[str, Any]], fname:str) -> Optional[Tuple[s
return ('Unknown', 0)
-def grep_book(filename:str, pattern:str, flags):
+def grep_book(filename:str, pattern:str, flags:int):
+ assert os.path.isfile(filename), "{} is not EPub file.".format(filename)
sought_RE = re.compile(pattern, flags)
metadata = epub_meta.get_epub_metadata(filename)
@@ -49,6 +37,18 @@ def grep_book(filename:str, pattern:str, flags):
printed_title = True
print(decoded_line)
-assert os.path.isfile(args.filename), "{} is not EPub file.".format(args.filename)
-book_fname = os.path.realpath(args.filename)
-grep_book(book_fname, args.pattern, search_flags)
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description='Grep through EPub book')
+ parser.add_argument('pattern')
+ parser.add_argument('filename')
+ parser.add_argument('-i', '--ignore-case',
+ action='store_true',
+ help="make search case insensitive")
+ args = parser.parse_args()
+
+ search_flags = 0
+ if args.ignore_case:
+ search_flags |= re.I
+
+ book_fname = os.path.realpath(args.filename)
+ grep_book(book_fname, args.pattern, search_flags)