import rdflib import rdflib.term from trygvis.eda import cli from trygvis.eda.digikey import * def work(schematic_url, g): client = DigikeyClient() db = DigikeyDatabase() download_category_tree(db, client) # Dump schematic: # SELECT ?schematic # WHERE { # ?schematic a kicad-type:schematic . # b:rateboard-2_a ? ?. # ?s ?predicate ?object . # } # allComponentsQ = prepareQuery('SELECT ?ref ?value WHERE { ?schematic_url a kicad-type:schematic . ?schematic_url kicad:component ?o . ?o rdfs:label ?ref . ?o kicad:value ?value . }', initNs = initNs) # # for row in g.query(allComponentsQ, initBindings={'schematic_url': schematic_url}): # print("ref=%s, value=%s" % (row.ref, row.value)) res = cli.sparql(g, "SELECT ?schematic WHERE {?schematic a kicad-type:schematic}") print("Found %d schematics in database" % len(res)) for row in res: print("schematic: %s" % row.schematic) res = cli.sparql(g, "SELECT ?dk_part ?dk_part_number WHERE {?dk_part a dk:part ; dk:partNumber ?dk_part_number}") print("Found %d Digikey parts in database" % len(res)) for row in res: print("Part: url=%s, partNumber=%s" % (row.dk_part, row.dk_part_number)) res = cli.sparql(g, """ SELECT ?digikey_pn (group_concat(distinct ?ref;separator=";") as ?refs) WHERE { ?schematic_url kicad:component ?cmp . ?cmp rdfs:label ?ref ; kicad:value ?value . OPTIONAL { ?cmp kicad:field ?d . ?d kicad:field_name "digikey" ; kicad:field_value ?digikey_pn . OPTIONAL { ?dk_part a dk:part ; dk:partNumber ?digikey_pn . } } } GROUP BY ?digikey_pn ORDER BY ?digikey_pn """, init_bindings={'schematic_url': rdflib.term.URIRef(schematic_url)}) size = len(res) if size == 0: cli.info('Could not find an parts for the schematic, did you use the correct URL: %s?' % schematic_url) for row in res: pn = row.digikey_pn if pn is None: continue refs = row.refs.split(';') cli.info("Part \"%s\" is used by %s" % (pn, refs)) filename = 'ttl/digikey-part-' + normalize_filename(pn + ".ttl") def download_graph(): cli.info("Downloading product: " + pn) product = download_product(client, db, pn) g = cli.create_graph(digikey=True) [g.add(node) for node in product.to_nodes()] return g cli.write_graph(download_graph, filename) def run(schematic_url, args): cli.info("Schematic: %s" % schematic_url) cli.with_database(lambda g: work(schematic_url, g))