|
| 1 | +import importlib |
| 2 | +import os |
1 | 3 |
|
| 4 | +import schemascii.components as _comp |
| 5 | +import schemascii.drawing as _drawing |
2 | 6 |
|
3 | 7 | __version__ = "0.3.2"
|
4 | 8 |
|
5 | 9 |
|
| 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 | + |
6 | 21 | 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) |
45 | 24 |
|
46 | 25 |
|
47 | 26 | 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")) |
0 commit comments