@@ -28,14 +28,14 @@ class Section(dict):
28
28
"""Section of data relevant to one portion of the drawing."""
29
29
30
30
header : str
31
- data : dict
31
+ data : dict [ str , typing . Any ]
32
32
33
- def __getitem__ (self , key ):
33
+ def __getitem__ (self , key : str ):
34
34
return self .data [key ]
35
35
36
- def matches (self , name ) -> bool :
36
+ def matches (self , name : str ) -> bool :
37
37
"""True if self.header matches the name."""
38
- return fnmatch .fnmatch (name , self .header )
38
+ return fnmatch .fnmatch (name . lower () , self .header . lower () )
39
39
40
40
41
41
@dataclass
@@ -80,22 +80,22 @@ def parse_from_string(cls, text: str, startline=1, filename="") -> Data:
80
80
def complain (msg ):
81
81
raise _errors .DiagramSyntaxError (
82
82
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 ())} " )
85
85
86
86
def complain_eof ():
87
87
restore (lastsig )
88
88
skip_space (True )
89
89
if index >= len (tokens ):
90
90
complain ("unexpected EOF" )
91
- complain ("cannot parse after this " )
91
+ complain ("unknown parse error " )
92
92
93
- def look ():
93
+ def look () -> str :
94
94
if index >= len (tokens ):
95
95
return "\0 "
96
96
return tokens [index ]
97
97
98
- def eat ():
98
+ def eat () -> str :
99
99
nonlocal line
100
100
nonlocal col
101
101
nonlocal index
@@ -150,7 +150,7 @@ def skip_i(newlines: bool = True):
150
150
if not skip_space ():
151
151
return
152
152
153
- def expect (expected : set [str ]):
153
+ def expect_and_eat (expected : set [str ]):
154
154
got = look ()
155
155
if got in expected :
156
156
eat ()
@@ -169,15 +169,15 @@ def parse_section() -> Section:
169
169
# print("** starting section", repr(name))
170
170
mark_used ()
171
171
skip_i ()
172
- expect ({"{" })
172
+ expect_and_eat ({"{" })
173
173
data = {}
174
174
while look () != "}" :
175
175
data |= parse_kv_pair ()
176
176
eat () # the "}"
177
177
skip_i ()
178
178
return Section (name , data )
179
179
180
- def parse_kv_pair () -> dict :
180
+ def parse_kv_pair () -> dict [ str , int | float | str ] :
181
181
skip_i ()
182
182
if look () == "}" :
183
183
# handle case of ";}"
@@ -187,7 +187,7 @@ def parse_kv_pair() -> dict:
187
187
key = eat ()
188
188
mark_used ()
189
189
skip_i ()
190
- expect ({"=" })
190
+ expect_and_eat ({"=" })
191
191
skip_space ()
192
192
expect_not (SPECIAL )
193
193
value = ""
@@ -214,7 +214,7 @@ def parse_kv_pair() -> dict:
214
214
pass
215
215
# don't eat the ending "}"
216
216
if look () != "}" :
217
- expect ({"\n " , ";" })
217
+ expect_and_eat ({"\n " , ";" })
218
218
# print("*** got KV", repr(key), repr(value))
219
219
return {key : value }
220
220
@@ -263,4 +263,3 @@ def __or__(self, other: Data | dict[str, typing.Any] | typing.Any) -> Data:
263
263
my_data = Data .parse_from_string (text )
264
264
pprint .pprint (my_data )
265
265
pprint .pprint (my_data .get_values_for ("R1" ))
266
- print (my_data .getopt ("R1" , "foo" ))
0 commit comments