5353
5454
5555def parse_netlist_and_extract_elements (
56- netlist_text ,
57- spice_type : SupportedSpiceTypes = "lumerical"
56+ netlist_text , spice_type : SupportedSpiceTypes = "lumerical"
5857):
5958 elements = [] # To store elements like components and subcircuits
6059 connections = [] # To store connections (routes) between elements
6160 settings = [] # To store settings like .MODEL definitions
6261
63- if spice_type == "lumerical" :
62+ if spice_type == "lumerical" :
6463 lines = netlist_text .split ("\n " )
6564 for line in lines :
6665 line = line .strip ()
6766 # Skip comments and empty lines
6867 if not line or line .startswith ("*" ):
6968 continue
70-
69+
7170 # Handle .subckt, .ends, and .MODEL lines specifically
7271 if line .startswith (".subckt" ):
7372 parts = re .split (r"\s+" , line )
@@ -97,7 +96,7 @@ def parse_netlist_and_extract_elements(
9796 component_name = parts [i ]
9897 settings .append (parts [i + 1 :])
9998 break
100-
99+
101100 elements .append (
102101 {
103102 "type" : "component" ,
@@ -108,54 +107,61 @@ def parse_netlist_and_extract_elements(
108107 }
109108 )
110109
111-
112- elif spice_type == "xschem" :
110+ elif spice_type == "xschem" :
113111 lines = netlist_text .split ("\n " )
114112 for line in lines :
115113 line = line .strip ()
116- if not line or line .startswith ('v {' ): # Skip the version line and empty lines
114+ if not line or line .startswith (
115+ "v {"
116+ ): # Skip the version line and empty lines
117117 continue
118-
119- if line .startswith (' C {' ):
118+
119+ if line .startswith (" C {" ):
120120 parts = line .split (" " )
121- component_info = parts [1 ].strip ('{}' )
121+ component_info = parts [1 ].strip ("{}" )
122122 x , y = parts [2 ], parts [3 ]
123123 attributes_str = " " .join (parts [4 :])
124- match = re .search (r'\{(.*)\}' , attributes_str ) # Attempt to find the attributes
125-
124+ match = re .search (
125+ r"\{(.*)\}" , attributes_str
126+ ) # Attempt to find the attributes
127+
126128 if match : # Check if a match was found
127129 attributes = match .group (1 )
128- name_match = re .search (r' name=([^ ]+)' , attributes )
129- label_match = re .search (r' lab=([^}\n]+)' , attributes )
130- name = name_match .group (1 ) if name_match else ''
131- label = label_match .group (1 ) if label_match else ''
132-
133- additional_settings = re .findall (r' (\w+)=([^\s}]+)' , attributes )
130+ name_match = re .search (r" name=([^ ]+)" , attributes )
131+ label_match = re .search (r" lab=([^}\n]+)" , attributes )
132+ name = name_match .group (1 ) if name_match else ""
133+ label = label_match .group (1 ) if label_match else ""
134+
135+ additional_settings = re .findall (r" (\w+)=([^\s}]+)" , attributes )
134136 settings_dict = dict (additional_settings )
135-
136- elements .append ({
137- "type" : "component" ,
138- "component_type" : component_info ,
139- "position" : {"x" : x , "y" : y },
140- "name" : name ,
141- "label" : label ,
142- "settings" : settings_dict
143- })
137+
138+ elements .append (
139+ {
140+ "type" : "component" ,
141+ "component_type" : component_info ,
142+ "position" : {"x" : x , "y" : y },
143+ "name" : name ,
144+ "label" : label ,
145+ "settings" : settings_dict ,
146+ }
147+ )
144148 else :
145149 # Handle lines that do not match the expected format
146150 print (f"Warning: Line skipped due to unexpected format: { line } " )
147- elif line .startswith ('N ' ):
151+ elif line .startswith ("N " ):
148152 parts = line .split (" " )
149153 x1 , y1 , x2 , y2 = parts [1 ], parts [2 ], parts [3 ], parts [4 ]
150154 attributes_str = " " .join (parts [5 :])
151- label_match = re .search (r'lab=([^}\n]+)' , attributes_str )
152- label = label_match .group (1 ) if label_match else ''
153-
154- connections .append ({
155- "start" : {"x" : x1 , "y" : y1 },
156- "end" : {"x" : x2 , "y" : y2 },
157- "label" : label
158- })
155+ label_match = re .search (r"lab=([^}\n]+)" , attributes_str )
156+ label = label_match .group (1 ) if label_match else ""
157+
158+ connections .append (
159+ {
160+ "start" : {"x" : x1 , "y" : y1 },
161+ "end" : {"x" : x2 , "y" : y2 },
162+ "label" : label ,
163+ }
164+ )
159165
160166 return elements , connections , settings
161167
0 commit comments