aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2016-12-27 22:49:58 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2016-12-27 22:49:58 +0100
commit8af35183f1f25e2f9d1d111cac85a48622f22673 (patch)
tree0b7ed75ff502742196508645323d265fc264ad4f
parent9bf18900c05547a40bb3c3a7a819a137e81dae92 (diff)
downloadeda-rdf-8af35183f1f25e2f9d1d111cac85a48622f22673.tar.gz
eda-rdf-8af35183f1f25e2f9d1d111cac85a48622f22673.tar.bz2
eda-rdf-8af35183f1f25e2f9d1d111cac85a48622f22673.tar.xz
eda-rdf-8af35183f1f25e2f9d1d111cac85a48622f22673.zip
o Replacing open_database with with_database to make sure it is closed after use.
-rw-r--r--trygvis/eda/cli/__init__.py9
-rw-r--r--trygvis/eda/cli/add_to_db.py23
-rwxr-xr-xtrygvis/eda/cli/db_stats.py31
-rwxr-xr-xtrygvis/eda/cli/digikey_download_for_schematic.py12
-rwxr-xr-xtrygvis/eda/cli/make_bom.py12
5 files changed, 47 insertions, 40 deletions
diff --git a/trygvis/eda/cli/__init__.py b/trygvis/eda/cli/__init__.py
index 9cfb710..8a4ce6f 100644
--- a/trygvis/eda/cli/__init__.py
+++ b/trygvis/eda/cli/__init__.py
@@ -27,7 +27,7 @@ def do_exit(msg=None):
sys.exit(msg)
-def open_database(path):
+def with_database(path, tx):
g = ConjunctiveGraph('Sleepycat')
rt = g.open(path, create=False)
if rt == store.NO_STORE:
@@ -36,7 +36,10 @@ def open_database(path):
elif rt != store.VALID_STORE:
raise CliException("The database is corrupt: %s" % path)
- return g
+ try:
+ tx(g)
+ finally:
+ g.close()
def create_graph(digikey=False, kicad=False):
@@ -67,7 +70,7 @@ _initNs = {
"kicad-type": kicad_rdf.KICAD_TYPE}
-def sparql(g, query, init_bindings = None):
+def sparql(g, query, init_bindings=None):
q = prepareQuery(query, initNs=_initNs)
return g.query(q, initBindings=init_bindings)
diff --git a/trygvis/eda/cli/add_to_db.py b/trygvis/eda/cli/add_to_db.py
index 0511850..e3b07c1 100644
--- a/trygvis/eda/cli/add_to_db.py
+++ b/trygvis/eda/cli/add_to_db.py
@@ -2,16 +2,17 @@ import trygvis.eda.cli as cli
def run(files, path, args):
- g = cli.open_database(path)
+ def load(g):
+ s = 0
+ for f in files:
+ cli.info("Adding %s" % f)
+ pre = len(g)
+ g.load(f, format="turtle")
+ post = len(g)
+ diff = post - pre
+ s += diff
+ cli.info("Loaded %d tuples" % diff)
- s = 0
- for f in files:
- cli.info("Adding %s" % f)
- pre = len(g)
- g.load(f, format="turtle")
- post = len(g)
- diff = post - pre
- s += diff
- cli.info("Loaded %d tuples" % diff)
+ cli.info("Done. Loaded %d tuples" % s)
- cli.info("Done. Loaded %d tuples" % s)
+ cli.with_database(path, load)
diff --git a/trygvis/eda/cli/db_stats.py b/trygvis/eda/cli/db_stats.py
index 4780c20..5ee99ac 100755
--- a/trygvis/eda/cli/db_stats.py
+++ b/trygvis/eda/cli/db_stats.py
@@ -2,9 +2,8 @@ from trygvis.eda import cli
def run(db_path):
- g = cli.open_database(db_path)
-
- res = cli.sparql(g, """
+ def db_stats(g):
+ res = cli.sparql(g, """
SELECT ?schematic ?label
WHERE {
?schematic a kicad-type:schematic
@@ -13,16 +12,16 @@ WHERE {
}
}
""")
- cli.info("Found %d schematics in database" % len(res))
- for row in res:
- name = row.label if row.label is not None else "<unnamed>"
- url = row.schematic
+ cli.info("Found %d schematics in database" % len(res))
+ for row in res:
+ name = row.label if row.label is not None else "<unnamed>"
+ url = row.schematic
- cli.info("%s:" % name)
- cli.info(" URL: %s" % url)
- cli.info()
+ cli.info("%s:" % name)
+ cli.info(" URL: %s" % url)
+ cli.info()
- res = cli.sparql(g, """
+ res = cli.sparql(g, """
SELECT
?dk_part ?dk_part_number ?label
WHERE {
@@ -33,7 +32,9 @@ WHERE {
}
}
ORDER BY ?dk_part_number
- """)
- cli.info("Found %d Digi-Key parts:" % len(res))
- for row in res:
- cli.info(" %-30s: %s" % (row.dk_part_number, row.label))
+""")
+ cli.info("Found %d Digi-Key parts:" % len(res))
+ for row in res:
+ cli.info(" %-30s: %s" % (row.dk_part_number, row.label))
+
+ cli.with_database(db_path, db_stats)
diff --git a/trygvis/eda/cli/digikey_download_for_schematic.py b/trygvis/eda/cli/digikey_download_for_schematic.py
index 99f5266..ecfbc9e 100755
--- a/trygvis/eda/cli/digikey_download_for_schematic.py
+++ b/trygvis/eda/cli/digikey_download_for_schematic.py
@@ -1,5 +1,3 @@
-from os.path import isfile
-
from rdflib.plugins.sparql import prepareQuery
import rdflib.term
@@ -20,10 +18,7 @@ initNs = {
"kicad-type": kicad_rdf.KICAD_TYPE}
-def run(schematic_url, db_path, args):
- cli.info("Schematic: %s" % schematic_url)
- g = cli.open_database(db_path)
-
+def work(schematic_url, g):
client = DigikeyClient()
db = DigikeyDatabase()
download_category_tree(db, client)
@@ -131,3 +126,8 @@ ORDER BY ?digikey_pn
# for a in attributes:
# [tmpG.add(node) for node in a.toNodes()]
# writeGraph(tmpG, 'ttl/' + filename)
+
+
+def run(schematic_url, db_path, args):
+ cli.info("Schematic: %s" % schematic_url)
+ cli.with_database(db_path, lambda g: work(schematic_url, g))
diff --git a/trygvis/eda/cli/make_bom.py b/trygvis/eda/cli/make_bom.py
index 39d664b..eae7043 100755
--- a/trygvis/eda/cli/make_bom.py
+++ b/trygvis/eda/cli/make_bom.py
@@ -22,10 +22,12 @@ class Component(object):
def run(schematic_url, db_path):
- g = cli.open_database(db_path)
+ cli.with_database(db_path, lambda g: work(schematic_url, g))
+
+def work(schematic_url, g):
components = {}
- dkParts = {}
+ dk_parts = {}
cli.info("Loading components")
@@ -95,11 +97,11 @@ ORDER BY ?ref ?attr_type ?attr_value
for row in res:
pn = row.part_number
- if not hasattr(dkParts, pn):
+ if not hasattr(dk_parts, pn):
part = DigiKeyPart(pn)
- dkParts[pn] = part
+ dk_parts[pn] = part
else:
- part = dkParts[pn]
+ part = dk_parts[pn]
part.set_attribute(row.type, row.value)
cli.info('%5s: %-20s %s' % (pn, row.type + ':', row.value))