diff options
Diffstat (limited to 'trygvis/eda/cli/digikey_download_for_schematic.py')
-rwxr-xr-x | trygvis/eda/cli/digikey_download_for_schematic.py | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/trygvis/eda/cli/digikey_download_for_schematic.py b/trygvis/eda/cli/digikey_download_for_schematic.py new file mode 100755 index 0000000..99f5266 --- /dev/null +++ b/trygvis/eda/cli/digikey_download_for_schematic.py @@ -0,0 +1,133 @@ +from os.path import isfile + +from rdflib.plugins.sparql import prepareQuery +import rdflib.term + +from trygvis.eda import cli, write_graph +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": RDF, + "rdfs": 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 run(schematic_url, db_path, args): + cli.info("Schematic: %s" % schematic_url) + g = cli.open_database(db_path) + + 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 = g.query(prepareQuery("SELECT ?schematic WHERE {?schematic a kicad-type:schematic}", initNs=initNs)) + 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)) + 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(""" +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 +""", initNs=initNs) + + res = g.query(q, initBindings={'schematic_url': rdflib.term.URIRef(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 + + 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) |