Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for updating children units symbol ID #41

Merged
merged 9 commits into from
Feb 14, 2023
34 changes: 28 additions & 6 deletions src/kiutils/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from dataclasses import dataclass, field
from typing import Optional, List
from os import path
import re

from kiutils.items.common import Effects, Position, Property, Font
from kiutils.items.syitems import *
Expand Down Expand Up @@ -243,9 +244,15 @@ class Symbol():
units: List = field(default_factory=list)
"""The `units` can be one or more child symbol tokens embedded in a parent symbol"""

unitId: int = None
eeintech marked this conversation as resolved.
Show resolved Hide resolved
"""Unit identifier: an integer that identifies which unit the symbol represents"""

styleId: int = None
eeintech marked this conversation as resolved.
Show resolved Hide resolved
"""Style identifier: indicates which body style the unit represents"""

@classmethod
def from_sexpr(cls, exp: list):
"""Convert the given S-Expresstion into a Symbol object
"""Convert the given S-Expression into a Symbol object

Args:
exp (list): Part of parsed S-Expression `(symbol ...)`
Expand Down Expand Up @@ -281,7 +288,17 @@ def from_sexpr(cls, exp: list):
if item[0] == 'on_board': object.onBoard = True if item[1] == 'yes' else False
if item[0] == 'power': object.isPower = True

if item[0] == 'symbol': object.units.append(Symbol().from_sexpr(item))
if item[0] == 'symbol':
eeintech marked this conversation as resolved.
Show resolved Hide resolved
# Get symbol unit
eeintech marked this conversation as resolved.
Show resolved Hide resolved
symbol_unit = Symbol().from_sexpr(item)
# Parse symbol unit identifiers
symbol_id_parse = re.match(r"^" + re.escape(object.id) + r"_(\d+?)_(\d+?)$", symbol_unit.id)
if not symbol_id_parse:
raise Exception(f'Failed to parse symbol unit identifiers due to invalid format: {symbol_unit.id}')
symbol_unit.unitId = int(symbol_id_parse.group(1))
symbol_unit.styleId = int(symbol_id_parse.group(2))
# Add symbol unit to s-expr
object.units.append(symbol_unit)
if item[0] == 'property': object.properties.append(Property().from_sexpr(item))

if item[0] == 'pin': object.pins.append(SymbolPin().from_sexpr(item))
Expand Down Expand Up @@ -327,7 +344,7 @@ def create_new(cls, id: str, reference: str, value: str,
)
return symbol

def to_sexpr(self, indent: int = 2, newline: bool = True) -> str:
def to_sexpr(self, indent: int = 2, newline: bool = True, parent_id = None) -> str:
"""Generate the S-Expression representing this object

Args:
Expand All @@ -353,16 +370,21 @@ def to_sexpr(self, indent: int = 2, newline: bool = True) -> str:
pinnames = f' (pin_names{pnoffset}{pnhide})' if self.pinNames else ''
pinnumbers = f' (pin_numbers hide)' if self.hidePinNumbers else ''
extends = f' (extends "{dequote(self.extends)}")' if self.extends is not None else ''

expression = f'{indents}(symbol "{dequote(self.id)}"{extends}{power}{pinnumbers}{pinnames}{inbom}{onboard}\n'

# Construct Symbol Unit Identifier
symbol_id = dequote(self.id)
if parent_id and self.unitId and self.styleId:
symbol_id = f'{dequote(parent_id)}_{str(self.unitId)}_{str(self.styleId)}'
eeintech marked this conversation as resolved.
Show resolved Hide resolved

expression = f'{indents}(symbol "{symbol_id}"{extends}{power}{pinnumbers}{pinnames}{inbom}{onboard}\n'
for item in self.properties:
expression += item.to_sexpr(indent+2)
for item in self.graphicItems:
expression += item.to_sexpr(indent+2)
for item in self.pins:
expression += item.to_sexpr(indent+2)
for item in self.units:
expression += item.to_sexpr(indent+2)
expression += item.to_sexpr(indent+2, parent_id=self.id)
expression += f'{indents}){endline}'
return expression

Expand Down