Skip to content

Commit 777a903

Browse files
fix some typing and stuff
1 parent d492ce5 commit 777a903

File tree

9 files changed

+44
-23
lines changed

9 files changed

+44
-23
lines changed

schemascii/__main__.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ def cli_main():
1919
default=None,
2020
dest="out_file",
2121
help="Output SVG file. (default input file plus .svg)")
22+
ap.add_argument(
23+
"-w",
24+
"--warnings-are-errors",
25+
action="store_true",
26+
help="Treat warnings as errors. (default: False)")
27+
ap.add_argument(
28+
"-v",
29+
"--verbose",
30+
action="store_true",
31+
help="Print verbose logging output. (default: False)")
2232
# TODO: implement this
2333
add_config_arguments(ap)
2434
args = ap.parse_args()
@@ -35,13 +45,18 @@ def cli_main():
3545
print(type(err).__name__ + ":", err, file=sys.stderr)
3646
sys.exit(1)
3747
if captured_warnings:
38-
for warn in captured_warnings:
39-
print("warning:", warn.message, file=sys.stderr)
48+
for warning in captured_warnings:
49+
print("Warning:", warning.message, file=sys.stderr)
50+
if args.warnings_are_errors:
51+
print("Error: warnings were treated as errors", file=sys.stderr)
52+
sys.exit(1)
4053
if args.out_file == "-":
4154
print(result_svg)
4255
else:
4356
with open(args.out_file, "w", encoding="utf-8") as out:
4457
out.write(result_svg)
58+
if args.verbose:
59+
print("Wrote SVG to", args.out_file)
4560

4661

4762
if __name__ == "__main__":

schemascii/annoline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def is_annoline_character(cls, ch: str) -> bool:
7373
def find_all(cls, grid: _grid.Grid) -> list[AnnotationLine]:
7474
"""Return all of the annotation lines found in the grid."""
7575
seen_points: set[complex] = set()
76-
all_lines: list[cls] = []
76+
all_lines: list[AnnotationLine] = []
7777

7878
for y, line in enumerate(grid.lines):
7979
for x, ch in enumerate(line):

schemascii/annotation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import html
24
import re
35
from dataclasses import dataclass
@@ -24,9 +26,9 @@ class Annotation(_dc.DataConsumer, namespaces=(":annotation",)):
2426
css_class = "annotation"
2527

2628
@classmethod
27-
def find_all(cls, grid: _grid.Grid):
29+
def find_all(cls, grid: _grid.Grid) -> list[Annotation]:
2830
"""Return all of the text annotations present in the grid."""
29-
out: list[cls] = []
31+
out: list[Annotation] = []
3032
for y, line in enumerate(grid.lines):
3133
for match in ANNOTATION_RE.finditer(line):
3234
x = match.span()[0]

schemascii/data.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class Section(dict):
2828
"""Section of data relevant to one portion of the drawing."""
2929

3030
header: str
31-
data: dict
31+
data: dict[str, typing.Any]
3232

33-
def __getitem__(self, key):
33+
def __getitem__(self, key: str):
3434
return self.data[key]
3535

36-
def matches(self, name) -> bool:
36+
def matches(self, name: str) -> bool:
3737
"""True if self.header matches the name."""
38-
return fnmatch.fnmatch(name, self.header)
38+
return fnmatch.fnmatch(name.lower(), self.header.lower())
3939

4040

4141
@dataclass
@@ -80,22 +80,22 @@ def parse_from_string(cls, text: str, startline=1, filename="") -> Data:
8080
def complain(msg):
8181
raise _errors.DiagramSyntaxError(
8282
f"{filename} line {line+startline}: {msg}\n"
83-
f" {lines[line]}\n"
84-
f" {' ' * col}{'^'*len(look())}".lstrip())
83+
f"{line} | {lines[line]}\n"
84+
f"{' ' * len(str(line))} | {' ' * col}{'^'*len(look())}")
8585

8686
def complain_eof():
8787
restore(lastsig)
8888
skip_space(True)
8989
if index >= len(tokens):
9090
complain("unexpected EOF")
91-
complain("cannot parse after this")
91+
complain("unknown parse error")
9292

93-
def look():
93+
def look() -> str:
9494
if index >= len(tokens):
9595
return "\0"
9696
return tokens[index]
9797

98-
def eat():
98+
def eat() -> str:
9999
nonlocal line
100100
nonlocal col
101101
nonlocal index
@@ -150,7 +150,7 @@ def skip_i(newlines: bool = True):
150150
if not skip_space():
151151
return
152152

153-
def expect(expected: set[str]):
153+
def expect_and_eat(expected: set[str]):
154154
got = look()
155155
if got in expected:
156156
eat()
@@ -169,15 +169,15 @@ def parse_section() -> Section:
169169
# print("** starting section", repr(name))
170170
mark_used()
171171
skip_i()
172-
expect({"{"})
172+
expect_and_eat({"{"})
173173
data = {}
174174
while look() != "}":
175175
data |= parse_kv_pair()
176176
eat() # the "}"
177177
skip_i()
178178
return Section(name, data)
179179

180-
def parse_kv_pair() -> dict:
180+
def parse_kv_pair() -> dict[str, int | float | str]:
181181
skip_i()
182182
if look() == "}":
183183
# handle case of ";}"
@@ -187,7 +187,7 @@ def parse_kv_pair() -> dict:
187187
key = eat()
188188
mark_used()
189189
skip_i()
190-
expect({"="})
190+
expect_and_eat({"="})
191191
skip_space()
192192
expect_not(SPECIAL)
193193
value = ""
@@ -214,7 +214,7 @@ def parse_kv_pair() -> dict:
214214
pass
215215
# don't eat the ending "}"
216216
if look() != "}":
217-
expect({"\n", ";"})
217+
expect_and_eat({"\n", ";"})
218218
# print("*** got KV", repr(key), repr(value))
219219
return {key: value}
220220

@@ -263,4 +263,3 @@ def __or__(self, other: Data | dict[str, typing.Any] | typing.Any) -> Data:
263263
my_data = Data.parse_from_string(text)
264264
pprint.pprint(my_data)
265265
pprint.pprint(my_data.get_values_for("R1"))
266-
print(my_data.getopt("R1", "foo"))

schemascii/grid.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def clip(self, p1: complex, p2: complex):
7575

7676
def shrink(self):
7777
"""Shrinks self so that there is not any space between the edges and
78-
the next non-printing character. Takes masks into account.
78+
the next non-whitespace character. Takes masks into account.
7979
"""
8080
# clip the top lines
8181
while all(self.get(complex(x, 0)).isspace()
@@ -95,6 +95,7 @@ def shrink(self):
9595
this_indent = len(line) - len(line.lstrip())
9696
min_indent = min(min_indent, this_indent)
9797
# chop the space
98+
# TODO: for left and right, need to take into account the mask array
9899
if min_indent > 0:
99100
self.width -= min_indent
100101
for line in self.data:

schemascii/net.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def find_all(cls, grid: _grid.Grid) -> list[Net]:
2121
"""Return a list of all the wire nets found on the grid.
2222
"""
2323
seen_points: set[complex] = set()
24-
all_nets: list[cls] = []
24+
all_nets: list[Net] = []
2525
all_tags = _wt.WireTag.find_all(grid)
2626

2727
for y, line in enumerate(grid.lines):

schemascii/refdes.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def short_name(self) -> str:
5656
U3A
5757
Q1G1
5858
R.Heater
59+
^
60+
this one is invalid; only the "R" and "H" are gotten
5961
GND
6062
""")
6163
rds = RefDes.find_all(gg)

schemascii/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def from_phase(cls, pt: complex) -> Side:
7070
"""Return the side that is closest to pt, if it is interpreted as
7171
a vector originating from the origin.
7272
"""
73+
# TODO: fix this so it compares the components,
74+
# instead of this distance mess
7375
ops = {
7476
-pi: Side.LEFT,
7577
pi: Side.LEFT,

schemascii/wire_tag.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WireTag(_dc.DataConsumer, namespaces=(":wire-tag",)):
3333
@classmethod
3434
def find_all(cls, grid: _grid.Grid) -> list[WireTag]:
3535
"""Find all of the wire tags present in the grid."""
36-
out: list[cls] = []
36+
out: list[WireTag] = []
3737
for y, line in enumerate(grid.lines):
3838
for match in WIRE_TAG_PAT.finditer(line):
3939
left_grp, right_grp = match.groups()

0 commit comments

Comments
 (0)