@@ -22,9 +22,28 @@ class GraphMetadata:
2222
2323 class _GraphMetadata :
2424
25+ METADATA_DIR = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'metadata' )
26+
27+ # Attempt to load a json file from the METADATA_DIR but return default as the content otherwise
28+ @staticmethod
29+ def _load_json (filename , default ):
30+ filepath = os .path .join (GraphMetadata ._GraphMetadata .METADATA_DIR , filename )
31+ try :
32+ with open (filepath ) as f :
33+ data = json .load (f )
34+ if data :
35+ return data
36+ except (FileNotFoundError , json .JSONDecodeError ) as e :
37+ logger .warning (f'Could not load { filename } : { e } , using default' )
38+ return default
39+
2540 def __init__ (self ):
2641 self .metadata = None
2742 self ._retrieve_metadata ()
43+ self .graph_metadata = None
44+ self ._retrieve_graph_metadata ()
45+ self .schema = None
46+ self ._retrieve_schema ()
2847 self .meta_kg = None
2948 self .meta_kg_response = None
3049 self .predicates_in_graph = set ()
@@ -39,12 +58,19 @@ def get_metadata(self):
3958 return self .metadata
4059
4160 def _retrieve_metadata (self ):
42- with open (os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'metadata' , 'metadata.json' )) as f :
43- self .metadata = json .load (f )
61+ self .metadata = self ._load_json ('metadata.json' , {})
62+
63+ def get_graph_metadata (self ):
64+ return self .graph_metadata
65+
66+ def _retrieve_graph_metadata (self ):
67+ self .graph_metadata = self ._load_json ('graph_metadata.json' , {})
68+
69+ def get_schema (self ):
70+ return self .schema
4471
45- if not self .metadata :
46- with open (os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'metadata' , 'about.json' )) as f :
47- self .metadata = json .load (f )
72+ def _retrieve_schema (self ):
73+ self .schema = self ._load_json ('schema.json' , {})
4874
4975 def get_meta_kg (self ):
5076 return self .meta_kg
@@ -53,31 +79,33 @@ def get_meta_kg_response(self):
5379 return self .meta_kg_response
5480
5581 def _retrieve_meta_kg (self ):
56- with open (os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'metadata' , 'meta_knowledge_graph.json' )) as f :
57- self .meta_kg = json .load (f )
58- try :
59- # validate the meta kg with the pydantic model
60- MetaKnowledgeGraph .parse_obj (self .meta_kg )
61- logger .info ('Successfully validated meta kg' )
82+ self .meta_kg = self ._load_json ('meta_knowledge_graph.json' , {"nodes" : {}, "edges" : []})
83+ try :
84+ # validate the meta kg with the pydantic model
85+ MetaKnowledgeGraph .parse_obj (self .meta_kg )
86+ logger .info ('Successfully validated meta kg' )
6287
63- self .node_categories_in_graph = set (self .meta_kg ['nodes' ].keys ())
64- logger .info (f'Used meta kg to determine node categories in graph: { self .node_categories_in_graph } ' )
88+ self .node_categories_in_graph = set (self .meta_kg ['nodes' ].keys ())
89+ logger .info (f'Used meta kg to determine node categories in graph: { self .node_categories_in_graph } ' )
6590
66- for edge in self .meta_kg ['edges' ]:
67- self .predicates_in_graph .add (edge ['predicate' ])
68- logger .info (f'Used meta kg to determine predicates in graph: { self .predicates_in_graph } ' )
91+ for edge in self .meta_kg ['edges' ]:
92+ self .predicates_in_graph .add (edge ['predicate' ])
93+ logger .info (f'Used meta kg to determine predicates in graph: { self .predicates_in_graph } ' )
6994
70- # create an already-encoded object that is ready to be returned quickly
71- self .meta_kg_response = jsonable_encoder (self .meta_kg )
72- except ValidationError as e :
73- logger .error (f'Error validating meta kg: { e } ' )
95+ # create an already-encoded object that is ready to be returned quickly
96+ self .meta_kg_response = jsonable_encoder (self .meta_kg )
97+ except ValidationError as e :
98+ logger .error (f'Error validating meta kg: { e } ' )
7499
75100 def get_sri_testing_data (self ):
76101 return self .sri_testing_data
77102
78103 def _retrieve_sri_test_data (self ):
79- with open (os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'metadata' , 'sri_testing_data.json' )) as f :
80- self .sri_testing_data = json .load (f )
104+ self .sri_testing_data = self ._load_json ('sri_testing_data.json' , {
105+ "version" : "" ,
106+ "source_type" : "primary" ,
107+ "edges" : [],
108+ })
81109
82110 # version is technically not part of the spec anymore
83111 # but this ensures validation with the model until it's removed
0 commit comments