@@ -31,32 +31,35 @@ def __init__(self):
3131 """Initialise by pre-loading the ARGO vocab terms."""
3232 self .argo_vocab_terms = get_all_terms_from_argo_vocabs ()
3333
34- def load_json_data (self , json_files : list [str ]):
34+ def load_json_data (self , json_obj : list [str | dict ]):
3535 """Take a list of JSON files and load content into memory.
3636
3737 Args:
38- json_files (list[str]): List of file paths.
38+ json_obj (list[str | dict ]): List of file paths or JSON dictionary
3939 """
40- json_file_paths = [Path (x ) for x in json_files ]
41-
4240 self .all_json_data = {}
43- for file in json_file_paths :
44- if not file .exists ():
45- raise Exception (f"Provided JSON file could not be found: { file } " )
46-
47- # Load the JSON into memory
48- self .all_json_data [file .name ] = load_json (file )
49-
50- def validate (self , json_files : list [str ]) -> dict [str , list [ValidationError ]]:
51- """Takes a list of JSON files and validates each.
41+ for item in json_obj :
42+ if isinstance (item , dict ):
43+ # Load the JSON object into memory
44+ self .all_json_data .update ({f"JSdict.{ id (item )} " : item })
45+ else :
46+ file = Path (item )
47+ if not file .exists ():
48+ raise Exception (f"Provided JSON file could not be found: { file } " )
49+ else :
50+ # Load the JSON file into memory
51+ self .all_json_data [file .name ] = load_json (Path (item ))
52+
53+ def validate (self , json_obj : list [str | dict ]) -> dict [str , list [ValidationError ]]:
54+ """Takes a list of JSON files or dictionary and validates each.
5255
5356 Args:
54- json_files (list[str]): List of file paths.
57+ json_obj (list[str|dict ]): List of file paths or JSON dictionary
5558
5659 Returns:
5760 dict[str, list[str]]: Errors, keyed by the input filename.
5861 """
59- self .load_json_data (json_files )
62+ self .load_json_data (json_obj )
6063
6164 self .validation_errors = {}
6265 for file , json_data in self .all_json_data .items ():
@@ -66,21 +69,21 @@ def validate(self, json_files: list[str]) -> dict[str, list[ValidationError]]:
6669 self .validation_errors [file ] += self ._validate_vocabs (json_data )
6770 return self .validation_errors
6871
69- def parse (self , json_file : str ) -> Sensor | Float | Platform :
72+ def parse (self , json_obj : str | dict ) -> Sensor | Float | Platform :
7073 """Parses provided metadata into Pydantic models.
7174
7275 Args:
73- json_file (str): Path of an input JSON file.
76+ json_obj (str | dict ): Path of an input JSON file or JSON data already in memory
7477
7578 Returns:
7679 _type_: _description_
7780 """
78- errors = self .validate ([json_file ])
79- errors = errors [Path (json_file ).name ]
80- if errors :
81+ errors = self .validate ([json_obj ])
82+ if any ([len (errors [e ]) > 0 for e in errors ]): # noqa C419
8183 raise Exception ("Data not valid, run the validation function for detailed errors." )
8284
83- data = self .all_json_data [Path (json_file ).name ]
85+ key = f"JSdict.{ id (json_obj )} " if isinstance (json_obj , dict ) else Path (json_obj ).name
86+ data = self .all_json_data [key ]
8487 schema_type = infer_schema_from_data (data )
8588 if schema_type == SENSOR_SCHEMA :
8689 return Sensor (** data )
@@ -101,6 +104,7 @@ def _validate_json(self, json_data: Any) -> list[ValidationError]:
101104 """
102105 schema_type = infer_schema_from_data (json_data )
103106 schema_version = infer_version_from_data (json_data )
107+ # print(f"Validating against schema version {schema_version}")
104108 json_validator = get_json_validator (schema_type , version = schema_version )
105109
106110 errors = []
0 commit comments