diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2018-11-30 12:06:30 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-11-30 12:06:30 +0100 |
commit | dc8c9a24cac3725613ddd76eb4728f8f361bb48d (patch) | |
tree | 1a721522085ef59d8a9dec43df7ed66edd1ce682 /epubgrep.py | |
parent | 0c6d0acdf3b93eee221a3093ce22c0c76b0b2127 (diff) | |
download | epubgrep-dc8c9a24cac3725613ddd76eb4728f8f361bb48d.tar.gz |
Add minimal setup.py and test0.2.0
Diffstat (limited to 'epubgrep.py')
-rwxr-xr-x | epubgrep.py | 34 |
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) |