diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-07-06 15:34:56 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-07-06 15:34:56 +0100 |
commit | a9cc468044c07f14154b4bbcbc2785f4a8eb7977 (patch) | |
tree | f310f83ec6a745524e22cd0218012baa2048fd1b | |
parent | 44514d0f2d6b416ec449b4491c5cf19e363f9566 (diff) | |
download | definitions-a9cc468044c07f14154b4bbcbc2785f4a8eb7977.tar.gz |
schema: Add DefinitionsNamespace class
This is a nice way of getting URIs for the various entities as we load
the .morph files, and can be reused.
Change-Id: I7187e9c4692d0f1059856502b521bc0e4e97a270
-rw-r--r-- | schema/parse.py | 91 |
1 files changed, 45 insertions, 46 deletions
diff --git a/schema/parse.py b/schema/parse.py index c2f369fb..1ee1b367 100644 --- a/schema/parse.py +++ b/schema/parse.py @@ -69,42 +69,42 @@ def get_name_from_morph_file(path): # FIXME: can you use assignment instead of Resource.set ? -def uriref_for_chunk(base_uri, chunk_name): - uriref = base_uri + 'chunks/' + chunk_name - return rdflib.URIRef(uriref) +class DefinitionsNamespace(rdflib.Namespace): + '''Helpers to construct URIs for Baserock entities. + These functions all return instances of the rdflib.URIRef() class. -def uriref_for_chunk_reference(base_uri, stratum_name, chunk_name): - stratum_uriref = uriref_for_stratum(base_uri, stratum_name) - chunk_reference_uriref = stratum_uriref + '/chunk-refs/' + chunk_name - return rdflib.URIRef(chunk_reference_uriref) + Example: + ns = DefinitionsNamespace('http://example.com') + chunk1 = ns.chunk('gcc') + print(chunk1) # => rdflib.URIRef('http://example.com/chunks/gcc') -def uriref_for_cluster(base_uri, cluster_name): - uriref = base_uri + 'clusters/' + cluster_name - return rdflib.URIRef(uriref) - + ''' -def uriref_for_stratum(base_uri, stratum_name): - uriref = base_uri + 'strata/' + stratum_name - return rdflib.URIRef(uriref) + def chunk(self, chunk_name): + return self.term('chunks/' + chunk_name) + def chunk_reference(self, stratum_uriref, chunk_name): + assert isinstance(stratum_uriref, rdflib.URIRef) + return stratum_uriref + '/chunk-refs/' + chunk_name -def uriref_for_stratum_artifact(base_uri, stratum_name, artifact_name): - stratum_uriref = uriref_for_stratum(base_uri, stratum_name) - artifact_uriref = stratum_uriref + '/products/' + artifact_name - return rdflib.URIRef(artifact_uriref) + def cluster(self, cluster_name): + return self.term('clusters/' + cluster_name) + def stratum(self, stratum_name): + return self.term('strata/' + stratum_name) -def uriref_for_system(base_uri, system_name): - uriref = base_uri + 'systems/' + system_name - return rdflib.URIRef(uriref) + def stratum_artifact(self, stratum_uriref, artifact_name): + assert isinstance(stratum_uriref, rdflib.URIRef) + return stratum_uriref + '/products/' + artifact_name + def system(self, system_name): + return self.term('systems/' + system_name) -def uriref_for_system_deployment(base_uri, cluster_name, label): - cluster_uriref = uriref_for_cluster(base_uri, cluster_name) - system_deployment_uriref = cluster_uriref + label - return rdflib.URIRef(system_deployment_uriref) + def system_deployment(self, cluster_uriref, label): + assert isinstance(cluster_uriref, rdflib.URIRef) + return cluster_uriref + '/' + label def load_all_morphologies(path='.'): @@ -123,13 +123,14 @@ def load_all_morphologies(path='.'): except Exception as e: warnings.warn("Problem loading %s: %s" % (filename, e)) - # FIXME: - base_uri = 'http://example.com/' + # FIXME: caller should pass in the namespace, or we should make one + # up based on os.path.abspath(path) + ns = DefinitionsNamespace('http://example.com/') entity = None if contents['kind'] == 'chunk': - chunk_uriref = uriref_for_chunk(base_uri, contents['name']) + chunk_uriref = ns.chunk(contents['name']) entity = chunk = rdflib.resource.Resource(graph, chunk_uriref) # FIXME: order is lost here !!!!! @@ -162,19 +163,18 @@ def load_all_morphologies(path='.'): rdflib.Literal(contents['post-install-commands'])) elif contents['kind'] == 'stratum': - stratum_name = contents['name'] - stratum_uriref = uriref_for_stratum(base_uri, stratum_name) + stratum_uriref = ns.stratum(contents['name']) entity = stratum = rdflib.resource.Resource(graph, stratum_uriref) for entry in contents.get('build-depends', []): build_dep_file = os.path.join(toplevel_path, entry['morph']) build_dep_name = get_name_from_morph_file(build_dep_file) - build_dep_uriref = uriref_for_stratum(base_uri, build_dep_name) + build_dep_uriref = ns.stratum(build_dep_name) stratum.add(BASEROCK.hasBuildDependency, build_dep_uriref) for entry in contents.get('products', []): - artifact_uri = uriref_for_stratum_artifact( - base_uri, stratum_name, entry['artifact']) + artifact_uri = ns.stratum_artifact( + stratum_uriref, entry['artifact']) artifact = rdflib.resource.Resource(graph, artifact_uri) # FIXME: order probably lost here if 'includes' in entry: @@ -194,11 +194,11 @@ def load_all_morphologies(path='.'): else: chunk_name = entry['name'] - chunk_ref_uriref = uriref_for_chunk_reference( - base_uri, stratum_name, chunk_name) + chunk_ref_uriref = ns.chunk_reference( + stratum_uriref, chunk_name) chunk_ref = rdflib.resource.Resource(graph, chunk_ref_uriref) - chunk_uriref = uriref_for_chunk(base_uri, chunk_name) + chunk_uriref = ns.chunk(chunk_name) chunk_ref.set(BASEROCK.refersToChunk, chunk_uriref) chunk_ref.set(BASEROCK.repo, rdflib.Literal(entry['repo'])) @@ -212,15 +212,15 @@ def load_all_morphologies(path='.'): rdflib.Literal(entry.get('prefix', '/usr'))) for entry_dep in entry.get('build-depends', []): - build_dep_uriref = uriref_for_chunk_reference( - base_uri, stratum_name, entry_dep) + build_dep_uriref = ns.chunk_reference( + stratum_uriref, entry_dep) chunk_ref.set(BASEROCK.hasChunkBuildDependency, build_dep_uriref) stratum.add(BASEROCK.containsChunkReference, chunk_ref) elif contents['kind'] == 'system': - system_uriref = uriref_for_system(base_uri, contents['name']) + system_uriref = ns.system(contents['name']) entity = system = rdflib.resource.Resource(graph, system_uriref) system.set(BASEROCK.arch, rdflib.Literal(contents['arch'])) @@ -236,8 +236,8 @@ def load_all_morphologies(path='.'): artifacts = ['%s-runtime' % entry['name'], '%s-devel' % entry['name']] for artifact in artifacts: - artifact_uriref = uriref_for_stratum_artifact( - base_uri, entry['name'], artifact) + artifact_uriref = ns.stratum_artifact( + ns.stratum(entry['name']), artifact) system.add(BASEROCK.containsStratumArtifact, artifact_uriref) @@ -245,19 +245,18 @@ def load_all_morphologies(path='.'): contents.get('configuration-extensions', []))) elif contents['kind'] == 'cluster': - cluster_name = contents['name'] - cluster_uriref = uriref_for_cluster(base_uri, cluster_name) + cluster_uriref = ns.cluster(contents['name']) entity = cluster = rdflib.resource.Resource(graph, cluster_uriref) for entry in contents.get('systems', []): system_morph_file = os.path.join(toplevel_path, entry['morph']) system_name = get_name_from_morph_file(system_morph_file) - system_uriref = uriref_for_system(base_uri, system_name) + system_uriref = ns.system(system_name) # FIXME: ignores deploy-defaults at present for label, details in entry['deploy'].items(): - deployment_uriref = uriref_for_system_deployment( - base_uri, cluster_name, label) + deployment_uriref = ns.system_deployment( + cluster_uriref, label) deployment = rdflib.resource.Resource( graph, deployment_uriref) |