|
1 | 1 | import json |
2 | 2 | from enum import Enum |
3 | 3 | from pathlib import Path |
4 | | -from typing import List |
| 4 | +from typing import List, Optional |
5 | 5 |
|
6 | 6 | from pydantic import BaseModel |
7 | 7 |
|
@@ -39,16 +39,101 @@ def load_board_identifiers() -> List[SerialBoardIdentifier]: |
39 | 39 | with open(boards_path, encoding="utf-8") as f: |
40 | 40 | json_data = json.load(f) |
41 | 41 |
|
42 | | - # Extract all unique board names |
| 42 | + # Extract all unique board names from the new structure |
43 | 43 | board_names = set() |
44 | 44 | for boards_list in json_data.values(): |
45 | | - board_names.update(boards_list) |
| 45 | + if isinstance(boards_list, list) and boards_list: |
| 46 | + # Check if it's the new format (list of objects) or old format (list of strings) |
| 47 | + if isinstance(boards_list[0], dict): |
| 48 | + # New format: extract board_name from objects |
| 49 | + board_names.update(board["board_name"] for board in boards_list if "board_name" in board) |
| 50 | + else: |
| 51 | + # Old format: boards_list contains strings directly |
| 52 | + board_names.update(boards_list) |
46 | 53 |
|
47 | 54 | return [ |
48 | 55 | SerialBoardIdentifier(attribute=SerialAttr.product, id_value=board_name, platform=Platform.GenericSerial) |
49 | 56 | for board_name in board_names |
50 | 57 | ] |
51 | 58 |
|
52 | 59 |
|
| 60 | +def get_board_id_from_name(board_name: str) -> Optional[int]: |
| 61 | + """ |
| 62 | + Get the board ID for a given board name by searching through the boards cache. |
| 63 | +
|
| 64 | + Args: |
| 65 | + board_name: The name of the board to look up. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + Optional[int]: The board ID if found, None otherwise. |
| 69 | + """ |
| 70 | + try: |
| 71 | + boards_path = get_boards_cache_path() |
| 72 | + |
| 73 | + # If cache doesn't exist, try to generate it |
| 74 | + if not boards_path.exists(): |
| 75 | + handler = ManifestHandler() |
| 76 | + handler.process_and_export(boards_path) |
| 77 | + |
| 78 | + with open(boards_path, encoding="utf-8") as f: |
| 79 | + json_data = json.load(f) |
| 80 | + |
| 81 | + # Search through all USB IDs for the board name |
| 82 | + for boards_list in json_data.values(): |
| 83 | + if isinstance(boards_list, list): |
| 84 | + for board in boards_list: |
| 85 | + if isinstance(board, dict) and board.get("board_name") == board_name: |
| 86 | + return board.get("board_id") |
| 87 | + elif isinstance(board, str) and board == board_name: |
| 88 | + # Old format doesn't have board IDs |
| 89 | + return None |
| 90 | + |
| 91 | + return None |
| 92 | + except (json.JSONDecodeError, OSError, FileNotFoundError): |
| 93 | + return None |
| 94 | + |
| 95 | + |
| 96 | +def get_board_id_from_usb_id(vid: int, pid: int, board_name: Optional[str] = None) -> Optional[int]: |
| 97 | + """ |
| 98 | + Get the board ID for a given USB VID:PID combination, optionally filtering by board name. |
| 99 | +
|
| 100 | + Args: |
| 101 | + vid: USB Vendor ID |
| 102 | + pid: USB Product ID |
| 103 | + board_name: Optional board name to filter results when multiple boards share the same USB ID |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Optional[int]: The board ID if found, None otherwise. |
| 107 | + """ |
| 108 | + try: |
| 109 | + boards_path = get_boards_cache_path() |
| 110 | + |
| 111 | + # If cache doesn't exist, try to generate it |
| 112 | + if not boards_path.exists(): |
| 113 | + handler = ManifestHandler() |
| 114 | + handler.process_and_export(boards_path) |
| 115 | + |
| 116 | + with open(boards_path, encoding="utf-8") as f: |
| 117 | + json_data = json.load(f) |
| 118 | + |
| 119 | + # Format USB ID as vid:pid (lowercase hex) |
| 120 | + usb_id = f"{vid:04x}:{pid:04x}" |
| 121 | + |
| 122 | + if usb_id in json_data: |
| 123 | + boards_list = json_data[usb_id] |
| 124 | + if isinstance(boards_list, list): |
| 125 | + for board in boards_list: |
| 126 | + if isinstance(board, dict): |
| 127 | + # If board_name is specified, filter by it |
| 128 | + return board.get("board_id") |
| 129 | + elif isinstance(board, str): |
| 130 | + # Old format doesn't have board IDs |
| 131 | + return None |
| 132 | + |
| 133 | + return None |
| 134 | + except (json.JSONDecodeError, OSError, FileNotFoundError): |
| 135 | + return None |
| 136 | + |
| 137 | + |
53 | 138 | # Load dynamic board identifiers from manifest |
54 | 139 | identifiers = load_board_identifiers() |
0 commit comments