|
1 |
| -from typing import BinaryIO |
| 1 | +import sys |
| 2 | +from typing import IO, BinaryIO, List, Union |
2 | 3 |
|
3 | 4 | import click
|
4 |
| -from pymarc import MARCReader |
| 5 | +from pymarc import Any, MARCReader |
5 | 6 |
|
6 | 7 | from .color import color_field, color_record
|
7 | 8 | from .filter import Filter
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | @click.command(help="Find MARC records matching patterns in a file.")
|
11 | 12 | @click.help_option("-h", "--help")
|
12 |
| -@click.argument("file", type=click.File("rb"), default="-") |
| 13 | +@click.argument("files", type=click.File("rb"), nargs=-1) |
13 | 14 | @click.option("--count", "-c", help="Count matching records", is_flag=True)
|
14 | 15 | @click.option(
|
15 | 16 | "--include", "-i", help="Include matching records (repeatable)", multiple=True
|
|
22 | 23 | @click.option("--color", help="Colorize mnemonic MARC output", is_flag=True)
|
23 | 24 | @click.version_option(package_name="marcgrep", message="%(prog)s %(version)s")
|
24 | 25 | def main(
|
25 |
| - file: BinaryIO, |
| 26 | + files: List[BinaryIO], |
26 | 27 | color: bool,
|
27 | 28 | count: bool,
|
28 | 29 | include: list[str],
|
29 | 30 | exclude: list[str],
|
30 | 31 | fields: str,
|
31 | 32 | limit: int,
|
32 | 33 | ):
|
33 |
| - counter = 0 |
34 |
| - matched_records = 0 |
35 |
| - reader = MARCReader(file) |
| 34 | + # handle stdin if no files are provided |
| 35 | + if not files: |
| 36 | + files = [sys.stdin.buffer] |
36 | 37 |
|
37 |
| - # build a list of filters, start with exclusive because they rule out records quicker |
38 |
| - filters: list[Filter] = [Filter(pattern, inclusive=False) for pattern in exclude] |
39 |
| - filters.extend(Filter(pattern) for pattern in include) |
| 38 | + any_matches = False |
40 | 39 |
|
41 |
| - for record in reader: |
42 |
| - if record: |
43 |
| - counter += 1 |
44 |
| - if all(f.match(record) for f in filters): |
45 |
| - matched_records += 1 |
46 |
| - if not count: |
47 |
| - if fields: |
48 |
| - for f in record.get_fields(*fields.split(",")): |
| 40 | + for file in files: |
| 41 | + counter = 0 |
| 42 | + matched_records = 0 |
| 43 | + reader = MARCReader(file) |
| 44 | + |
| 45 | + # build a list of filters, start with exclusive because they rule out records quicker |
| 46 | + filters: list[Filter] = [ |
| 47 | + Filter(pattern, inclusive=False) for pattern in exclude |
| 48 | + ] |
| 49 | + filters.extend(Filter(pattern) for pattern in include) |
| 50 | + |
| 51 | + for record in reader: |
| 52 | + if record: |
| 53 | + counter += 1 |
| 54 | + if all(f.match(record) for f in filters): |
| 55 | + any_matches = True |
| 56 | + matched_records += 1 |
| 57 | + if not count: |
| 58 | + if fields: |
| 59 | + for f in record.get_fields(*fields.split(",")): |
| 60 | + if color: |
| 61 | + color_field(f) |
| 62 | + else: |
| 63 | + print(f) |
| 64 | + else: |
49 | 65 | if color:
|
50 |
| - color_field(f) |
| 66 | + color_record(record) |
51 | 67 | else:
|
52 |
| - print(f) |
53 |
| - else: |
54 |
| - if color: |
55 |
| - color_record(record) |
56 |
| - else: |
57 |
| - print(record) |
58 |
| - if limit and counter >= limit: |
59 |
| - break |
| 68 | + print(record) |
| 69 | + if limit and counter >= limit: |
| 70 | + break |
60 | 71 |
|
61 |
| - if count: |
62 |
| - print(matched_records) |
| 72 | + if count: |
| 73 | + print(f"{file.name}: {matched_records}") |
63 | 74 |
|
64 | 75 | # non-zero exit if no records match
|
65 |
| - return exit(0 if matched_records else 1) |
| 76 | + return exit(0 if any_matches else 1) |
66 | 77 |
|
67 | 78 |
|
68 | 79 | if __name__ == "__main__":
|
|
0 commit comments