|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from elsevier_coordinate_extraction.settings import get_settings |
| 9 | + |
| 10 | +from .inputs import ( |
| 11 | + parse_dois, |
| 12 | + parse_jsonl, |
| 13 | + parse_pmids, |
| 14 | + validate_records, |
| 15 | +) |
| 16 | +from .orchestrator import process_articles |
| 17 | + |
| 18 | + |
| 19 | +DESCRIPTION = """ |
| 20 | +Download Elsevier articles, extract text/tables/coordinates, |
| 21 | +and write structured outputs. |
| 22 | +""" |
| 23 | + |
| 24 | +EXAMPLES = """Examples: |
| 25 | + # Download by PubMed IDs inline |
| 26 | + elsevier-extract --pmids 12345678,23456789 --output-dir ./results |
| 27 | +
|
| 28 | + # Download by DOIs from file |
| 29 | + elsevier-extract --dois dois.txt --output-dir ./results |
| 30 | +
|
| 31 | + # Batch from JSONL |
| 32 | + elsevier-extract --jsonl identifiers.jsonl --continue-on-error |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +def create_parser() -> argparse.ArgumentParser: |
| 37 | + parser = argparse.ArgumentParser( |
| 38 | + prog="elsevier-extract", |
| 39 | + description=DESCRIPTION, |
| 40 | + epilog=EXAMPLES, |
| 41 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 42 | + ) |
| 43 | + input_group = parser.add_mutually_exclusive_group(required=True) |
| 44 | + input_group.add_argument( |
| 45 | + "--pmids", type=str, help="Comma-separated PMIDs or file path" |
| 46 | + ) |
| 47 | + input_group.add_argument( |
| 48 | + "--dois", type=str, help="Comma-separated DOIs or file path" |
| 49 | + ) |
| 50 | + input_group.add_argument( |
| 51 | + "--jsonl", type=Path, help="JSONL file with doi/pmid records" |
| 52 | + ) |
| 53 | + |
| 54 | + parser.add_argument( |
| 55 | + "--output-dir", |
| 56 | + type=Path, |
| 57 | + default=Path("./elsevier_output"), |
| 58 | + help="Base directory for article outputs", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--skip-xml", |
| 62 | + action="store_true", |
| 63 | + help="Skip writing raw XML", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "--skip-text", |
| 67 | + action="store_true", |
| 68 | + help="Skip writing extracted text", |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + "--skip-tables", |
| 72 | + action="store_true", |
| 73 | + help="Skip writing extracted tables", |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--skip-coordinates", |
| 77 | + action="store_true", |
| 78 | + help="Skip writing coordinates JSON", |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--continue-on-error", |
| 82 | + action="store_true", |
| 83 | + help="Keep going after failures", |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--max-workers", |
| 87 | + type=int, |
| 88 | + help="Override extraction worker count", |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "--no-cache", |
| 92 | + action="store_true", |
| 93 | + help="Disable response caching", |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + "--verbose", |
| 97 | + "-v", |
| 98 | + action="store_true", |
| 99 | + help="Print progress messages", |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + "--quiet", |
| 103 | + "-q", |
| 104 | + action="store_true", |
| 105 | + help="Minimal console output", |
| 106 | + ) |
| 107 | + return parser |
| 108 | + |
| 109 | + |
| 110 | +def gather_records(args: argparse.Namespace) -> list[dict[str, str]]: |
| 111 | + if args.pmids: |
| 112 | + return parse_pmids(args.pmids) |
| 113 | + if args.dois: |
| 114 | + return parse_dois(args.dois) |
| 115 | + if args.jsonl: |
| 116 | + return parse_jsonl(args.jsonl) |
| 117 | + return [] |
| 118 | + |
| 119 | + |
| 120 | +async def async_main(args: argparse.Namespace) -> int: |
| 121 | + try: |
| 122 | + records = validate_records(gather_records(args)) |
| 123 | + except Exception as exc: |
| 124 | + print(f"Input error: {exc}", file=sys.stderr) |
| 125 | + return 1 |
| 126 | + |
| 127 | + if not records: |
| 128 | + print("No identifiers were provided.", file=sys.stderr) |
| 129 | + return 1 |
| 130 | + |
| 131 | + settings = get_settings() |
| 132 | + if args.max_workers: |
| 133 | + settings = settings.__class__( |
| 134 | + **{ |
| 135 | + **settings.__dict__, |
| 136 | + "extraction_workers": args.max_workers, |
| 137 | + } |
| 138 | + ) |
| 139 | + |
| 140 | + if not args.quiet: |
| 141 | + print(f"Processing {len(records)} article(s)...") |
| 142 | + |
| 143 | + try: |
| 144 | + stats = await process_articles( |
| 145 | + records, |
| 146 | + args.output_dir, |
| 147 | + settings=settings, |
| 148 | + skip_xml=args.skip_xml, |
| 149 | + skip_text=args.skip_text, |
| 150 | + skip_tables=args.skip_tables, |
| 151 | + skip_coordinates=args.skip_coordinates, |
| 152 | + continue_on_error=args.continue_on_error, |
| 153 | + use_cache=not args.no_cache, |
| 154 | + verbose=args.verbose, |
| 155 | + ) |
| 156 | + except Exception as exc: |
| 157 | + print(f"Processing failed: {exc}", file=sys.stderr) |
| 158 | + return 1 |
| 159 | + |
| 160 | + if not args.quiet: |
| 161 | + summary = ( |
| 162 | + "\nSummary: success=" |
| 163 | + f"{stats['success']} failed={stats['failed']} " |
| 164 | + f"skipped={stats['skipped']}" |
| 165 | + ) |
| 166 | + print(summary) |
| 167 | + |
| 168 | + return 0 if stats["failed"] == 0 else 1 |
| 169 | + |
| 170 | + |
| 171 | +def main() -> int: |
| 172 | + parser = create_parser() |
| 173 | + args = parser.parse_args() |
| 174 | + try: |
| 175 | + return asyncio.run(async_main(args)) |
| 176 | + except KeyboardInterrupt: |
| 177 | + print("\nInterrupted", file=sys.stderr) |
| 178 | + return 130 |
0 commit comments