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/digikey/__init__.py | 49 +++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'trygvis/eda/digikey/__init__.py') 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