-
Notifications
You must be signed in to change notification settings - Fork 16
/
core.py
116 lines (92 loc) · 3 KB
/
core.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
#!/usr/bin/env python3
import os
import sys
vcdParsers = []
try:
from pyDigitalWaveTools.vcd.parser import VcdParser
vcdParsers.append('pyDigitalWaveTools')
except ModuleNotFoundError:
pass
try:
from .vcdvcd import VCDVCD
vcdParsers.append('vcdvcd')
except ModuleNotFoundError:
pass
if not vcdParsers:
print('Error no VCD parser has been found...')
sys.exit(-1)
print(f'Available VCD parsers: {vcdParsers}')
here = os.path.dirname(os.path.realpath(__file__))
def renameKey(dictionary, oldkey, newkey):
if newkey != oldkey:
dictionary[newkey] = dictionary[oldkey]
del dictionary[oldkey]
def format_pyDigitalWaveTools(data):
now = [0]
def traverse(node, hierlist):
name = node['type']['name']
del node['type']['name']
node.update(node['type'])
node['type'] = name
try:
for c in node['children']:
traverse(c, hierlist+[node['name']])
except KeyError:
# No child:
node['hierarcy'] = hierlist
node['wave'] = [{'time': item[0], 'bin': item[1]}
for item in node['data']]
del node['data']
now[0] = max(node['wave'][-1]['time'], now[0])
return node
ret = traverse(data, [])
ret['now'] = now[0]
return ret
def format_vcdvcd(data):
now = [0]
for vcdID, signal in data.items():
try:
renameKey(signal, 'var_type', 'type')
renameKey(signal, 'size', 'width')
try:
signal['wave'] = [{'time': item[0], 'bin': item[1]}
for item in signal['tv']]
del signal['tv']
now[0] = max(signal['wave'][-1]['time'], now[0])
except KeyError:
signal['wave'] = []
signal['vcdid'] = vcdID
hierarcy = signal['references'][0].split('.')
signal['name'] = hierarcy[-1]
except:
print(f'Error at: {vcdID}')
raise
return {
'children': list(data.values()),
"name": "dduummyy",
"now": now[0],
"type": "struct"
}
def parseWith_pyDigitalWaveTools(fname):
with open(fname) as vcd_file:
vcd = VcdParser()
vcd.parse(vcd_file)
data = vcd.scope.toJson()
return format_pyDigitalWaveTools(data)
def parseWith_vcdvcd(fname=None, content=None):
if content:
fname = None
vcd = VCDVCD(fname, content)
data = vcd.get_data()
return format_vcdvcd(data)
def parseFile(fname=None, content=None):
try:
print('Parse with parseWith_vcdvcd...')
ret = parseWith_vcdvcd(fname, content)
print('Parse with parseWith_vcdvcd... OK')
except Exception as ex:
print(ex)
print('Parse with parseWith_pyDigitalWaveTools...')
ret = parseWith_pyDigitalWaveTools(fname)
print('Parse with parseWith_pyDigitalWaveTools... OK')
return ret