diff options
author | David Robillard <d@drobilla.net> | 2010-10-20 19:00:34 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2010-10-20 19:00:34 +0000 |
commit | 0a5ca10fd4450c0db04ca75e59eb5480988acbbd (patch) | |
tree | 2809f4485cb72b8b1fd5eae925b038f6d71f8592 /lv2include/lv2include.py | |
parent | f60ef36a6de1cbdde7cedae8b16bdaccdac26549 (diff) | |
download | lv2-0a5ca10fd4450c0db04ca75e59eb5480988acbbd.tar.xz |
Support both Redland and RDFLib.
Diffstat (limited to 'lv2include/lv2include.py')
-rwxr-xr-x | lv2include/lv2include.py | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/lv2include/lv2include.py b/lv2include/lv2include.py index 5a8900b..3f234e2 100755 --- a/lv2include/lv2include.py +++ b/lv2include/lv2include.py @@ -32,26 +32,49 @@ import os import stat import sys -import RDF +redland = True + +try: + import RDF # Attempt to import Redland +except: + try: + import rdflib # Attempt to import RDFLib + redland = False + except: + print >> sys.stderr, "Failed to import RDF (Redland) or rdflib" + +def rdf_namespace(uri): + "Create a new RDF namespace" + if redland: + return RDF.NS(uri) + else: + return rdflib.Namespace(uri) def rdf_load(uri): "Load an RDF model" - model = RDF.Model() - parser = RDF.Parser(name="guess") - parser.parse_into_model(model, uri) + if redland: + model = RDF.Model() + parser = RDF.Parser(name="turtle") + parser.parse_into_model(model, uri) + else: + model = rdflib.ConjunctiveGraph() + model.parse(uri, format="n3") return model def rdf_find_type(model, rdf_type): "Return a list of the URIs of all resources in model with a given type" - results = model.find_statements(RDF.Statement(None, rdf.type, rdf_type)) - ret = [] - for r in results: - ret.append(str(r.subject.uri)) - return ret - -def rdf_namespace(uri): - "Create a new RDF namespace" - return RDF.NS(uri) + if redland: + results = model.find_statements(RDF.Statement(None, rdf.type, rdf_type)) + ret = [] + for r in results: + ret.append(str(r.subject.uri)) + return ret + else: + results = model.triples([None, rdf.type, rdf_type]) + ret = [] + for r in results: + ret.append(r[0]) + return ret rdf = rdf_namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') lv2 = rdf_namespace('http://lv2plug.in/ns/lv2core#') |