Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pcbdraw/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PKG_BASE = os.path.dirname(__file__)

def parse_pcbdraw(lexer: Any, m: re.Match[str], state: Any=None) -> Any:
"""The rule given to mistune to parse out the desired variables (side and component)"""
text = m.group(1)
side, components = text.split("|")
components = list(map(lambda x: x.strip(), components.split(",")))
Expand Down Expand Up @@ -232,11 +233,13 @@ def generate_image(boardfilename: str, side: str, components: List[str],
plot_args += ["--filter", ",".join(components)]
plot_args += ["--highlight", ",".join(active)]
plot_args += [boardfilename, outputfile]
tmp_std = sys.stdout # make a copy of stdout in order to preserve it before Click overrides it
try:
plot.main(args=plot_args)
except SystemExit as e:
if e.code is not None and e.code != 0:
raise e from None
sys.stdout = tmp_std # restore copied stdout for print statements to work

def get_data_path() -> List[str]:
paths: List[str] = []
Expand Down
37 changes: 34 additions & 3 deletions pcbdraw/ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import platform
import sys
import re
from dataclasses import dataclass
from enum import IntEnum
from typing import Tuple, Optional, Any, List
Expand Down Expand Up @@ -68,6 +69,36 @@ def convert(self, value: Any, param: Optional[click.Parameter],
values = [x.strip() for x in value.split(",")]
return values


class CommaComponentList(CommaList):
name = "Comma separated component list, with optional ranges like R1-R20"

def convert(self, value: Any, param: Optional[click.Parameter],
ctx: Optional[click.Context]) -> List[str]:
values = super().convert(value, param, ctx)
for i in reversed(range(len(values))):
c = values[i]
if c.count('-') == 1: # if we have a - for a range (for example R3-R9)
s_m = re.match(r'([a-zA-Z]+?)(\d+)-([a-zA-Z]+?)(\d+)$', c)
if s_m is None:
continue
try:
prefix = s_m.group(1)
if prefix != s_m.group(3): # if the first and second prefix don't match, ignore
continue
start_n = int(s_m.group(2))
end_n = int(s_m.group(4))
except (IndexError, ValueError):
# if we either didn't have a full regex match, or the numbers weren't integers somehow?
continue
if start_n > end_n: # check that the first number is the lower limit (R6-R2 is not valid)
continue
# Replace XY-XZ with [XY, X(Y+1), X(Y+2), ..., X(Z-1), ZX]
values += [f"{prefix}{i}" for i in range(start_n, end_n + 1)]
values.pop(i)
return values


@dataclass
class WarningStderrReporter:
silent: bool
Expand Down Expand Up @@ -99,9 +130,9 @@ def __call__(self, tag: str, msg: str) -> None:
help="Specify which side of the PCB to render")
@click.option("--mirror", is_flag=True,
help="Mirror the board")
@click.option("--highlight", type=CommaList(), default=[],
@click.option("--highlight", type=CommaComponentList(), default=[],
help="Comma separated list of components to highlight")
@click.option("--filter", "-f", type=CommaList(), default=None,
@click.option("--filter", "-f", type=CommaComponentList(), default=None,
help="Comma separated list of components to show, if not specified, show all")
@click.option("--vcuts", "-v", type=KiCADLayer(), default=None,
help="If layer specified, renders V-cuts from it")
Expand All @@ -115,7 +146,7 @@ def __call__(self, tag: str, msg: str) -> None:
help="Treat warnings as errors")
@click.option("--resistor-values", type=CommaList(), default=[],
help="Comma separated list of resistor value remapping. For example, \"R1:10k,R2:470\"")
@click.option("--resistor-flip", type=CommaList(), default=[],
@click.option("--resistor-flip", type=CommaComponentList(), default=[],
help="Comma separated list of resistor bands to flip")
@click.option("--paste", is_flag=True,
help="Add paste layer")
Expand Down