forked from Qucs/qucs-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_models.py
227 lines (168 loc) · 6.33 KB
/
parse_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'''
Hacked parser to gather info about available components from Qucs C++ sources
Note:
* there a few name classes mismatches or inexistent beween qucs / qucs-core
* Basic_BJT - ?
* it skips some HDL based components
'''
import glob
import re
import os
import sys
import pickle
def get_class(s):
# text between parenthesis
return s[s.find("(")+1:s.find(")")]
def scan_repo(repo):
'''
Scan the repository for:
- registered components
- component object properties
input:
- repo -- path to repository
return:
- report -- formatted text table
- data -- dictionary with table data
data[registered_Name]=[model, name, source file, category]
'''
# Get class names and type of model
# ex. Resistor - Lumped
# Diode - Nonlinear
mod = os.path.join(repo, 'qucs/qucs/module.cpp')
print 'Scan registred components: \t %s' %(mod)
regs = [
' REGISTER_LUMPED',
' REGISTER_SOURCE',
' REGISTER_PROBE',
' REGISTER_TRANS',
' REGISTER_NONLINEAR',
' REGISTER_VERILOGA',
' REGISTER_DIGITAL',
' REGISTER_FILE',
' REGISTER_SIMULATION',
' REGISTER_EXTERNAL'
]
# Skip ' REGISTER_PAINT'
mod_reg = {}
for index in range(len(regs)):
kind = regs[index].strip().split('_')[1]
with open(mod) as fm:
for line in fm:
if regs[index] in line:
model_class = get_class(line)
# handle Resistor with multiple parameters
if ',' in model_class:
model_class = model_class.split(',')[0]
mod_reg[model_class] = kind
# Gather component `data` indo dictionary, saved as Picke later on.
# data[registered_Name]=[model, name, source file, category]
data = {}
comp_src = 'qucs/qucs/components/'
comp_dir = os.path.join(repo, comp_src)
print 'Looking for components dir: \t %s%s' %(comp_dir, '*.cpp')
if not os.path.isdir(comp_dir):
sys.exit("Directory not found: <components>"+comp_src)
# list all component sources
devices = glob.glob(comp_dir+'*.cpp')
report = ''
report += '\n%20s' %('-'*80)
report += '\n%-20s | %-20s | %-15s | %-10s' %('Registered Class', 'Model Name', 'Instance Prefix', 'Source')
report += '\n%20s' %('-'*80)
# Parse data in component cpp files
for src in devices:
constr = False
nam = False
verbose = False
with open(src) as fp:
# skip non-device files
skip = ['component.cpp',
'componentdialog.cpp',
'optimizedialog.cpp',
'spicedialog.cpp',
'vafile.cpp',
'vacomponent.cpp',
'verilogfile.cpp',
'vhdlfile.cpp',
'vfile.cpp']
# component cpp file
base = os.path.basename(src)
if base in skip:
continue # skip to next src
# skip moc files
if 'moc.cpp' in base:
continue # skip to next src
if verbose:
print '\n%s' %src
for line in fp:
if not constr:
# look for constructor
if '::' in line:
constr = True
# class name
regName = line.split('::')[0].strip()
if verbose:
print 'register Name:', regName
if ' Model' in line:
# get stuff between " "
model = re.findall('"([^"]*)"', line)[0]
if verbose:
print 'model Name: ', model
# simple bjt has no name, but netlist adds (T) for it
if model=='_BJT':
nam = True
name = 'T'
# logic gates have no name, but netlist adds (Y) for them
if (model == "AND" or model == "NAND" or model == "NOR" or
model == "OR" or model == "XNOR" or model == "XOR"):
nam = True
name = 'Y'
# simple MOSFET has no name, but netlist adds (T) for it
if model=='_MOSFET':
nam = True
name = 'T'
if nam:
if verbose:
print 'instance Name:', name
continue
if not nam:
if 'Name' in line:
if '=' in line:
nam = True
name = re.findall('"([^"]*)"', line)[0]
if verbose:
print 'instance Name:', name
continue
report += '\n%-20s | %-20s | %-15s | %-10s' %(regName, model, name, base)
# try to skip unknown classes from Qucs into Qucs-core
# ignore unregistered
if regName in mod_reg:
data[regName]=[model, name, base, mod_reg[regName]]
else:
print ' Not registered class: ', regName
report += '\n%20s' %('-'*80)
report += '\nTotal: %i' %(len(data.keys()))
report += '\n%20s' %('-'*80)
return report, data
if __name__ == "__main__":
if len(sys.argv) != 2:
print '\nPlease provide full path to Qucs repo clone:'
print '$ python parse_mode.py /path/to/src/qucs'
sys.exit('Not enough arguments!')
repo = str(sys.argv[1])
print ''
print 'Looking for repository: \t %s' %(repo)
if not os.path.isdir(repo):
sys.exit("Directory not found: <repo>"+repo)
# Scan code for registered components and their properties
report, data = scan_repo(repo)
# Save table
report_out = 'qucs_components.txt'
print 'Saving components table: \t', report_out
with open(report_out, 'w') as myFile:
myFile.write(report)
# Save data, to be used on the test reports
# Save a dictionary into a pickle file.
datafile = 'qucs_components_data.p'
print 'Saving data (as Pickle): \t', datafile
pickle.dump( data, open( datafile, "wb" ) )
# parse type from registered