aboutsummaryrefslogtreecommitdiff
path: root/trygvis/eda/cli/digikey_download_for_project.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-01-07 14:00:46 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2017-01-07 14:00:46 +0100
commit0958273a71dd19c2a90471a182ccc5b90b14e5b4 (patch)
tree8e33385ca9df94b80ce9b1f8ba06438b807f137a /trygvis/eda/cli/digikey_download_for_project.py
parent5d7fc9c4b14536006f2435b1379887f95937e096 (diff)
downloadeda-rdf-0958273a71dd19c2a90471a182ccc5b90b14e5b4.tar.gz
eda-rdf-0958273a71dd19c2a90471a182ccc5b90b14e5b4.tar.bz2
eda-rdf-0958273a71dd19c2a90471a182ccc5b90b14e5b4.tar.xz
eda-rdf-0958273a71dd19c2a90471a182ccc5b90b14e5b4.zip
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.
Diffstat (limited to 'trygvis/eda/cli/digikey_download_for_project.py')
-rwxr-xr-xtrygvis/eda/cli/digikey_download_for_project.py105
1 files changed, 105 insertions, 0 deletions
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))