Skip to content

Commit ca2a24e

Browse files
authored
Merge pull request #9221 from elpekenin/feat/stub/list
[Tooling] Add `--list` to `circuitpython_setboard`
2 parents b340b73 + 49e0f5a commit ca2a24e

File tree

1 file changed

+90
-13
lines changed

1 file changed

+90
-13
lines changed
Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,103 @@
11
# SPDX-FileCopyrightText: 2024 Tim Cocks
22
#
33
# SPDX-License-Identifier: MIT
4-
from importlib import resources
5-
import os
4+
import argparse
65
import sys
76
import shutil
7+
from collections import defaultdict
8+
from importlib import resources
9+
from importlib.abc import Traversable
10+
11+
12+
def get_definitions_or_exit(board: str) -> Traversable:
13+
"""Get the definitions file for a board given its name."""
14+
15+
path = resources.files("board_definitions").joinpath(board)
16+
17+
file = path.joinpath("__init__.pyi")
18+
if not file.is_file():
19+
sys.stderr.write(f"Definitions for: '{board}' were not found\n")
20+
sys.exit(1)
21+
22+
return file
23+
24+
25+
def get_doc_or_exit(board: str) -> str:
26+
"""Get the docstring for a board given its name."""
27+
28+
with get_definitions_or_exit(board).open("r") as f:
29+
return f.read().split('"""')[1]
30+
31+
32+
def header(txt: str) -> str:
33+
"""Helper text formatter."""
34+
return txt + "\n" + "-" * len(txt) + "\n"
835

936

1037
def set_board():
11-
if len(sys.argv) != 2:
12-
print(f"Incorrect args. Please call with: 'circuitpython_setboard chosen_board'")
13-
return
38+
parser = argparse.ArgumentParser(
39+
prog=__name__,
40+
usage="Install CircuitPython board-specific stubs",
41+
)
42+
parser.add_argument("chosen_board", help="selected board", nargs="?")
43+
parser.add_argument(
44+
"-l",
45+
"--list",
46+
help=f"show available boards. can filter eg: '{__name__} -l feather'",
47+
action="store_true",
48+
)
1449

15-
chosen_board = sys.argv[1]
16-
print(f"setting board: {chosen_board}")
50+
args = parser.parse_args()
1751

18-
board_stubs_file = resources.files("board-stubs").joinpath("__init__.pyi")
19-
board_definitions_path = resources.files("board_definitions").joinpath(chosen_board)
20-
board_definitions_file = board_definitions_path.joinpath("__init__.pyi")
52+
if args.list:
53+
port_boards: defaultdict[str, list[str]] = defaultdict(list)
54+
55+
# NOTE: "" in some_str == True
56+
looking_for = "" if args.chosen_board is None else args.chosen_board.lower()
57+
58+
for board in resources.files("board_definitions").iterdir():
59+
# NOTE: For the hand-crafted finding of port in the docstring, its
60+
# format is assumed to be:
61+
#
62+
# <empty line>
63+
# Board stub for ...
64+
# - port: ...
65+
# - board_id: ...
66+
# - NVM size: ...
67+
# - Included modules: ...
68+
# - Frozen libraries: ...
69+
#
70+
71+
lines = get_doc_or_exit(board).split("\n")
72+
port = lines[2].split("-")[1].split(":")[1].strip()
73+
74+
if looking_for not in board.name.lower() and looking_for not in port.lower():
75+
continue
76+
77+
port_boards[port].append(board.name)
2178

22-
if not board_definitions_file.is_file():
23-
print(f"Board: '{chosen_board}' was not found")
24-
return
79+
if not port_boards:
80+
sys.stdout.write("Nothing found, check out your filter.\n")
81+
sys.exit(0)
2582

83+
sys.stdout.write("Available boards are: \n")
84+
# sort by port name
85+
for port, boards in sorted(port_boards.items(), key=lambda kv: kv[0]):
86+
sys.stdout.write(
87+
header(port)
88+
+ " * "
89+
# sort by board name
90+
+ "\n * ".join(sorted(boards))
91+
+ "\n\n"
92+
)
93+
94+
sys.exit(0)
95+
96+
if args.chosen_board is None:
97+
sys.stderr.write("Must select a board\n")
98+
sys.exit(1)
99+
100+
board_definitions_file = get_definitions_or_exit(args.chosen_board)
101+
102+
board_stubs_file = resources.files("board-stubs").joinpath("__init__.pyi")
26103
shutil.copyfile(board_definitions_file, board_stubs_file)

0 commit comments

Comments
 (0)