diff options
Diffstat (limited to 'trygvis')
-rw-r--r-- | trygvis/eda/__init__.py | 5 | ||||
-rw-r--r-- | trygvis/eda/cli/__init__.py | 8 | ||||
-rwxr-xr-x | trygvis/eda/cli/digikey_download_metadata.py | 13 | ||||
-rw-r--r-- | trygvis/eda/digikey/__init__.py | 63 | ||||
-rw-r--r-- | trygvis/eda/digikey/rdf.py | 12 |
5 files changed, 70 insertions, 31 deletions
diff --git a/trygvis/eda/__init__.py b/trygvis/eda/__init__.py index 408bd7c..26181d4 100644 --- a/trygvis/eda/__init__.py +++ b/trygvis/eda/__init__.py @@ -1,2 +1,7 @@ +from rdflib import Namespace + +EDA = Namespace("https://trygvis.io/purl/eda#") + + class EdaException(Exception): pass diff --git a/trygvis/eda/cli/__init__.py b/trygvis/eda/cli/__init__.py index 422cf48..3807fb3 100644 --- a/trygvis/eda/cli/__init__.py +++ b/trygvis/eda/cli/__init__.py @@ -5,13 +5,14 @@ from os import mkdir from os.path import dirname, isdir from typing import Callable -from rdflib import store, ConjunctiveGraph, Graph, RDF, RDFS +from rdflib import store, ConjunctiveGraph, Graph, RDF, RDFS, OWL from rdflib.plugins.sparql import prepareQuery import rdflib.plugins.stores.sparqlstore as sparqlstore import configparser # noinspection PyUnresolvedReferences import argparse +from .. import EDA from ..digikey import rdf as digikey_rdf from ..kicad import rdf as kicad_rdf @@ -84,6 +85,9 @@ def with_database(tx): def create_graph(digikey=False, kicad=False) -> Graph: g = Graph() + g.bind("owl", OWL) + g.bind("eda", EDA) + if digikey: g.bind("dk", digikey_rdf.DIGIKEY) g.bind("dk-part", digikey_rdf.DIGIKEY_PART) @@ -124,6 +128,8 @@ def write_graph(gen_g: Callable[[], Graph], filename: str = None, force_write: b _initNs = { "rdf": RDF, "rdfs": RDFS, + "owl": OWL, + "eda": EDA, "dk": digikey_rdf.DIGIKEY, "dk-attr-type": digikey_rdf.DIGIKEY_ATTRIBUTE_TYPE, "dk-attr-value": digikey_rdf.DIGIKEY_ATTRIBUTE_VALUE, diff --git a/trygvis/eda/cli/digikey_download_metadata.py b/trygvis/eda/cli/digikey_download_metadata.py index 60880ab..59c8099 100755 --- a/trygvis/eda/cli/digikey_download_metadata.py +++ b/trygvis/eda/cli/digikey_download_metadata.py @@ -1,4 +1,6 @@ -from trygvis.eda import cli +from rdflib.term import URIRef + +from trygvis.eda import cli, EDA from trygvis.eda.digikey import * @@ -19,11 +21,15 @@ def do_categories(db: DigikeyDatabase, client: DigikeyClient, output: str): def do_category(client: DigikeyClient, sc: DigikeyProductCategory, output: str): - cli.info('Downloading categories for %s' % sc.label) + cli.info('Downloading attributes for %s' % sc.label) def make_graph(): attributes = download_attribute_types_from_category(sc, client) - g = cli.create_graph() + g = cli.create_graph(digikey=True) + + g.add((sc.url(), RDF.type, OWL.Ontology)) + g.add((sc.url(), OWL.imports, URIRef(rdf.DIGIKEY))) + for a in attributes: [g.add(node) for node in a.to_nodes()] @@ -31,6 +37,7 @@ def do_category(client: DigikeyClient, sc: DigikeyProductCategory, output: str): cli.write_graph(make_graph, output, force_write=True) + def run(args): db = DigikeyDatabase() client = DigikeyClient() diff --git a/trygvis/eda/digikey/__init__.py b/trygvis/eda/digikey/__init__.py index 8fe18c8..2f93858 100644 --- a/trygvis/eda/digikey/__init__.py +++ b/trygvis/eda/digikey/__init__.py @@ -7,7 +7,7 @@ from cachecontrol.caches.file_cache import FileCache from cachecontrol.heuristics import ExpiresAfter from lxml import html from rdflib import Literal -from rdflib.namespace import RDF, RDFS +from rdflib.namespace import RDF, RDFS, OWL import trygvis.eda.digikey.rdf @@ -41,8 +41,8 @@ class DigikeyDatabase(object): return sc return None - def merge_attribute_types(self, attributeTypes): - for a in attributeTypes: + def merge_attribute_types(self, attribute_types): + for a in attribute_types: if a.id in self.attributeTypes: # TODO: implement merging continue @@ -78,11 +78,12 @@ class DigikeyProductCategory(object): node = self.url() nodes = [ (node, RDF.type, rdf.DIGIKEY.productCategory), + # (node, RDF.type, OWL.Class), (node, RDFS.label, Literal(self.label)), ] if self.parent is not None: - parentUrl = rdf.DIGIKEY_PRODUCT_CATEGORY[self.parent.id] - nodes.append((node, rdf.DIGIKEY.parent, parentUrl)) + parent_url = rdf.DIGIKEY_PRODUCT_CATEGORY[self.parent.id] + nodes.append((node, rdf.DIGIKEY.parent, parent_url)) if self.digikey_url is not None: nodes.append((node, rdf.DIGIKEY.url, Literal(self.digikey_url))) return nodes @@ -101,17 +102,34 @@ class DigikeyAttributeType(object): assert self.options is not None def to_nodes(self): - nodes = [] node = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id] - nodes.append((node, RDF.type, rdf.DIGIKEY.attributeType)) - nodes.append((node, RDFS.label, Literal(self.label))) + node_value = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id + "-value"] + node_option = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id + "-option"] + nodes = [ + (node, RDF.type, rdf.DIGIKEY.attributeType), + (node, RDF.type, OWL.Class), + (node, RDFS.subClassOf, rdf.DIGIKEY['AttributeType']), + (node, RDFS.label, Literal(self.label)), + + (node_option, RDF.type, OWL.ObjectProperty), + (node_option, RDFS.domain, node), + (node_option, RDFS.range, node_value), + (node_option, RDFS.label, Literal('Options for ' + self.label)), + + (node_value, RDF.type, OWL.Class), + (node_value, RDFS.subClassOf, rdf.DIGIKEY['AttributeValue']), + (node_value, RDFS.label, Literal(self.label + ' values')), + ] for o in self.options: - optionNode = rdf.DIGIKEY_ATTRIBUTE_VALUE[self.id + '-' + o.id] + option_node = rdf.DIGIKEY_ATTRIBUTE_VALUE[self.id + '-' + o.id] nodes.extend([ - (optionNode, rdf.DIGIKEY.id, Literal(o.id)), - (optionNode, RDFS.label, Literal(o.label)), - (node, rdf.DIGIKEY.value, optionNode)]) + (option_node, RDF.type, OWL.Class), + (option_node, RDFS.subClassOf, node_value), + (option_node, rdf.DIGIKEY.id, Literal(o.id)), + (option_node, RDFS.label, Literal(o.label)), + # (node, rdf.DIGIKEY.value, option_node)]) + (node, node_option, option_node)]) return nodes @@ -190,17 +208,20 @@ def download_category_tree(database: DigikeyDatabase, client: DigikeyClient, for h2 in items: label = _to_string(h2) # print(h2) - pcId = None + if not label == 'Resistors': + continue + + pc_id = None for a in h2.getchildren(): url = a.get('href') - pcId = _id_from_url(url) - if pcId is None: + pc_id = _id_from_url(url) + if pc_id is None: continue - if pcId is None: + if pc_id is None: continue - pc = DigikeyProductCategory(pcId, label) + pc = DigikeyProductCategory(pc_id, label) n = h2.getnext() if n.tag == 'span': n = n.getnext() @@ -230,13 +251,13 @@ def download_attribute_types_from_category(category: DigikeyProductCategory, cli td = select.getparent() index = td.getparent().index(td) try: - attributeLabel = headers[index] + attribute_label = headers[index] except: continue - attributeId = select.get('name') - print("label: " + attributeLabel + ", id: " + attributeId) + attribute_id = select.get('name') + print("label: " + attribute_label + ", id: " + attribute_id) options = [] - type = DigikeyAttributeType(category, attributeId, attributeLabel, options) + type = DigikeyAttributeType(category, attribute_id, attribute_label, options) for o in select.xpath("./option"): id = o.get('value') label = _to_string(o) diff --git a/trygvis/eda/digikey/rdf.py b/trygvis/eda/digikey/rdf.py index 7a21275..5f1dede 100644 --- a/trygvis/eda/digikey/rdf.py +++ b/trygvis/eda/digikey/rdf.py @@ -1,7 +1,7 @@ -import rdflib +from rdflib import Namespace -DIGIKEY = rdflib.Namespace("https://trygvis.io/purl/digikey#") -DIGIKEY_ATTRIBUTE_TYPE = rdflib.Namespace("https://trygvis.io/purl/digikey-attribute-type#") -DIGIKEY_ATTRIBUTE_VALUE = rdflib.Namespace("https://trygvis.io/purl/digikey-attribute-value#") -DIGIKEY_PART = rdflib.Namespace("https://trygvis.io/purl/digikey-part#") -DIGIKEY_PRODUCT_CATEGORY = rdflib.Namespace("https://trygvis.io/purl/digikey-product-category#") +DIGIKEY = Namespace("https://trygvis.io/purl/digikey#") +DIGIKEY_ATTRIBUTE_TYPE = Namespace("https://trygvis.io/purl/digikey-attribute-type#") +DIGIKEY_ATTRIBUTE_VALUE = Namespace("https://trygvis.io/purl/digikey-attribute-value#") +DIGIKEY_PART = Namespace("https://trygvis.io/purl/digikey-part#") +DIGIKEY_PRODUCT_CATEGORY = Namespace("https://trygvis.io/purl/digikey-product-category#") |