Skip to content

Commit a823d6a

Browse files
update options handling and readme
1 parent 1680f75 commit a823d6a

File tree

6 files changed

+85
-32
lines changed

6 files changed

+85
-32
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,51 @@ A command-line tool and library for converting ASCII-art diagrams into beautiful
1010

1111
Works with Python 3.10+. It uses the new `match` feature in a few places. If you need to run Schemascii on an older version of Python, feel free to fork it and send me a pull request.
1212

13+
Command line usage:
14+
15+
```usage
16+
usage: schemascii [-h] [-V] [-o OUT_FILE] [--padding PADDING] [--scale SCALE] [--stroke_width STROKE_WIDTH] [--stroke STROKE]
17+
[--label {L,V,VL}]
18+
in_file
19+
20+
Render ASCII-art schematics into SVG.
21+
22+
positional arguments:
23+
in_file File to process.
24+
25+
options:
26+
-h, --help show this help message and exit
27+
-V, --version show program's version number and exit
28+
-o OUT_FILE, --out OUT_FILE
29+
Output SVG file. (default input file plus .svg)
30+
--padding PADDING Amount of padding to add on the edges.
31+
--scale SCALE Scale at which to enlarge the entire diagram by.
32+
--stroke_width STROKE_WIDTH
33+
Width of the lines
34+
--stroke STROKE Color of the lines.
35+
--label {L,V,VL} Component label style (L=include label, V=include value, VL=both)
36+
```
37+
38+
Python usage example:
39+
40+
```python
41+
import schemascii
42+
43+
# Render a file
44+
svg = schemascii.render("my_circuit.txt")
45+
46+
# Render a string
47+
text = ... # this is the text of your file
48+
svg = schemascii.render("<string>", text)
49+
50+
# Provide options
51+
svg = schemascii.render("my_circuit.txt",
52+
padding=10,
53+
scale=15,
54+
stroke_width=2,
55+
stroke="black",
56+
label="LV")
57+
# these are the defaults
58+
```
59+
1360
<!-- https://realpython.com/pypi-publish-python-package/ -->

schemascii/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77

88
__version__ = "0.1.0"
99

10+
default_options = {
11+
'padding': 10,
12+
'scale': 15,
13+
'stroke_width': 2,
14+
'stroke': 'black',
15+
'label': 'LV',
16+
}
17+
1018

1119
def render(filename: str, text: str = None, **options) -> str:
1220
"Render the Schemascii diagram to an SVG string."
1321
if text is None:
1422
with open(filename, encoding="ascii") as f:
1523
text = f.read()
24+
# default options
25+
options = default_options | options
1626
# get everything
1727
grid = Grid(filename, text)
1828
components, bom_data = find_all(grid)
@@ -21,8 +31,8 @@ def render(filename: str, text: str = None, **options) -> str:
2131
b.id == c.id and b.type == c.type]
2232
for c in components}
2333
# get some options
24-
padding = options.get('padding', 1)
25-
scale = options.get('scale', 1)
34+
padding = options['padding']
35+
scale = options['scale']
2636

2737
wires = get_wires(grid, **options)
2838
components_strs = (render_component(

schemascii/__main__.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@ def cli_main():
1919
help="Output SVG file. (default input file plus .svg)")
2020
ap.add_argument("--padding",
2121
help="Amount of padding to add on the edges.",
22-
type=int,
23-
default=10)
22+
type=int)
2423
ap.add_argument("--scale",
2524
help="Scale at which to enlarge the entire diagram by.",
26-
type=int,
27-
default=15)
25+
type=int)
2826
ap.add_argument("--stroke_width",
2927
help="Width of the lines",
30-
type=int,
31-
default=2)
28+
type=int)
3229
ap.add_argument("--stroke",
33-
help="Color of the lines.",
34-
default="black")
30+
help="Color of the lines.")
3531
ap.add_argument("--label",
3632
help="Component label style "
3733
"(L=include label, V=include value, VL=both)",
38-
choices="L V VL".split(),
39-
default="VL")
34+
choices="L V VL".split())
4035
args = ap.parse_args()
4136
if args.out_file is None:
4237
args.out_file = args.in_file + ".svg"

schemascii/components_render.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Callable
22
from cmath import phase, rect
33
from math import pi
4+
from warnings import warn
45
from .utils import (Cbox, Terminal, BOMData, XML, Side,
56
polylinegon, id_text, make_text_point,
67
bunch_o_lines, deep_transform, make_plus, make_variable)
@@ -200,17 +201,17 @@ def integrated_circuit(
200201
bom_data: BOMData | None,
201202
**options):
202203
"Draw an IC"
203-
label_style = options.get("label", "VL")
204-
scale = options.get("scale", 1)
204+
label_style = options["label"]
205+
scale = options["scale"]
205206
sz = (box.p2 - box.p1) * scale
206207
mid = (box.p2 + box.p1) * scale / 2
207208
out = XML.rect(
208209
x=box.p1.real * scale,
209210
y=box.p1.imag * scale,
210211
width=sz.real,
211212
height=sz.imag,
212-
stroke__width=options.get("stroke_width", 1),
213-
stroke=options.get("stroke", "black"),
213+
stroke__width=options["stroke_width"],
214+
stroke=options["stroke"],
214215
fill="none")
215216
for term in terminals:
216217
out += bunch_o_lines([(
@@ -225,9 +226,9 @@ def integrated_circuit(
225226
x=mid.real,
226227
y=mid.imag,
227228
text__anchor="middle",
228-
font__size=options.get("scale", 1),
229-
fill=options.get("stroke", "black"))
230-
print("IC's in progress...")
229+
font__size=options["scale"],
230+
fill=options["stroke"])
231+
warn("ICs are not fully implemented yet.")
231232
return out
232233

233234
# code for drawing

schemascii/utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ def mk_tag(*contents, **attrs) -> str:
112112

113113
def polylinegon(points: list[complex], is_polygon: bool = False, **options):
114114
"Turn the list of points into a <polyline> or <polygon>."
115-
scale = options.get("scale", 1)
116-
w = options.get("stroke_width", 0.15)
117-
c = options.get("stroke", "black")
115+
scale = options["scale"]
116+
w = options["stroke_width"]
117+
c = options["stroke"]
118118
pts = ' '.join(
119119
f'{x.real * scale},{x.imag * scale}'
120120
for x in points)
@@ -126,9 +126,9 @@ def polylinegon(points: list[complex], is_polygon: bool = False, **options):
126126
def bunch_o_lines(points: list[tuple[complex, complex]], **options):
127127
"Return a <line> for each pair of points."
128128
out = ''
129-
scale = options.get('scale', 1)
130-
w = options.get("stroke_width", 0.15)
131-
c = options.get("stroke", "black")
129+
scale = options['scale']
130+
w = options["stroke_width"]
131+
c = options["stroke"]
132132
for p1, p2 in points:
133133
out += XML.line(
134134
x1=p1.real * scale,
@@ -148,7 +148,7 @@ def id_text(
148148
point: complex | None = None,
149149
**options):
150150
"Format the component ID and value around the point"
151-
label_style = options.get("label", "VL")
151+
label_style = options["label"]
152152
if point is None:
153153
point = sum(t.pt for t in terminals) / len(terminals)
154154
data = ""
@@ -176,14 +176,14 @@ def id_text(
176176
any(Side.BOTTOM == t.side for t in terminals)
177177
or any(Side.TOP == t.side for t in terminals)
178178
) else "middle",
179-
font__size=options.get("scale", 1),
180-
fill=options.get("stroke", "black"))
179+
font__size=options["scale"],
180+
fill=options["stroke"])
181181

182182

183183
def make_text_point(t1: complex, t2: complex, **options) -> complex:
184184
"Compute the scaled coordinates of the text anchor point."
185185
quad_angle = phase(t1 - t2) + pi / 2
186-
scale = options.get("scale", 1)
186+
scale = options["scale"]
187187
text_pt = (t1 + t2) * scale / 2
188188
offset = rect(scale / 2, quad_angle)
189189
text_pt += complex(abs(offset.real), -abs(offset.imag))

schemascii/wires.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def blank_wire(grid: Grid, p1: complex, p2: complex):
9494
def next_wire(grid: Grid, **options) -> str | None:
9595
"""Returns a SVG string of the next line in the grid,
9696
or None if there are no more. The line is masked off."""
97-
scale = options.get("scale", 1)
98-
stroke_width = options.get("stroke_width", 1)
99-
color = options.get("stroke", "black")
97+
scale = options["scale"]
98+
stroke_width = options["stroke_width"]
99+
color = options["stroke"]
100100
# Find the first wire or return None
101101
for i, line in enumerate(grid.lines):
102102
indexes = [line.index(c) for c in '-|()*' if c in line]

0 commit comments

Comments
 (0)