Skip to content

Commit 9c858e0

Browse files
formatting
1 parent c9ba88e commit 9c858e0

File tree

4 files changed

+40
-51
lines changed

4 files changed

+40
-51
lines changed

schemascii/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import importlib
22
import os
33

4-
import schemascii.components as _comp
4+
import schemascii.components as _c
55
import schemascii.drawing as _drawing
66

77
__version__ = "0.3.2"
88

99

1010
def import_all_components():
11-
for f in os.scandir(os.path.dirname(_comp.__file__)):
11+
for f in os.scandir(os.path.dirname(_c.__file__)):
1212
if f.is_file():
1313
importlib.import_module(
14-
f"{_comp.__package__}.{f.name.removesuffix('.py')}")
14+
f"{_c.__package__}.{f.name.removesuffix('.py')}")
1515

1616

1717
import_all_components()
@@ -24,9 +24,5 @@ def render(filename: str, text: str | None = None, **options) -> str:
2424

2525

2626
if __name__ == "__main__":
27-
print(render(
28-
"test_data/test_resistors.txt",
29-
scale=20,
30-
padding=20,
31-
stroke_width=2,
32-
stroke="black"))
27+
import schemascii.component as _comp
28+
print(_comp.Component.all_components)

schemascii/__main__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import argparse
22
import sys
33
import warnings
4-
from . import render, __version__
5-
from .errors import Error
6-
from .configs import add_config_arguments
4+
5+
import schemascii
6+
import schemascii.errors as _errors
77

88

99
def cli_main():
1010
ap = argparse.ArgumentParser(
11-
prog="schemascii", description="Render ASCII-art schematics into SVG."
12-
)
11+
prog="schemascii", description="Render ASCII-art schematics into SVG.")
1312
ap.add_argument(
14-
"-V", "--version", action="version", version="%(prog)s " + __version__
15-
)
13+
"-V", "--version", action="version",
14+
version="%(prog)s " + schemascii.__version__)
1615
ap.add_argument("in_file", help="File to process.")
1716
ap.add_argument(
1817
"-o",
1918
"--out",
2019
default=None,
2120
dest="out_file",
22-
help="Output SVG file. (default input file plus .svg)",
23-
)
21+
help="Output SVG file. (default input file plus .svg)")
22+
# TODO: implement this
2423
add_config_arguments(ap)
2524
args = ap.parse_args()
2625
if args.out_file is None:
@@ -31,8 +30,8 @@ def cli_main():
3130
args.in_file = "<stdin>"
3231
try:
3332
with warnings.catch_warnings(record=True) as captured_warnings:
34-
result_svg = render(args.in_file, text, **vars(args))
35-
except Error as err:
33+
result_svg = schemascii.render(args.in_file, text, **vars(args))
34+
except _errors.Error as err:
3635
print(type(err).__name__ + ":", err, file=sys.stderr)
3736
sys.exit(1)
3837
if captured_warnings:

schemascii/component.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@dataclass
1717
class Component(_dc.DataConsumer, namespaces=(":component",)):
1818
"""An icon representing a single electronic component."""
19+
1920
all_components: typing.ClassVar[dict[str, type[Component]]] = {}
2021

2122
options = [
@@ -132,9 +133,12 @@ def __init_subclass__(cls, ids: tuple[str, ...] = None,
132133
if not (id_letters.isalpha() and id_letters.upper() == id_letters):
133134
raise ValueError(
134135
f"invalid reference designator letters: {id_letters!r}")
135-
if id_letters in cls.all_components:
136+
if (id_letters in cls.all_components
137+
and cls.all_components[id_letters] is not cls):
136138
raise ValueError(
137-
f"duplicate reference designator letters: {id_letters!r}")
139+
f"duplicate reference designator letters: {id_letters!r} "
140+
f"(trying to register {cls!r}, already "
141+
f"occupied by {cls.all_components[id_letters]!r})")
138142
cls.all_components[id_letters] = cls
139143

140144
@property

schemascii/utils.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def id_text(
419419
if nolabels:
420420
return ""
421421
if not point:
422-
point = sum(t.pt for t in terminals) / len(terminals)
422+
point = centroid(t.pt for t in terminals)
423423
data = ""
424424
if isinstance(value, str):
425425
data = value
@@ -441,12 +441,9 @@ def id_text(
441441
if len(terminals) > 1:
442442
textach = (
443443
"start"
444-
if (
445-
any(Side.BOTTOM == t.side for t in terminals)
446-
or any(Side.TOP == t.side for t in terminals)
447-
)
448-
else "middle"
449-
)
444+
if (any(Side.BOTTOM == t.side for t in terminals)
445+
or any(Side.TOP == t.side for t in terminals))
446+
else "middle")
450447
else:
451448
textach = "middle" if terminals[0].side in (
452449
Side.TOP, Side.BOTTOM) else "start"
@@ -472,22 +469,15 @@ def make_text_point(t1: complex, t2: complex, **options) -> complex:
472469
return text_pt
473470

474471

475-
def make_plus(terminals: list[Terminal], center: complex,
476-
theta: float, **options) -> str:
477-
"""Make a + sign if the terminals indicate the component is polarized."""
478-
if all(t.flag != "+" for t in terminals):
479-
return ""
472+
def make_plus(center: complex, theta: float, **options) -> str:
473+
"""Make a '+' sign for indicating polarity."""
480474
return XML.g(
481475
bunch_o_lines(
482476
deep_transform(
483477
deep_transform([(0.125, -0.125), (0.125j, -0.125j)], 0, theta),
484-
center + deep_transform(0.33 + 0.75j, 0, theta),
485-
0,
486-
),
487-
**options,
488-
),
489-
class_="plus",
490-
)
478+
center + deep_transform(0.33 + 0.75j, 0, theta), 0),
479+
**options),
480+
class_="plus")
491481

492482

493483
def arrow_points(p1: complex, p2: complex) -> list[tuple[complex, complex]]:
@@ -497,16 +487,16 @@ def arrow_points(p1: complex, p2: complex) -> list[tuple[complex, complex]]:
497487
return [
498488
(p2, p1),
499489
(p2, p2 - rect(tick_len, angle + pi / 5)),
500-
(p2, p2 - rect(tick_len, angle - pi / 5)),
501-
]
490+
(p2, p2 - rect(tick_len, angle - pi / 5))]
502491

503492

504493
def make_variable(center: complex, theta: float, **options) -> str:
505-
"""Draw a 'variable' arrow across the component."""
506-
return bunch_o_lines(deep_transform(arrow_points(-1, 1),
507-
center,
508-
(theta % pi) + pi / 4),
509-
**options)
494+
"""Draw a "variable" arrow across the component."""
495+
return XML.g(bunch_o_lines(deep_transform(arrow_points(-1, 1),
496+
center,
497+
(theta % pi) + pi / 4),
498+
**options),
499+
class_="variable")
510500

511501

512502
def light_arrows(center: complex, theta: float, out: bool, **options):
@@ -516,13 +506,13 @@ def light_arrows(center: complex, theta: float, out: bool, **options):
516506
a, b = 1j, 0.3 + 0.3j
517507
if out:
518508
a, b = b, a
519-
return bunch_o_lines(
509+
return XML.g(bunch_o_lines(
520510
deep_transform(arrow_points(a, b),
521511
center, theta - pi / 2)
522512
+ deep_transform(arrow_points(a - 0.5, b - 0.5),
523513
center, theta - pi / 2),
524-
**options
525-
)
514+
**options),
515+
class_="light-emitting" if out else "light-dependent")
526516

527517

528518
def sort_terminals_counterclockwise(

0 commit comments

Comments
 (0)