From e92fb46ec42991dca60d22d8b1ab321b7c4ff146 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 28 Dec 2016 16:37:39 +0100 Subject: o Adding --force flag to digikey-download-for-schematic. o Unbreaking download_product(). --- trygvis/eda/cli/digikey_download_for_schematic.py | 6 +-- trygvis/eda/cli/digikey_download_metadata.py | 2 +- trygvis/eda/cli/eda_rdf.py | 3 ++ trygvis/eda/cli/make_bom.py | 2 +- trygvis/eda/digikey/__init__.py | 49 +++++++++++++---------- 5 files changed, 36 insertions(+), 26 deletions(-) (limited to 'trygvis/eda') diff --git a/trygvis/eda/cli/digikey_download_for_schematic.py b/trygvis/eda/cli/digikey_download_for_schematic.py index 4be2632..bf4f0ce 100755 --- a/trygvis/eda/cli/digikey_download_for_schematic.py +++ b/trygvis/eda/cli/digikey_download_for_schematic.py @@ -5,7 +5,7 @@ from trygvis.eda import cli from trygvis.eda.digikey import * -def work(schematic_url, g): +def work(schematic_url, force, g): client = DigikeyClient() db = DigikeyDatabase() download_category_tree(db, client) @@ -79,9 +79,9 @@ ORDER BY ?digikey_pn [g.add(node) for node in product.to_nodes()] return g - cli.write_graph(download_graph, filename) + cli.write_graph(download_graph, filename, force_write=force) def run(schematic_url, args): cli.info("Schematic: %s" % schematic_url) - cli.with_database(lambda g: work(schematic_url, g)) + cli.with_database(lambda g: work(schematic_url, args.force, g)) diff --git a/trygvis/eda/cli/digikey_download_metadata.py b/trygvis/eda/cli/digikey_download_metadata.py index d807c1a..60880ab 100755 --- a/trygvis/eda/cli/digikey_download_metadata.py +++ b/trygvis/eda/cli/digikey_download_metadata.py @@ -41,5 +41,5 @@ def run(args): for pc in db.productCategories: for sc in pc.subCategories: - output = "%s/digikey-%s.ttl" % (args.output_dir, normalize_filename(sc.label)) + output = "%s/digikey-category-%s.ttl" % (args.output_dir, normalize_filename(sc.label)) do_category(client, sc, output) diff --git a/trygvis/eda/cli/eda_rdf.py b/trygvis/eda/cli/eda_rdf.py index 7b4ed2c..ee4b0dd 100644 --- a/trygvis/eda/cli/eda_rdf.py +++ b/trygvis/eda/cli/eda_rdf.py @@ -9,6 +9,8 @@ class CliCommand(object): self.description = description +# TODO: move all of the command classes to the file they delegate to. + class AddToDb(CliCommand): def __init__(self): super().__init__("add-to-db", "Import RDF triplet file to the database") @@ -55,6 +57,7 @@ class DigikeyDownloadForSchematic(CliCommand): def run(self, argv): p = argparse.ArgumentParser(prog=self.key, description=self.description) p.add_argument("--schematic", required=True) + p.add_argument("-f", "--force", default=False, action='store_true') args = p.parse_args(argv) from trygvis.eda.cli import digikey_download_for_schematic diff --git a/trygvis/eda/cli/make_bom.py b/trygvis/eda/cli/make_bom.py index fcdbe99..8d8c9f1 100755 --- a/trygvis/eda/cli/make_bom.py +++ b/trygvis/eda/cli/make_bom.py @@ -104,4 +104,4 @@ ORDER BY ?ref ?attr_type ?attr_value part = dk_parts[pn] part.set_attribute(row.type, row.value) - cli.info('%5s: %-20s %s' % (pn, row.type + ':', row.value)) + cli.info('%-30s %-50s %s' % (pn, row.type + ':', row.value)) diff --git a/trygvis/eda/digikey/__init__.py b/trygvis/eda/digikey/__init__.py index ec1167d..8fe18c8 100644 --- a/trygvis/eda/digikey/__init__.py +++ b/trygvis/eda/digikey/__init__.py @@ -146,9 +146,9 @@ class DigikeyProduct(object): nodes.append((node, rdf.DIGIKEY.partNumber, Literal(self.part_number))) nodes.append((node, RDFS.label, Literal(self.description))) for v in self.values: - typeLabel = v.type.label if v.type is not None else v.typeLabel - typeId = v.type.id if v.type is not None else v.typeId - nodes.append((node, rdf.DIGIKEY['attribute-value'], rdf.DIGIKEY_ATTRIBUTE_VALUE[typeId + '-' + v.id])) + type_label = v.type.label if v.type is not None else v.type_label + type_id = v.type.id if v.type is not None else v.type_id + nodes.append((node, rdf.DIGIKEY['attribute-value'], rdf.DIGIKEY_ATTRIBUTE_VALUE[type_id + '-' + v.id])) for c in self.categories: nodes.append((node, rdf.DIGIKEY.category, c.url())) @@ -255,42 +255,49 @@ def download_product(client: DigikeyClient, db, query): values = [] categories = [] for table in tree.xpath("//table[contains(@class, 'attributes-table-main')]"): - label = None - id = None for tr in table.xpath(".//tr"): if tr.get("id") is not None: continue tds = tr.xpath("./th | ./td") + # print('tds: ' + str(tds)) + # for x in tds: + # print(_to_string(x)) if len(tds) != 3: continue type_label = _to_string(tds[0]) label = _to_string(tds[1]) + + type_id = value = None for input in tds[2].xpath("./input[@name]"): - typeId = input.get("name") - id = input.get("value") - else: - typeId = None + type_id = input.get("name") + value = input.get("value") - if id is None or typeId is None: + if value is None or type_id is None: continue - if typeId == "t": # categories are handled later + if type_id == "t": # categories are handled later continue - values.append(DigikeyAttributeValue(id, label, type_id=typeId, type_label=type_label)) + values.append(DigikeyAttributeValue(value, label, type_id=type_id, type_label=type_label)) for td in table.xpath(".//td[@class='attributes-td-categories-link']"): tr = td.getparent() - id = None - url = None - for a in td.xpath(".//a[@href]"): - url = a.get("href") - id = _id_from_url(url) - for input in tr.xpath(".//input[@name='t' and @value]"): - categoryId = input.get("value") + a = next((a for a in td.xpath(".//a[@href]")), None) + if a is None: + continue + + label = "dummy" # a.text() - if id is None: + url = a.get("href") + if url is None: continue - categories.append(DigikeyProductCategory(id, label, digikey_url=url)) + + value = _id_from_url(url) + if value is None: + continue + + category_id = next((e.get("value") for e in tr.xpath(".//input[@name='t' and @value]")), None) + + categories.append(DigikeyProductCategory(category_id, label, digikey_url=url)) part_id = part_number = None for n in tree.xpath("//input[@name='partid' and @value]"): -- cgit v1.2.3