forked from Changes729/kicad_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kisexp.py
83 lines (76 loc) · 1.83 KB
/
kisexp.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 12 10:08:59 2020
"""
import io
QUOTE = {
"'":"'",
'"':'"',
}
BRACKETS = {'(': ')'}
BRACKETS_END = {')':1}
SPACE = {' ':1, '\t':1, '\r':1, '\n':1}
def quoteData(content, index):
data = ""
q_e = QUOTE[content[index]]
c_len = len(content)
escape = False
index+=1;
while index < c_len:
c = content[index]
if c == '\\':
escape = True
elif c == q_e:
if(not escape):
return data, index+1
else:
data += c;
else:
escape = False
data += c
index+=1
return data, index+1
def normalData(content, index):
data = ""
c_len = len(content)
while index < c_len:
c = content[index]
if c in SPACE:
index +=1
break
elif c in BRACKETS:
break
elif c in BRACKETS_END:
break
else:
data += c
index+=1
return data, index
def parseSexp(content, index = 0):
res = []
c_len = len(content)
data = ""
bracket_end = ""
while index < c_len:
c = content[index]
if c in BRACKETS:
bracket_end = BRACKETS[c]
data, index = parseSexp(content, index+1)
res.append(data)
elif c in QUOTE:
data, index = quoteData(content, index)
res.append(data)
elif c in SPACE:
index+=1;
elif c in BRACKETS_END:
index+=1
break
else:
data, index = normalData(content, index)
res.append(data)
return res, index
def loadKicadNet(filename):
file = io.open(filename, "r", encoding="utf-8")
data = file.read()
res = parseSexp(data)
return res[0][0]