|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# ------------------------- |
| 9 | +# CONFIG |
| 10 | +# ------------------------- |
| 11 | + |
| 12 | +logging.basicConfig( |
| 13 | + level=logging.INFO, |
| 14 | + format="%(levelname)s: %(message)s" |
| 15 | +) |
| 16 | + |
| 17 | +ADDRESS_REGEX = re.compile( |
| 18 | + r'ADDRESS_SHC_[A-Fa-f0-9]{8}_0x[A-Fa-f0-9]{8}' |
| 19 | +) |
| 20 | +TXT_LINE_REGEX = re.compile( |
| 21 | + r'^\s*(ADDRESS_SHC_[A-Fa-f0-9]{8}_0x[A-Fa-f0-9]{8})\s*\|\s*([\d]*\.?[\d]+)\s*%\s*\|\s*(.*)\s*$' |
| 22 | +) |
| 23 | + |
| 24 | +# ------------------------- |
| 25 | +# FUNCTIONS |
| 26 | +# ------------------------- |
| 27 | + |
| 28 | +def parse_args(): |
| 29 | + parser = argparse.ArgumentParser(description="Process a address header file and generate or update a status file.") |
| 30 | + parser.add_argument("file", help="Header file to process") |
| 31 | + parser.add_argument( |
| 32 | + "folder", |
| 33 | + nargs="?", |
| 34 | + default=None, |
| 35 | + help="Optional folder (defaults to script directory)" |
| 36 | + ) |
| 37 | + return parser.parse_args() |
| 38 | + |
| 39 | + |
| 40 | +def resolve_folder(folder_arg): |
| 41 | + if folder_arg: |
| 42 | + return Path(folder_arg) |
| 43 | + return Path(__file__).parent |
| 44 | + |
| 45 | + |
| 46 | +def read_header_addresses(file_path): |
| 47 | + if not file_path.exists(): |
| 48 | + logging.error(f"Header file not found: {file_path}") |
| 49 | + return [] |
| 50 | + |
| 51 | + content = file_path.read_text() |
| 52 | + matches = ADDRESS_REGEX.findall(content) |
| 53 | + |
| 54 | + logging.info(f"Found {len(matches)} addresses in header.") |
| 55 | + return matches |
| 56 | + |
| 57 | + |
| 58 | +def read_existing_txt(txt_path): |
| 59 | + data_map = {} |
| 60 | + |
| 61 | + if not txt_path.exists(): |
| 62 | + logging.info(f"No existing txt file found: {txt_path}") |
| 63 | + return data_map |
| 64 | + |
| 65 | + logging.info(f"Reading existing txt file: {txt_path}") |
| 66 | + |
| 67 | + with txt_path.open("r", encoding="utf-8") as f: |
| 68 | + for i, line in enumerate(f, 1): |
| 69 | + line = line.rstrip("\n") |
| 70 | + |
| 71 | + match = TXT_LINE_REGEX.match(line) |
| 72 | + if not match: |
| 73 | + logging.warning(f"Line {i} malformed: {line}") |
| 74 | + continue |
| 75 | + |
| 76 | + name, percent, comment = match.groups() |
| 77 | + |
| 78 | + if name in data_map: |
| 79 | + logging.warning(f"Duplicate entry ignored at line {i}: {name}") |
| 80 | + continue |
| 81 | + |
| 82 | + data_map[name] = (percent, comment) |
| 83 | + |
| 84 | + logging.info(f"Loaded {len(data_map)} existing entries.") |
| 85 | + return data_map |
| 86 | + |
| 87 | + |
| 88 | +def format_line(addr, percent, comment): |
| 89 | + return f"{addr} | {percent}% | {comment}" |
| 90 | + |
| 91 | + |
| 92 | +def write_output(txt_path, addresses, data_map): |
| 93 | + # Ensure directory exists |
| 94 | + if not txt_path.parent.exists(): |
| 95 | + logging.info(f"Creating folder: {txt_path.parent}") |
| 96 | + txt_path.parent.mkdir(parents=True, exist_ok=True) |
| 97 | + |
| 98 | + logging.info(f"Writing output to: {txt_path}") |
| 99 | + |
| 100 | + with txt_path.open("w", encoding="utf-8") as f: |
| 101 | + for addr in addresses: |
| 102 | + if addr in data_map: |
| 103 | + percent, comment = data_map.pop(addr) |
| 104 | + else: |
| 105 | + percent = "0.0" |
| 106 | + comment = "Pending" |
| 107 | + |
| 108 | + # writing |
| 109 | + f.write(format_line(addr, percent, comment) + "\n") |
| 110 | + |
| 111 | + # logging leftovers |
| 112 | + for leftover, (percent, comment) in data_map.items(): |
| 113 | + logging.warning( |
| 114 | + f"Unused entry in txt (not in header): {format_line(leftover, percent, comment)}" |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +# ------------------------- |
| 119 | +# MAIN |
| 120 | +# ------------------------- |
| 121 | + |
| 122 | +def main(): |
| 123 | + args = parse_args() |
| 124 | + |
| 125 | + header_path = Path(args.file) |
| 126 | + folder_path = resolve_folder(args.folder) |
| 127 | + |
| 128 | + logging.info(f"Using folder: {folder_path}") |
| 129 | + |
| 130 | + addresses = read_header_addresses(header_path) |
| 131 | + |
| 132 | + if not addresses: |
| 133 | + logging.error("No addresses found in header file. Exiting.") |
| 134 | + raise SystemExit(1) |
| 135 | + |
| 136 | + txt_filename = header_path.name + ".txt" |
| 137 | + txt_path = folder_path / txt_filename |
| 138 | + |
| 139 | + data_map = read_existing_txt(txt_path) |
| 140 | + |
| 141 | + write_output(txt_path, addresses, data_map) |
| 142 | + |
| 143 | + logging.info("Done.") |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments