diff options
-rwxr-xr-x | epubgrep.py | 31 | ||||
-rw-r--r-- | setup.py | 2 |
2 files changed, 31 insertions, 2 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") @@ -4,7 +4,7 @@ from setuptools import setup, find_packages setup( name="epubgrep", - version="0.7.1", + version="0.8.0", description='Grep through EPub files', author=u'Matěj Cepl', author_email='mcepl@cepl.eu', |