diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2022-09-07 00:28:22 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2024-01-26 20:56:14 +0100 |
commit | ae8b069807f62792b5aca2806ca17046475e9e30 (patch) | |
tree | eff799e29161e3f1c8a8574467a32bb4490c406d /epubgrep.py | |
parent | 3b89f197710e6373d7736dc5463a44f4c31783db (diff) | |
download | epubgrep-ae8b069807f62792b5aca2806ca17046475e9e30.tar.gz |
Add author search.0.8.0
Diffstat (limited to 'epubgrep.py')
-rwxr-xr-x | epubgrep.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/epubgrep.py b/epubgrep.py index 8afbf54..18ab6e4 100755 --- a/epubgrep.py +++ b/epubgrep.py @@ -115,10 +115,33 @@ def _metadata_search(mdata: dict, sre: re.Pattern, fname: str, out += _colorize_found(tag, res, col) return out +def _author_search(mdata: dict, sre: re.Pattern, fname: str, + col: bool) -> str: + """ + Search through metadata, not text. + + :param: mdata: complete metadata to search through + :param: sre: re.Pattern to search + :param: fname: filename of the book + :param: col: should we colorize the output + """ + out = '' + title = '' + authors = mdata.get('authors') + + for auth in authors: + res = sre.search(auth) + if res: + if not title: + title = f'{fname}:' + out += ' ' + _colorize_found(auth, res, col) + + return title + out + def grep_book(filename: str, opts: argparse.Namespace, re_flags: int) -> Optional[str]: - assert os.path.isfile(filename), f'{filename} is not EPub file.' + assert os.path.isfile(filename), f'{filename} is not a file.' sought_RE = re.compile('(' + opts.pattern + ')', re_flags) count = 0 icount = 0 @@ -136,6 +159,9 @@ def grep_book(filename: str, opts: argparse.Namespace, book = zipfile.ZipFile(filename) printed_booktitle = False + if opts.author: + return _author_search(metadata, sought_RE, filename, opts.color) + if opts.metadata: return _metadata_search(metadata, sought_RE, filename, opts.color) @@ -177,6 +203,9 @@ def main(): parser = argparse.ArgumentParser(description='Grep through EPub book') parser.add_argument('pattern') parser.add_argument('files', nargs='+') + parser.add_argument('-a', '--author', + action='store_true', + help="prints titles with given author") parser.add_argument('-c', '--count', action='store_true', help="just counts of found patterns") |