From 5174d4dd33ef2670cb294883d55cbe6f0d977324 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Thu, 2 Jul 2015 13:27:08 +0100 Subject: schema: Link clusters to systems properly Previously this was a FIXME placeholder. Change-Id: I1979dde0e63abb0d45aa16126589cd81d4410c73 --- schema/parse.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/schema/parse.py b/schema/parse.py index 079b3b2b..02f022fe 100644 --- a/schema/parse.py +++ b/schema/parse.py @@ -15,7 +15,22 @@ surf.ns.register(baserock='http://baserock.org/definitions/example-schema#') surf.ns.register(dc='http://purl.org/dc/terms/') -def load_all_morphologies(session, store): +def parse_morph_file(path): + with open(path) as f: + text = f.read() + contents = yaml.safe_load(text) + morph_type = contents['kind'] + assert 'name' in contents + assert contents['kind'] in ['cluster', 'system', 'stratum', 'chunk'] + return contents + + +def get_name_from_morph_file(path): + contents = parse_morph_file(path) + return contents['name'] + + +def load_all_morphologies(session, store, path='.'): Chunk = session.get_class(surf.ns.BASEROCK.Chunk) ChunkReference = session.get_class(surf.ns.BASEROCK.ChunkReference) Stratum = session.get_class(surf.ns.BASEROCK.Stratum) @@ -24,16 +39,13 @@ def load_all_morphologies(session, store): SystemDeployment = session.get_class(surf.ns.BASEROCK.SystemDeployment) Cluster = session.get_class(surf.ns.BASEROCK.Cluster) - def load_morph(path): + toplevel_path = path + + def load_morph(toplevel_path, filename): try: - with open(path) as f: - text = f.read() - contents = yaml.safe_load(text) - morph_type = contents['kind'] - assert 'name' in contents - assert contents['kind'] in ['cluster', 'system', 'stratum', 'chunk'] + contents = parse_morph_file(filename) except Exception as e: - warnings.warn("Problem loading %s: %s" % (path, e)) + warnings.warn("Problem loading %s: %s" % (filename, e)) # FIXME: base_uri = 'http://example.com/' @@ -148,7 +160,9 @@ def load_all_morphologies(session, store): for entry in contents.get('systems', []): # FIXME: can't get the URI from the 'morph' entry... need to load # the actual .morph file and get the name from there. - system_uri = 'http://FIXME' + system_morph_file = os.path.join(toplevel_path, entry['morph']) + system_name = get_name_from_morph_file(system_morph_file) + system_uri = base_uri + 'systems/' + system_name # FIXME: ignores deploy-defaults at present for label, details in entry['deploy'].items(): @@ -183,12 +197,12 @@ def load_all_morphologies(session, store): # and dump that into a property. print 'Parsing .morph files...' - for dirname, dirnames, filenames in os.walk('..'): + for dirname, dirnames, filenames in os.walk(toplevel_path): if '.git' in dirnames: dirnames.remove('.git') for filename in sorted(filenames): if filename.endswith('.morph'): - load_morph(os.path.join(dirname, filename)) + load_morph(toplevel_path, os.path.join(dirname, filename)) print 'Committing to database...' try: -- cgit v1.2.1