forked from realthunder/kicad_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kicad_pcb.py
94 lines (72 loc) · 2.4 KB
/
kicad_pcb.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
'''
``kicad_pcb`` parser using `sexp_parser.SexpParser`
The parser `KicadPCB` demonstrates the usage of a more general S-expression
parser of class `sexp_parser.SexpParser`. Check out the source to see how easy
it is to implement a parser in an almost declarative way.
A usage demonstration is available in `test.py`
'''
try:
from .sexp_parser import *
except ImportError:
from sexp_parser.sexp_parser import *
__author__ = "Zheng, Lei"
__copyright__ = "Copyright 2016, Zheng, Lei"
__license__ = "MIT"
__version__ = "1.0.0"
__email__ = "[email protected]"
__status__ = "Prototype"
class KicadPCB_gr_text(SexpParser):
__slots__ = ()
_default_bools = 'hide'
class KicadPCB_drill(SexpParser):
__slots__ = ()
_default_bools = 'oval'
class KicadPCB_pad(SexpParser):
__slots__ = ()
_parse1_drill = KicadPCB_drill
def _parse1_layers(self,data):
if not isinstance(data,list) or len(data)<3:
raise ValueError('expects list of more than 2 element')
return Sexp(data[1],data[2:],data[0])
class KicadPCB_module(SexpParser):
__slots__ = ()
_default_bools = 'locked'
_parse_fp_text = KicadPCB_gr_text
_parse_pad = KicadPCB_pad
class KicadPCB(SexpParser):
# To make sure the following key exists, and is of type SexpList
_module = ['fp_text',
'fp_circle',
'fp_arc',
'pad',
'model']
_defaults =('net',
('net_class',
'add_net'),
'dimension',
'gr_text',
'gr_line',
'gr_circle',
'gr_arc',
'gr_curve',
'gr_poly',
'segment',
'arc',
'via',
'module',
['module'] + _module,
['footprint'] + _module,
('zone',
'filled_polygon'))
_alias_keys = {'footprint' : 'module'}
_parse_module = KicadPCB_module
_parse_footprint = KicadPCB_module
_parse_gr_text = KicadPCB_gr_text
def export(self, out, indent=' '):
exportSexp(self,out,'',indent)
def getError(self):
return getSexpError(self)
@staticmethod
def load(filename, quote_no_parse=None, encoding='utf-8'):
with open(filename,'r', encoding=encoding) as f:
return KicadPCB(parseSexp(f.read(), quote_no_parse))