Skip to content

Commit 2717793

Browse files
use custom errors
1 parent 536c5e5 commit 2717793

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

schemascii/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import argparse
22
import sys
33
from . import render, __version__
4+
from .errors import Error
45

56

67
def cli_main():
78
ap = argparse.ArgumentParser(
89
prog="schemascii",
910
description="Render ASCII-art schematics into SVG.")
11+
ap.add_argument("-V", "--version",
12+
type="version",
13+
version=__version__)
1014
ap.add_argument("in_file",
1115
help="File to process.")
1216
ap.add_argument("-o", "--out",
@@ -18,8 +22,8 @@ def cli_main():
1822
args.out_file = args.in_file + ".svg"
1923
try:
2024
result_svg = render(args.in_file)
21-
except Exception as e:
22-
print("error:", e, file=sys.stderr)
25+
except Error as err:
26+
print(repr(err), file=sys.stderr)
2327
sys.exit(1)
2428
with open(args.out_file, "w", encoding="utf-8") as out:
2529
out.write(result_svg)

schemascii/components.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
from .grid import Grid
33
from .utils import Cbox, BOMData
4+
from .errors import DiagramSyntaxError, BOMError
5+
46

57
SMALL_COMPONENT_OR_BOM = re.compile(r'#*([A-Z]+)(\d+|\.\w+)(:[^\s]+)?#*')
68

@@ -47,22 +49,22 @@ def find_big(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
4749
y2 = j
4850
break
4951
if not cs[0] == cs[-1] == ':':
50-
raise SyntaxError(
52+
raise DiagramSyntaxError(
5153
f'{grid.filename}: Fragmented box '
5254
f'starting at line {y1 + 1}, col {x1 + 1}')
5355
else:
54-
raise SyntaxError(
56+
raise DiagramSyntaxError(
5557
f'{grid.filename}: Unfinished box '
5658
f'starting at line {y1 + 1}, col {x1 + 1}')
5759
inside = grid.clip(complex(x1, y1), complex(x2, y2))
5860
results, resb = find_small(inside)
5961
if len(results) == 0 and len(resb) == 0:
60-
raise ValueError(
62+
raise BOMError(
6163
f'{grid.filename}: Box starting at '
6264
f'line {y1 + 1}, col {x1 + 1} is '
6365
f'missing reference designator')
6466
if len(results) != 1 and len(resb) != 1:
65-
raise ValueError(
67+
raise BOMError(
6668
f'{grid.filename}: Box starting at '
6769
f'line {y1 + 1}, col {x1 + 1} has '
6870
f'multiple reference designators')
@@ -95,9 +97,9 @@ def find_all(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
9597

9698

9799
if __name__ == '__main__':
98-
grid = Grid("test_data/test_resistors.txt")
99-
bbb, _ = find_all(grid)
100+
test_grid = Grid("test_data/test_resistors.txt")
101+
bbb, _ = find_all(test_grid)
100102
all_pts = []
101103
for box in bbb:
102104
all_pts.extend([box.p1, box.p2])
103-
grid.spark(*all_pts)
105+
test_grid.spark(*all_pts)

schemascii/components_render.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from cmath import phase, rect
33
from math import pi
44
from .utils import (Cbox, Terminal, BOMData, XML, Side,
5-
polylinegon, id_text, make_text_point,
6-
bunch_o_lines, deep_transform, make_plus, make_variable)
7-
from .metric import format_metric_unit
5+
polylinegon, id_text, make_text_point,
6+
bunch_o_lines, deep_transform, make_plus, make_variable)
7+
from .errors import TerminalsError, BOMError, UnsupportedComponentError
88

99
RENDERERS = {}
1010

@@ -31,7 +31,7 @@ def n_check(
3131
bom_data: list[BOMData],
3232
**kwargs):
3333
if len(terminals) != n_terminals:
34-
raise TypeError(
34+
raise TerminalsError(
3535
f"{box.type}{box.id} component can only "
3636
f"have {n_terminals} terminals")
3737
return func(box, terminals, bom_data, **kwargs)
@@ -47,7 +47,7 @@ def de_ambiguous(
4747
bom_data: list[BOMData],
4848
**kwargs):
4949
if len(bom_data) > 1:
50-
raise ValueError(
50+
raise BOMError(
5151
f"Ambiguous BOM data for {box.type}{box.id}: {bom_data!r}")
5252
return func(
5353
box,
@@ -66,7 +66,7 @@ def sort_terminals(
6666
bom_data: list[BOMData],
6767
**kwargs):
6868
if len(terminals) != 2:
69-
raise TypeError(
69+
raise TerminalsError(
7070
f"{box.type}{box.id} component can only "
7171
f"have 2 terminals")
7272
if terminals[1].flag == '+':
@@ -261,8 +261,7 @@ def render_component(
261261
**kwargs):
262262
"Render the component into an SVG string."
263263
if box.type not in RENDERERS:
264-
raise NameError(
265-
f"Unsupported component type: {box.type}")
264+
raise UnsupportedComponentError(box.type)
266265
return XML.g(
267266
RENDERERS[box.type](box, terminals, bom_data, **kwargs),
268267
class_=f"component {box.type}"

schemascii/errors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Error(Exception):
2+
"A generic Schemascii error."
3+
4+
5+
class DiagramSyntaxError(SyntaxError, Error):
6+
"Bad formatting in Schemascii diagram syntax."
7+
8+
9+
class TerminalsError(TypeError, Error):
10+
"Incorrect usage of terminals on this component."
11+
12+
13+
class BOMError(ValueError, Error):
14+
"Problem with BOM data for a component."
15+
16+
17+
class UnsupportedComponentError(NameError, Error):
18+
"Component type is not supported."

0 commit comments

Comments
 (0)