Skip to content

Commit 28ed3f4

Browse files
add inline options
TODO: document the change & release
1 parent b867ba8 commit 28ed3f4

File tree

8 files changed

+108
-49
lines changed

8 files changed

+108
-49
lines changed

release.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def writefile(file, text):
4141
cmd("python3 -m build --sdist")
4242
cmd("python3 -m build --wheel")
4343

44-
cmd("schemascii test_data/test_charge_pump.txt --out test_data/test_charge_pump.txt.svg --padding 30")
44+
cmd("schemascii test_data/test_charge_pump.txt --out test_data/test_charge_pump.txt.svg")
4545

4646
print("for some reason convert isn't working with the css, so aborting the auto-rendering")
4747
sys.exit(0)

schemascii/__init__.py

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from .inline_config import get_inline_configs
2+
from .configs import apply_config_defaults
13
from .grid import Grid
24
from .components import find_all
35
from .edgemarks import find_edge_marks
@@ -8,36 +10,16 @@
810

911
__version__ = "0.1.3"
1012

11-
default_options = {
12-
'padding': 10,
13-
'scale': 15,
14-
'stroke_width': 2,
15-
'stroke': 'black',
16-
'label': 'LV',
17-
'nolabels': False,
18-
}
19-
20-
option_types = {
21-
'padding': float,
22-
'scale': float,
23-
'stroke_width': float,
24-
'stroke': str,
25-
'label': str,
26-
'nolabels': bool,
27-
}
28-
2913

3014
def render(filename: str, text: str = None, **options) -> str:
3115
"Render the Schemascii diagram to an SVG string."
3216
if text is None:
3317
with open(filename, encoding="ascii") as f:
3418
text = f.read()
35-
# default options
36-
options = default_options | options
37-
for oname, otypefun in option_types.items():
38-
options[oname] = otypefun(options[oname])
3919
# get everything
4020
grid = Grid(filename, text)
21+
# Passed-in options override diagram inline options
22+
options = apply_config_defaults(get_inline_configs(grid) | options)
4123
components, bom_data = find_all(grid)
4224
terminals = {c: find_edge_marks(grid, c) for c in components}
4325
fixed_bom_data = {c: [b for b in bom_data if

schemascii/__main__.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44
from . import render, __version__
55
from .errors import Error
6+
from .configs import add_config_arguments
67

78

89
def cli_main():
@@ -18,30 +19,7 @@ def cli_main():
1819
default=None,
1920
dest="out_file",
2021
help="Output SVG file. (default input file plus .svg)")
21-
ap.add_argument("--padding",
22-
help="Amount of padding to add on the edges.",
23-
type=int,
24-
default=10)
25-
ap.add_argument("--scale",
26-
help="Scale at which to enlarge the entire diagram by.",
27-
type=int,
28-
default=15)
29-
ap.add_argument("--stroke_width",
30-
help="Width of the lines",
31-
type=int,
32-
default=2)
33-
ap.add_argument("--stroke",
34-
help="Color of the lines.",
35-
default="black")
36-
ap.add_argument("--label",
37-
help="Component label style "
38-
"(L=include label, V=include value, VL=both)",
39-
choices=["L", "V", "VL"],
40-
default="VL")
41-
ap.add_argument("--nolabels",
42-
help="Turns off labels on all components, "
43-
"except for part numbers on ICs.",
44-
action="store_true")
22+
add_config_arguments(ap)
4523
args = ap.parse_args()
4624
if args.out_file is None:
4725
args.out_file = args.in_file + ".svg"

schemascii/configs.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import argparse
2+
from dataclasses import dataclass
3+
from .errors import ArgumentError
4+
5+
6+
@dataclass
7+
class ConfigConfig:
8+
name: str
9+
clazz: type | list
10+
default: object
11+
description: str
12+
13+
14+
OPTIONS = [
15+
ConfigConfig("padding", float, 10,
16+
"Amount of padding to add on the edges."),
17+
ConfigConfig("scale", float, 15,
18+
"Scale at which to enlarge the entire diagram by."),
19+
ConfigConfig("stroke_width", float, 2, "Width of the lines"),
20+
ConfigConfig("stroke", str, "black", "Color of the lines."),
21+
ConfigConfig("label", ["", "VL", "L", "V"], "VL",
22+
"Component label style (L=include label, V=include value, VL=both)"),
23+
ConfigConfig("nolabels", bool, False,
24+
"Turns off labels on all components, except for part numbers on ICs."),
25+
]
26+
27+
28+
def add_config_arguments(a: argparse.ArgumentParser):
29+
"Register all the config options on the argument parser."
30+
for opt in OPTIONS:
31+
if isinstance(opt.clazz, list):
32+
a.add_argument(
33+
"--" + opt.name,
34+
help=opt.description,
35+
choices=opt.clazz,
36+
default=opt.default)
37+
else:
38+
a.add_argument(
39+
"--" + opt.name,
40+
help=opt.description,
41+
type=opt.clazz,
42+
default=opt.default)
43+
44+
45+
def apply_config_defaults(options: dict) -> dict:
46+
"Merge the defaults and ensure the options are the right type."
47+
for opt in OPTIONS:
48+
if opt.name not in options:
49+
options[opt.name] = opt.default
50+
continue
51+
if isinstance(opt.clazz, list):
52+
if options[opt.name] not in opt.clazz:
53+
raise ArgumentError(
54+
f"config option {opt.name}: invalid choice: {options[opt.name]} "
55+
f"(valid options are {', '.join(map(repr, opt.clazz))})")
56+
continue
57+
try:
58+
options[opt.name] = opt.clazz(options[opt.name])
59+
except ValueError as err:
60+
raise ArgumentError(f"config option {opt.name}: "
61+
f"invalid {opt.clazz.__name__} value: "
62+
f"{options[opt.name]}") from err
63+
return options

schemascii/errors.py

+4
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ class BOMError(ValueError, Error):
1616

1717
class UnsupportedComponentError(NameError, Error):
1818
"Component type is not supported."
19+
20+
21+
class ArgumentError(ValueError, Error):
22+
"Invalid config argument value."

schemascii/grid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Grid:
2-
"""Helper class for manmaging a 2-D
2+
"""Helper class for managing a 2-D
33
grid of ASCII art."""
44

55
def __init__(self, filename: str, data: str = None):

schemascii/inline_config.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
from .grid import Grid
3+
4+
INLINE_CONFIG_RE = re.compile(r"!([a-z]+)=([^!]*)!", re.I)
5+
6+
7+
def get_inline_configs(grid: Grid) -> dict:
8+
out = {}
9+
for y, line in enumerate(grid.lines):
10+
for m in INLINE_CONFIG_RE.finditer(line):
11+
interval = m.span()
12+
key = m.group(1)
13+
val = m.group(2)
14+
for x in range(*interval):
15+
grid.setmask(complex(x, y))
16+
try:
17+
val = float(val)
18+
except ValueError:
19+
pass
20+
out[key] = val
21+
return out
22+
23+
24+
if __name__ == '__main__':
25+
g = Grid("null",
26+
"""
27+
foobar -------C1-------
28+
!padding=30!!label=!
29+
!foobar=bar!
30+
""")
31+
print(get_inline_configs(g))
32+
print(g)

test_data/test_charge_pump.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*--BAT1+--*-------*---*
1+
*--BAT1+--*-------*---* !padding=30!
22
| | | |
33
| R1 .~~~. |
44
| | : :-*

0 commit comments

Comments
 (0)