Skip to content

Commit 5946f3b

Browse files
committed
fix:fix pin issues and saving and loading from yaml file for pins. now subcircuits have pins.
1 parent d4c5093 commit 5946f3b

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

PySpice/Spice/Library/SpiceInclude.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import hashlib
3333
import logging
3434
import os
35-
35+
import re
3636
import yaml
3737

3838
from PySpice.Spice.Parser import SpiceFile, ParseError
@@ -183,8 +183,39 @@ def _parse_nodes(self, nodes: list[str]) -> None:
183183
for index, node_str in enumerate(nodes):
184184
internal_node = None
185185
name = None
186+
if isinstance(node_str, dict):
187+
# if we are hear we probably have read the yaml file
188+
internal_node = node_str['internal_node']
189+
node_str = node_str['name']
190+
if isinstance(node_str, str):
191+
node_str = node_str.strip()
192+
# make sure the node is a valid spice node identifier
193+
reg_ex = r'''
194+
(?i: # case-insensitive
195+
(?:[+\-\%](?=[a-z_]))? # optional +, - or % at the start, only if followed by letter/underscore
196+
[a-z0-9_]+ # one or more letters/digits/underscores
197+
(?:\.[a-z0-9_]+)? # optionally a dot, followed by one or more letters/digits/underscores
198+
(?:(?<=[a-z0-9])[+\-] # optionally a plus or minus if it's right after a letter/digit
199+
(?![a-z0-9.]) # and not followed by letter/digit/dot
200+
)?
201+
)(?!:) # negative lookahead: do not allow a colon immediately after
202+
'''
203+
# Using VERBOSE to ignore whitespace/comments in the regex
204+
pattern = re.compile(reg_ex, re.VERBOSE)
205+
match = pattern.fullmatch(node_str)
206+
if match:
207+
name = match.group(0)
208+
if not internal_node:
209+
internal_node = index + 1
210+
# continue
211+
else:
212+
self._logger.warning(f"Invalid pin format {node_str} for {self.name}")
213+
# self._valid = False
214+
# return
215+
continue
186216
if isinstance(node_str, int):
187217
internal_node = node_str
218+
name = f'{internal_node}'
188219
else:
189220
node_str = node_str.strip()
190221
i = node_str.find(' ')
@@ -232,7 +263,7 @@ def pin_names(self) -> list[str]:
232263

233264
def to_yaml(self) -> dict:
234265
_ = super().to_yaml()
235-
_.update({'nodes': self._nodes})
266+
_.update({'nodes': [{'index': _.index, 'name': _.name, 'internal_node': _.internal_node} for _ in self._nodes]})
236267
return _
237268

238269
##############################################

0 commit comments

Comments
 (0)