Skip to content

Commit c9ba88e

Browse files
import_all_components()
1 parent 363c723 commit c9ba88e

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

schemascii/__init__.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
1+
import importlib
2+
import os
13

4+
import schemascii.components as _comp
5+
import schemascii.drawing as _drawing
26

37
__version__ = "0.3.2"
48

59

10+
def import_all_components():
11+
for f in os.scandir(os.path.dirname(_comp.__file__)):
12+
if f.is_file():
13+
importlib.import_module(
14+
f"{_comp.__package__}.{f.name.removesuffix('.py')}")
15+
16+
17+
import_all_components()
18+
del import_all_components
19+
20+
621
def render(filename: str, text: str | None = None, **options) -> str:
7-
"Render the Schemascii diagram to an SVG string."
8-
raise NotImplementedError
9-
if text is None:
10-
with open(filename, encoding="ascii") as f:
11-
text = f.read()
12-
# get everything
13-
grid = Grid(filename, text)
14-
# Passed-in options override diagram inline options
15-
options = apply_config_defaults(
16-
options | get_inline_configs(
17-
grid) | options.get("override_options", {})
18-
)
19-
components, bom_data = find_all(grid)
20-
terminals = {c: find_edge_marks(grid, c) for c in components}
21-
fixed_bom_data = {
22-
c: [b for b in bom_data if b.id == c.id and b.type == c.type]
23-
for c in components
24-
}
25-
# get some options
26-
padding = options["padding"]
27-
scale = options["scale"]
28-
29-
wires = get_wires(grid, **options)
30-
components_strs = (
31-
render_component(c, terminals[c], fixed_bom_data[c], **options)
32-
for c in components
33-
)
34-
return XML.svg(
35-
wires,
36-
*components_strs,
37-
width=grid.width * scale + padding * 2,
38-
height=grid.height * scale + padding * 2,
39-
viewBox=f"{-padding} {-padding} "
40-
f"{grid.width * scale + padding * 2} "
41-
f"{grid.height * scale + padding * 2}",
42-
xmlns="http://www.w3.org/2000/svg",
43-
class_="schemascii",
44-
)
22+
"""Render the Schemascii diagram to an SVG string."""
23+
return _drawing.Drawing.from_file(filename, text).to_xml_string(options)
4524

4625

4726
if __name__ == "__main__":
48-
print(
49-
render(
50-
"test_data/test_resistors.txt",
51-
scale=20,
52-
padding=20,
53-
stroke_width=2,
54-
stroke="black",
55-
)
56-
)
27+
print(render(
28+
"test_data/test_resistors.txt",
29+
scale=20,
30+
padding=20,
31+
stroke_width=2,
32+
stroke="black"))

schemascii/components/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import schemascii.component as _c
55
import schemascii.errors as _errors
66

7-
# TODO: import all of the component subclasses to register them
8-
97

108
@dataclass
119
class NTerminalComponent(_c.Component):

0 commit comments

Comments
 (0)