protoboard: remove match syntax for python 3.8

wip
jaseg 2022-06-21 13:54:05 +02:00
rodzic 0ecb612d59
commit a01e44b142
2 zmienionych plików z 76 dodań i 53 usunięć

Wyświetl plik

@ -73,10 +73,16 @@ class PatternProtoArea:
def __init__(self, pitch_x, pitch_y=None, border=None): def __init__(self, pitch_x, pitch_y=None, border=None):
self.pitch_x = pitch_x self.pitch_x = pitch_x
self.pitch_y = pitch_y or pitch_x self.pitch_y = pitch_y or pitch_x
match border:
case None: self.border = (0, 0, 0, 0) if border is None:
case (t, r, b, l): self.border = border self.border = (0, 0, 0, 0)
case _: self.border = (border, border, border, border) elif hasattr(border, '__iter__'):
if len(border == 4):
self.border = border
else:
raise TypeError('border must be None, int, or a 4-tuple of floats (top, right, bottom, left)')
else:
self.border = (border, border, border, border)
@property @property
def pitch(self): def pitch(self):
@ -105,10 +111,16 @@ class PatternProtoArea:
class EmptyProtoArea: class EmptyProtoArea:
def __init__(self, copper=False, border=None): def __init__(self, copper=False, border=None):
self.copper = copper self.copper = copper
match border:
case None: self.border = (0, 0, 0, 0) if border is None:
case (t, r, b, l): self.border = border self.border = (0, 0, 0, 0)
case _: self.border = (border, border, border, border) elif hasattr(border, '__iter__'):
if len(border == 4):
self.border = border
else:
raise TypeError('border must be None, int, or a 4-tuple of floats (top, right, bottom, left)')
else:
self.border = (border, border, border, border)
def generate(self, x, y, w, h, defs=None, center=True, clip=''): def generate(self, x, y, w, h, defs=None, center=True, clip=''):
if self.copper: if self.copper:
@ -189,10 +201,16 @@ class ProtoBoard:
self.layout = parse_layout(expr) self.layout = parse_layout(expr)
self.mounting_holes = mounting_holes self.mounting_holes = mounting_holes
self.center = center self.center = center
match border:
case None: self.border = (0, 0, 0, 0) if border is None:
case (t, r, b, l): self.border = border self.border = (0, 0, 0, 0)
case _: self.border = (border, border, border, border) elif hasattr(border, '__iter__'):
if len(border == 4):
self.border = border
else:
raise TypeError('border must be None, int, or a 4-tuple of floats (top, right, bottom, left)')
else:
self.border = (border, border, border, border)
def generate(self, w, h): def generate(self, w, h):
out = {l: [] for l in LAYERS} out = {l: [] for l in LAYERS}
@ -237,12 +255,17 @@ class ProtoBoard:
def convert_to_mm(value, unit): def convert_to_mm(value, unit):
match unit.lower(): unitl = unit.lower()
case 'mm': return value if unitl == 'mm':
case 'cm': return value*10 return value
case 'in': return value*25.4 elif unitl == 'cm':
case 'mil': return value/1000*25.4 return value*10
raise ValueError(f'Invalid unit {unit}, allowed units are mm, cm, in, and mil.') elif unitl == 'in':
return value*25.4
elif unitl == 'mil':
return value/1000*25.4
else:
raise ValueError(f'Invalid unit {unit}, allowed units are mm, cm, in, and mil.')
value_re = re.compile('([0-9]*\.?[0-9]+)(cm|mm|in|mil|%)') value_re = re.compile('([0-9]*\.?[0-9]+)(cm|mm|in|mil|%)')
def eval_value(value, total_length=None): def eval_value(value, total_length=None):
@ -336,53 +359,53 @@ class TwoSideLayout:
yield from map(self.flip, self.bottom.generate(x, y, w, h, defs, center, clip)) yield from map(self.flip, self.bottom.generate(x, y, w, h, defs, center, clip))
def _map_expression(node): def _map_expression(node):
match node: if isinstance(node, ast.Name):
case ast.Name(): return node.id
return node.id
case ast.Constant(): elif isinstance(node, ast.Constant):
return node.value return node.value
case ast.BinOp(op=ast.BitOr()) | ast.BinOp(op=ast.BitAnd()) | ast.BinOp(op=ast.Add()):
left_prop = right_prop = None
left, right = node.left, node.right elif isinstance(node, ast.BinOp) and isinstance(node.op, (ast.BitOr, ast.BitAnd, ast.Add)):
left_prop = right_prop = None
if isinstance(left, ast.BinOp) and isinstance(left.op, ast.MatMult): left, right = node.left, node.right
left_prop = _map_expression(left.right)
left = left.left
if isinstance(right, ast.BinOp) and isinstance(right.op, ast.MatMult): if isinstance(left, ast.BinOp) and isinstance(left.op, ast.MatMult):
right_prop = _map_expression(right.right) left_prop = _map_expression(left.right)
right = right.left left = left.left
left, right = _map_expression(left), _map_expression(right) if isinstance(right, ast.BinOp) and isinstance(right.op, ast.MatMult):
right_prop = _map_expression(right.right)
right = right.left
direction = 'h' if isinstance(node.op, ast.BitOr) else 'v' left, right = _map_expression(left), _map_expression(right)
if isinstance(left, PropLayout) and left.direction == direction and left_prop is None:
left.content.append(right)
left.proportions.append(right_prop)
return left
elif isinstance(right, PropLayout) and right.direction == direction and right_prop is None: direction = 'h' if isinstance(node.op, ast.BitOr) else 'v'
right.content.insert(0, left) if isinstance(left, PropLayout) and left.direction == direction and left_prop is None:
right.proportions.insert(0, left_prop) left.content.append(right)
return right left.proportions.append(right_prop)
return left
elif isinstance(node.op, ast.Add): elif isinstance(right, PropLayout) and right.direction == direction and right_prop is None:
if left_prop or right_prop: right.content.insert(0, left)
raise SyntaxError(f'Proportions ("@") not supported for two-side layout ("+")') right.proportions.insert(0, left_prop)
return right
return TwoSideLayout(left, right) elif isinstance(node.op, ast.Add):
if left_prop or right_prop:
raise SyntaxError(f'Proportions ("@") not supported for two-side layout ("+")')
else: return TwoSideLayout(left, right)
return PropLayout([left, right], direction, [left_prop, right_prop])
case ast.BinOp(op=ast.MatMult()):
raise SyntaxError(f'Unexpected width specification "{ast.unparse(node.right)}"')
case _: else:
raise SyntaxError(f'Invalid layout expression "{ast.unparse(node)}"') return PropLayout([left, right], direction, [left_prop, right_prop])
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.MatMult):
raise SyntaxError(f'Unexpected width specification "{ast.unparse(node.right)}"')
else:
raise SyntaxError(f'Invalid layout expression "{ast.unparse(node)}"')
def parse_layout(expr): def parse_layout(expr):
''' Example layout: ''' Example layout:

Wyświetl plik