Skip to content

Commit

Permalink
Added assertion for invalid footprint or library names
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Anderson authored and dvc94ch committed Apr 6, 2019
1 parent 6734748 commit 4271afe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
38 changes: 17 additions & 21 deletions pykicad/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def xy_schema(attr):
}
}

###########################
# Footprint components #
###########################
class Net(AST):
tag = 'net'
schema = {
Expand Down Expand Up @@ -305,7 +308,6 @@ def __init__(self, center, end, layer='F.SilkS', width=None, tstamp=None,

def flip(self):
'''Flip a circle.'''

self.layer = flip_layer(self.layer)


Expand Down Expand Up @@ -338,7 +340,6 @@ def __init__(self, start, end, angle, layer='F.SilkS', width=None,

def flip(self):
'''Flip an arc.'''

self.layer = flip_layer(self.layer)
raise NotImplementedError()

Expand All @@ -361,7 +362,6 @@ def __init__(self, pts, layer='F.SilkS', width=None, tstamp=None, status=None):

def flip(self):
'''Flip polygon.'''

self.layer = flip_layer(self.layer)
raise NotImplementedError()

Expand Down Expand Up @@ -411,7 +411,6 @@ def __init__(self, start, bezier1, bezier2, end, layer='F.SilkS',

def flip(self):
'''Flip curve.'''

self.layer = flip_layer(self.layer)
raise NotImplementedError()

Expand Down Expand Up @@ -447,7 +446,6 @@ def __init__(self, path, at, scale, rotate):
super(Model, self).__init__(
path=path, at=at, scale=scale, rotate=rotate)


class Module(AST):
tag = 'module'
schema = {
Expand Down Expand Up @@ -507,6 +505,20 @@ class Module(AST):
'_multiple': True
}
}

@classmethod
def from_file(cls, path):
'''Returns parsed module at specified path'''
module = open(path, 'r', encoding='utf-8').read()
return cls.parse(module)

@classmethod
def from_library(cls, lib, name):
'''Returns parsed module with specified name from specified library'''
path = find_module(lib, name)
assert path is not None, \
"Footprint {0} in Library {1} Not Found!".format(name,lib)
return cls.from_file(find_module(lib, name))

def __init__(self, name, version=None, locked=False, placed=False,
layer='F.Cu', tedit=None, tstamp=None, at=None,
Expand Down Expand Up @@ -546,7 +558,6 @@ def pads_by_name(self, name):
'''Returns a list of pads.
The pads in the list may be in an arbitrary order, or be
non-consecutive. Multiple pads can have the same name.'''

pads = []
for pad in self.pads:
if pad.name == name:
Expand All @@ -557,7 +568,6 @@ def set_reference(self, name):
'''Change the reference/identifier of a module.
Aside from changing the name, we also need to update the
textual elements of type 'reference'.'''

self.name = name
for text in self.texts:
if text.type == 'reference':
Expand All @@ -566,7 +576,6 @@ def set_reference(self, name):
def set_value(self, value):
'''Change the value of a module.
Updates all textual elements of type 'value'.'''

for text in self.texts:
if text.type == 'value':
text.text = value
Expand All @@ -579,26 +588,22 @@ def geometry(self):

def elements_by_layer(self, layer):
'''Returns a iterator of elements on layer.'''

for elem in self.geometry():
if elem.layer == layer:
yield elem

def courtyard(self):
'''Returns the courtyard elements of a module.'''

return list(self.elements_by_layer(self.layer.split('.')[0] + '.CrtYd'))

def place(self, x, y):
'''Sets the x and y coordinates of the module.'''

self.at[0] = x
self.at[1] = y

def rotate(self, angle):
'''Rotates the module by an angle.
Also applies rotation to all text elements and pads.'''

if len(self.at) > 2:
self.at[2] += angle
else:
Expand All @@ -612,7 +617,6 @@ def rotate(self, angle):

def connect(self, pad, net):
'''Sets the net on all pads called :data:pad.'''

for pad in self.pads_by_name(pad):
pad.net = net

Expand All @@ -625,11 +629,3 @@ def flip(self):
for elem in self.geometry():
elem.flip()

@classmethod
def from_file(cls, path):
module = open(path, 'r', encoding='utf-8').read()
return cls.parse(module)

@classmethod
def from_library(cls, lib, name):
return cls.from_file(find_module(lib, name))
15 changes: 0 additions & 15 deletions pykicad/pcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def __init__(self, center, end, layer='Edge.Cuts', width=None,
super(GrCircle, self).__init__(center=center, end=end, layer=layer,
width=width, tstamp=tstamp, status=status)


class GrPolygon(AST):
tag = 'gr_poly'
schema = {
Expand All @@ -141,7 +140,6 @@ def __init__(self, pts, layer='Edge.Cuts', width=None, tstamp=None, status=None)
super(GrPolygon, self).__init__(pts=pts, layer=layer, width=width,
tstamp=tstamp, status=status)


class GrCurve(AST):
tag = 'gr_curve'
schema = {
Expand Down Expand Up @@ -185,7 +183,6 @@ def __init__(self, start, bezier1, bezier2, end, layer='Edge.Cuts',
bezier2=bezier2, end=end, layer=layer,
width=width, tstamp=tstamp, status=status)


class Via(AST):
tag = 'via'
schema = {
Expand All @@ -209,7 +206,6 @@ def __init__(self, at, size, drill, net, micro=False, blind=False, layers=None,
drill=drill, layers=layers, net=net,
tstamp=tstamp, status=status)


class Layer(AST):
tag = ''
schema = {
Expand Down Expand Up @@ -241,7 +237,6 @@ def __init__(self, name, code=None, type='signal', hide=None):

super(Layer, self).__init__(code=code, name=name, type=type, hide=hide)


class NetClass(AST):
tag = 'net_class'
schema = {
Expand Down Expand Up @@ -284,7 +279,6 @@ def __init__(self, name, description='', clearance=None, trace_width=None,
diff_pair_gap=diff_pair_gap,
nets=nets)


class Zone(AST):
tag = 'zone'
schema = {
Expand Down Expand Up @@ -363,7 +357,6 @@ def __init__(self, net=None, net_name=None, layer=None, tstamp=None,
polygon=polygon, filled_polygon=filled_polygon,
fill_segments=fill_segments)


class Target(AST):
tag = 'target'
schema = {
Expand All @@ -385,7 +378,6 @@ def __init__(self, shape, at, size=None, width=None,
super(Target, self).__init__(shape=shape, at=at, size=size, width=width,
layer=layer, tstamp=tstamp)


class Dimension(AST):
tag = 'dimension'
schema = {
Expand Down Expand Up @@ -433,7 +425,6 @@ def __init__(self, value, width, layer='F.SilkS', text=None, feature1=None,
arrow2a=arrow2a, arrow2b=arrow2b,
tstamp=tstamp)


class Setup(AST):
tag = 'setup'
schema = {
Expand Down Expand Up @@ -541,7 +532,6 @@ def __init__(self, user_trace_width=None, trace_clearance=None,
visible_elements=visible_elements,
pcbplotparams=pcbplotparams)


def comment(number):
str_num = str(number)
return {
Expand All @@ -552,7 +542,6 @@ def comment(number):
(str_num, tree_to_string(x)))
}


class Pcb(AST):
tag = 'kicad_pcb'
schema = {
Expand Down Expand Up @@ -756,26 +745,22 @@ def geometry(self):

def elements_by_layer(self, layer):
'''Returns a iterator of elements on layer.'''

for elem in self.geometry():
if elem.layer == layer:
yield elem

def outline(self):
'''Returns the outline of a pcb.'''

return list(self.elements_by_layer('Edge.Cuts'))

def module_by_reference(self, name):
'''Returns a module called name.'''

for module in self.modules:
if module.name == name:
return module

def net_by_code(self, code):
'''Returns a net with code.'''

for net in self.nets:
if net.code == code:
return net
Expand Down
16 changes: 12 additions & 4 deletions pykicad/sexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def ast(ast, attr):


def find_attr(attr, value, schema):
'''
'''
def is_leaf_node(schema):
if not isinstance(schema, dict):
return True
Expand Down Expand Up @@ -333,14 +336,19 @@ def merge_dict(d1, d2):
d1[key] = value

class AST(object):
'''Extraordinarily undocumented AST class'''
'''
Abstract Syntax Tree (AST)
Extraordinarily undocumented AST class
'''
tag = 'sexpr'
schema = text

def __init__(self, **kwargs):
'''Set attributes as kwargs passed to AST initializer'''
self.attributes = kwargs

def __getattr__(self, attr):
'''Checks to see if attr is in attributes otherwise raise AttrError'''
try:
return self.attributes.get(attr)
except KeyError as e:
Expand All @@ -365,7 +373,7 @@ def __repr__(self):
return '(%s %s)' % (self.tag, repr(attrs))

def __str__(self):
return self.to_string()[1:] + '\n'
return self.to_string()[1:]

def to_string(self, attributes=None):
if attributes is None:
Expand All @@ -382,7 +390,6 @@ def to_string(self, attributes=None):
tree = self.attributes
else:
tree = {self.tag: tree}

return tree_to_string(tree)

def init_list(self, arg, default):
Expand All @@ -401,8 +408,9 @@ def parser(cls):

@classmethod
def parse(cls, string):
'''Parses str and returns instance of class passed into func'''
if not hasattr(cls, '_parser'):
cls._parser = generate_parser(cls.tag, cls.schema)
cls._parser = cls.parser()
parse_result = cls._parser.parseString(string)
result = {}
for res in parse_result:
Expand Down

0 comments on commit 4271afe

Please sign in to comment.