Coverage for lunar_toolkit/cli/query.py: 26%

30 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-19 02:58 +0000

1"""query - print metadata for LROC NAC images from the Parquet index.""" 

2 

3import argparse 

4import sys 

5from pathlib import Path 

6 

7COMMAND = "query" 

8 

9 

10def add_parser(sub: argparse._SubParsersAction) -> None: 

11 p = sub.add_parser( 

12 COMMAND, 

13 help="Print metadata for LROC NAC images from the Parquet index.", 

14 formatter_class=argparse.RawDescriptionHelpFormatter, 

15 epilog=""" 

16Examples 

17-------- 

18 lunar query M1096757719LE 

19 lunar query M1096757719LE M1096757719RE 

20 lunar query --from-file ids.txt 

21 lunar query /path/to/M1096757719LE.IMG 

22""", 

23 ) 

24 p.add_argument( 

25 "sources", 

26 nargs="*", 

27 metavar="SOURCE", 

28 help="Product IDs, local .IMG paths, or a text file of IDs.", 

29 ) 

30 p.add_argument( 

31 "--from-file", 

32 metavar="FILE", 

33 help="Text file of product IDs or paths, one per line.", 

34 ) 

35 p.add_argument( 

36 "--data-dir", 

37 type=Path, 

38 default=None, 

39 metavar="DIR", 

40 help="Override Parquet data directory.", 

41 ) 

42 

43 

44def run(args: argparse.Namespace) -> int: 

45 from ..lroc.nac import NacImageCollection 

46 from ._utils import extract_product_id, load_sources 

47 

48 sources = load_sources(args) 

49 if not sources: 

50 print("No sources provided. Pass product IDs or --from-file.", file=sys.stderr) 

51 return 1 

52 

53 product_ids = [extract_product_id(s) for s in sources] 

54 

55 collection = NacImageCollection.from_sources(product_ids, data_dir=args.data_dir) 

56 found = {img.product_id: img for img in collection} 

57 

58 for pid in product_ids: 

59 img = found.get(pid) 

60 print(pid) 

61 if img is None: 

62 print(" (not found in index)") 

63 else: 

64 items = img.metadata_items() 

65 width = max((len(label) for label, _ in items), default=30) + 2 

66 for label, value in items: 

67 print(f" {label:<{width}} {value}") 

68 print() 

69 

70 return 0