8000 better error messages at command line · dragoncoder047/schemascii@96ee5e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96ee5e6

Browse files
better error messages at command line
1 parent 52c1516 commit 96ee5e6

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

schemascii/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import os
23
import sys
34
import warnings
45

@@ -69,13 +70,15 @@ def cli_main():
6970
if args.out_file is None:
7071
args.out_file = sys.stdout
7172
elif args.out_file is None:
72-
args.out_file = open(args.in_file.name + ".svg")
73+
args.out_file = open(args.in_file.name + ".svg", "w")
7374
try:
7475
with warnings.catch_warnings(record=True) as captured_warnings:
7576
result_svg = schemascii.render(args.in_file.name,
7677
args.in_file.read(), args.fudge)
7778
except _errors.Error as err:
78-
ap.error(str(err))
79+
if args.out_file is not sys.stdout:
80+
os.unlink(args.out_file.name)
81+
ap.error(err.nice_message())
7982

8083
if captured_warnings:
8184
for warning in captured_warnings:
@@ -86,7 +89,6 @@ def cli_main():
8689
sys.exit(1)
8790

8891
args.out_file.write(result_svg)
89-
9092
finally:
9193
args.in_file.close()
9294
args.out_file.close()

schemascii/errors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
class Error(Exception):
22
"""A generic Schemascii error encountered when rendering a drawing."""
33

4+
message_prefix: str | None = None
5+
6+
def nice_message(self) -> str:
7+
return f"{self.message_prefix}{
8+
": " if self.message_prefix else ""
9+
}{self!s}"
10+
411

512
class DiagramSyntaxError(SyntaxError, Error):
613
"""Bad formatting in Schemascii diagram syntax."""
14+
message_prefix = "syntax error"
715

816

917
class TerminalsError(ValueError, Error):
1018
"""Incorrect usage of terminals on this component."""
19+
message_prefix = "terminals error"
1120

1221

1322
class BOMError(ValueError, Error):
1423
"""Problem with BOM data for a component."""
24+
message_prefix = "BOM error"
1525

1626

1727
class UnsupportedComponentError(NameError, Error):
1828
"""Component type is not supported."""
29+
message_prefix = "unsupported component"
1930

2031

2132
class NoDataError(ValueError, Error):
2233
"""Data item is required, but not present."""
34+
message_prefix = "missing data item"
2335

2436

2537
class DataTypeError(TypeError, Error):
2638
"""Invalid data type in data section."""
39+
message_prefix = "invalid data type"

scripts/scriptutils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import os
22
import sys
3-
import typing
43

54
# pylint: disable=missing-function-docstring
65

7-
T = typing.TypeVar("T")
8-
96

107
def cmd(sh_line: str, say: bool = True):
118
if say:
@@ -25,6 +22,6 @@ def spit(file: os.PathLike, text: str):
2522
f.write(text)
2623

2724

28-
def spy(value: T) -> T:
25+
def spy[T](value: T) -> T:
2926
print(value)
3027
return value

0 commit comments

Comments
 (0)
0