aboutsummaryrefslogtreecommitdiffstats
path: root/epubgrep.py
diff options
context:
space:
mode:
Diffstat (limited to 'epubgrep.py')
-rwxr-xr-xepubgrep.py31
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")