From 8c90d2e409e9c457a8e27fc11d8e030248eb6f70 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 28 Dec 2016 15:46:01 +0100 Subject: o Modernized digikey-download-for-schematic so that it works against the remote SPARQL endpoint. --- trygvis/eda/cli/digikey_download_for_schematic.py | 67 ++++------------------- 1 file changed, 10 insertions(+), 57 deletions(-) diff --git a/trygvis/eda/cli/digikey_download_for_schematic.py b/trygvis/eda/cli/digikey_download_for_schematic.py index 0cb5baa..4be2632 100755 --- a/trygvis/eda/cli/digikey_download_for_schematic.py +++ b/trygvis/eda/cli/digikey_download_for_schematic.py @@ -1,22 +1,8 @@ -from rdflib.plugins.sparql import prepareQuery import rdflib import rdflib.term from trygvis.eda import cli from trygvis.eda.digikey import * -from trygvis.eda.digikey import rdf as digikey_rdf -from trygvis.eda.kicad import rdf as kicad_rdf - -initNs = { - "rdf": rdflib.RDF, - "rdfs": rdflib.RDFS, - "dk": digikey_rdf.DIGIKEY, - "dk-attr-type": digikey_rdf.DIGIKEY_ATTRIBUTE_TYPE, - "dk-attr-value": digikey_rdf.DIGIKEY_ATTRIBUTE_VALUE, - "dk-part": digikey_rdf.DIGIKEY_PART, - "dk-p-c": digikey_rdf.DIGIKEY_PRODUCT_CATEGORY, - "kicad": kicad_rdf.KICAD, - "kicad-type": kicad_rdf.KICAD_TYPE} def work(schematic_url, g): @@ -37,17 +23,17 @@ def work(schematic_url, g): # for row in g.query(allComponentsQ, initBindings={'schematic_url': schematic_url}): # print("ref=%s, value=%s" % (row.ref, row.value)) - res = g.query(prepareQuery("SELECT ?schematic WHERE {?schematic a kicad-type:schematic}", initNs=initNs)) + 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 = g.query(prepareQuery("SELECT ?dk_part ?dk_part_number WHERE {?dk_part a dk:part ; dk:partNumber ?dk_part_number}", initNs=initNs)) + 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)) - q = prepareQuery(""" + res = cli.sparql(g, """ SELECT ?digikey_pn (group_concat(distinct ?ref;separator=";") as ?refs) @@ -59,17 +45,20 @@ WHERE { ?cmp kicad:field ?d . ?d kicad:field_name "digikey" ; kicad:field_value ?digikey_pn . -# OPTIONAL { + OPTIONAL { ?dk_part a dk:part ; dk:partNumber ?digikey_pn . -# } + } } } GROUP BY ?digikey_pn ORDER BY ?digikey_pn -""", initNs=initNs) +""", 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) - res = g.query(q, initBindings={'schematic_url': rdflib.term.URIRef(schematic_url)}) for row in res: pn = row.digikey_pn @@ -92,42 +81,6 @@ ORDER BY ?digikey_pn cli.write_graph(download_graph, filename) - # q = prepareQuery(""" - # SELECT - # DISTINCT ?category ?digikeyUrl - # WHERE { - # ?schematic_url kicad:component ?cmp . - # ?cmp rdfs:label ?ref . - # ?cmp kicad:value ?value . - # ?cmp kicad:field ?d . - # ?d kicad:field_name "digikey" . - # ?d kicad:field_value ?digikey . - # - # ?part a dk:part . - # ?part dk:partNumber ?digikey . - # ?part dk:category ?category . - # ?category dk:parent ?_ . - # ?category dk:url ?digikeyUrl - # }""", initNs=initNs) - # - # res = g.query(q, initBindings={'schematic_url': schematic_url}) - # - # cli.info("Found %d categories" % (len(res))) - # for row in res: - # cli.info("Category: %s" % (row.category)) - # category = db.findSubCategoryByUrl(row.category) - # if category is None: - # raise Exception('could not find category: ' + row.category) - # attributes = downloadAttributeTypesFromCategory(category, client) - # db.mergeAttributeTypes(attributes) - # - # filename = 'digikey-category-' + normalize_filename(str(category.label) + ".ttl") - # if not isfile(filename): - # tmpG = cli.create_graph() - # for a in attributes: - # [tmpG.add(node) for node in a.toNodes()] - # writeGraph(tmpG, 'ttl/' + filename) - def run(schematic_url, args): cli.info("Schematic: %s" % schematic_url) -- cgit v1.2.3