Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions kicad_pcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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`
A usage demonstration is available in `test_pcb.py`
'''

try:
Expand Down Expand Up @@ -41,7 +41,7 @@ def _parse1_layers(self,data):
return Sexp(data[1],data[2:],data[0])


class KicadPCB_module(SexpParser):
class KicadPCB_footprint(SexpParser):
__slots__ = ()
_default_bools = 'locked'
_parse_fp_text = KicadPCB_gr_text
Expand All @@ -51,42 +51,43 @@ class KicadPCB_module(SexpParser):
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',
'segment',
'arc',
'via',
['module'] + _module,
['footprint'] + _module,
('zone',
'filled_polygon'))
_footprint = [
'fp_text',
'fp_line',
'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',
['footprint'] + _footprint,
['module'] + _footprint,
('zone', 'filled_polygon'))

_alias_keys = {'footprint' : 'module'}
_parse_module = KicadPCB_module
_parse_footprint = KicadPCB_module
_parse_footprint = KicadPCB_footprint
_parse_footprint = KicadPCB_footprint
_parse_gr_text = KicadPCB_gr_text

def export(self, out, indent=' '):
exportSexp(self,out,'',indent)
exportSexp(self, out, '', indent)

def getError(self):
return getSexpError(self)

@staticmethod
def load(filename, quote_no_parse=None):
with open(filename,'r') as f:
with open(filename, 'r') as f:
return KicadPCB(parseSexp(f.read(), quote_no_parse))

112 changes: 112 additions & 0 deletions kicad_sch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'''

``kicad_sch`` parser using `sexp_parser.SexpParser`

The parser `KicadSCH` 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_sch.py`
'''

try:
from .sexp_parser import *
except ImportError:
from sexp_parser.sexp_parser import *

__author__ = "Heck, Leandro"
__copyright__ = "Copyright 2022, Leandro Heck"
__license__ = "MIT"
__version__ = "1.0.0"
__email__ = "leoheck@gmail.com"
__status__ = "Prototype"


class KicadSCH_gr_text(SexpParser):
__slots__ = ()
_default_bools = 'hide'


class KicadSCH_drill(SexpParser):
__slots__ = ()
_default_bools = 'oval'


class KicadSCH_pad(SexpParser):
__slots__ = ()
_parse1_drill = KicadSCH_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 KicadSCH_symbol(SexpParser):
__slots__ = ()
_default_bools = 'locked'
_parse_fp_text = KicadSCH_gr_text
_parse_pad = KicadSCH_pad


class KicadSCH(SexpParser):

# To make sure the following key exists, and is of type SexpList

_symbol = [
'lib_id',
'in_bom',
'on_board',
'uuid',
'property',
'pin'
]

_sheet_instances = [
'path',
]

_defaults = (
'uuid',
'paper',
'title_block',
'lib_symbols',
'junction',
'wire',
'text',
'label'
'symbol',
'sheet_instances',
'symbol_instances'
)


# _defaults = ('net',
# ('net_class','add_net'),
# 'dimension',
# 'gr_text',
# 'gr_line',
# 'gr_circle',
# 'gr_arc',
# 'gr_curve',
# 'segment',
# 'arc',
# 'via',
# ['module'] + _footprint,
# ['footprint'] + _footprint,
# ('zone', 'filled_polygon'))

_parse_symbol = KicadSCH_symbol
_parse_symbol = KicadSCH_symbol
_parse_gr_text = KicadSCH_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):
with open(filename, 'r') as f:
return KicadSCH(parseSexp(f.read(), quote_no_parse))
Loading