from trygvis.eda import cli import rdflib class DigiKeyPart(object): def __init__(self, part_number): self.part_number = part_number self.attributes = {} def set_attribute(self, key, value): self.attributes[key] = value class Component(object): def __init__(self, ref, value): self.ref = ref self.value = value self.fields = {} def set_field(self, field_name, field_value): self.fields[field_name] = field_value def run(schematic_url): cli.with_database(lambda g: work(schematic_url, g)) def work(schematic_url, g): components = {} dk_parts = {} cli.info("Loading components") res = cli.sparql(g, """ SELECT ?ref ?value WHERE { ?schematic a kicad-type:schematic ; kicad:component ?cmp . ?cmp a kicad-type:schematic_component ; kicad:value ?value ; rdfs:label ?ref . } ORDER BY ?ref """, init_bindings={"schematic": rdflib.URIRef(schematic_url)}) for row in res: c = Component(row.ref, row.value) components[row.ref] = c cli.info('ref=%s, value=%s' % (c.ref, c.value)) cli.info("Loading custom component attributes") res = cli.sparql(g, """ SELECT ?ref ?field ?field_name ?field_value WHERE { ?schematic a kicad-type:schematic ; kicad:component ?cmp . ?cmp a kicad-type:schematic_component ; rdfs:label ?ref ; kicad:field ?field . ?field a kicad-type:field ; kicad:field_name ?field_name . ?field a kicad-type:field ; kicad:field_value ?field_value . } ORDER BY ?ref ?field_name """, init_bindings={"schematic": rdflib.URIRef(schematic_url)}) for row in res: c = components[row.ref] assert isinstance(c, Component) c.set_field(row.field_name, row.field_value) cli.info('%5s: %-20s %s' % (c.ref, row.field_name + ':', row.field_value)) cli.info("Loading Digi-Key parts for schematic") res = cli.sparql(g, """ SELECT ?ref ?part_number ?type ?value WHERE { ?schematic kicad:component ?cmp . ?cmp a kicad-type:schematic_component ; rdfs:label ?ref ; kicad:field ?d . ?d kicad:field_name "digikey" ; kicad:field_value ?part_number . ?part a dk:part ; rdfs:label ?label ; dk:partNumber ?part_number ; dk:attribute-value ?attr_value . ?attr_value rdfs:label ?value . ?attr_type a dk:attributeType ; rdfs:label ?type ; dk:value ?attr_value . } ORDER BY ?ref ?attr_type ?attr_value """, init_bindings={"schematic": rdflib.URIRef(schematic_url)}) for row in res: pn = row.part_number if not hasattr(dk_parts, pn): part = DigiKeyPart(pn) dk_parts[pn] = part else: part = dk_parts[pn] part.set_attribute(row.type, row.value) cli.info('%5s: %-20s %s' % (pn, row.type + ':', row.value))