From 0958273a71dd19c2a90471a182ccc5b90b14e5b4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 7 Jan 2017 14:00:46 +0100 Subject: Renaming 'schematic' to 'project'. Renaming 'kicad-bom-to-ttl' to 'kicad-import-project'. Renaming 'digikey-download-for-schematic' to 'digikey-download-for-project'. Splitting out the Export xml file code into its own module. init: putting project.url and project.file in config.ini. init: putting db.update-url in config.ini if given on the command line. kicad-import-project: by default, assume that the user want to update local database, optionally write the ttl file to disk. cli.write_graph: create any missing parent directories. --- trygvis/eda/cli/digikey_download_for_project.py | 105 ++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 trygvis/eda/cli/digikey_download_for_project.py (limited to 'trygvis/eda/cli/digikey_download_for_project.py') diff --git a/trygvis/eda/cli/digikey_download_for_project.py b/trygvis/eda/cli/digikey_download_for_project.py new file mode 100755 index 0000000..02e3cb1 --- /dev/null +++ b/trygvis/eda/cli/digikey_download_for_project.py @@ -0,0 +1,105 @@ +import argparse +import rdflib +import rdflib.term + +from trygvis.eda import cli +from trygvis.eda.digikey import * + + +class DigikeyDownloadForProjectCommand(cli.CliCommand): + def __init__(self): + super().__init__("digikey-download-for-project", "Download missing data from digikey.com") + + def run(self, argv): + p = argparse.ArgumentParser(prog=self.key, description=self.description) + p.add_argument("--project", required=False) + p.add_argument("-f", "--force", default=False, action='store_true') + args = p.parse_args(argv) + + run(args) + + +def work(project_url, force, g): + client = DigikeyClient() + db = DigikeyDatabase() + download_category_tree(db, client) + + # Dump project: + # SELECT ?project + # WHERE { + # ?project a kicad-type:project . + # b:rateboard-2_a ? ?. + # ?s ?predicate ?object . + # } + + # allComponentsQ = prepareQuery('SELECT ?ref ?value WHERE { ?project_url a kicad-type:project . ?project_url kicad:component ?o . ?o rdfs:label ?ref . ?o kicad:value ?value . }', initNs = initNs) + # + # for row in g.query(allComponentsQ, initBindings={'project_url': project_url}): + # print("ref=%s, value=%s" % (row.ref, row.value)) + + res = cli.sparql(g, "SELECT ?project WHERE {?project a kicad-type:project}") + print("Found %d projects in database" % len(res)) + for row in res: + print("project: %s" % row.project) + + 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 { + ?project_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={'project_url': rdflib.term.URIRef(project_url)}) + size = len(res) + + if size == 0: + cli.info('Could not find an parts for the project, did you use the correct URL: %s?' % project_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, force_write=force) + + +def run(args): + config = cli.read_config() + + project_url = config['project']['url'] + + cli.info("Project: %s" % project_url) + cli.with_database(lambda g: work(project_url, args.force, g)) -- cgit v1.2.3