[docs]classGenerateModelDocumentation(ComputationStep):""" Generates Model Documentation from schema provided in the model config file """# Command line options
[docs]step_params=[{'name':'doc_out_dir','flag':'-o','is_path':True,'pre_exist':False,'help':'Path to the directory in which to generate the Documentation files','default':'.'},{'name':'doc_json','flag':'-d','is_path':True,'pre_exist':True,'required':True,'help':'The json file containing model meta-data for documentation'},{'name':'doc_schema_info','flag':'-s','is_path':True,'pre_exist':True,'required':False,'help':'The schema for the model meta-data json'},]
[docs]defvalidate_doc_schema(self,schema_path,docjson_path):"""Validates docjson_path file with schema_path file Args: schema_path (str | os.PathLike): Schema path file docjson_path (str | os.PathLike): Documentation JSON path file Returns: docjson (Dict): Json data loaded as a dictionary """withopen(schema_path,"r")asf:schema=json.load(f)withopen(docjson_path,"r")asf:docjson=json.load(f)if"datasets"notindocjson:raiseValidationError(f"key \'datasets\' not found inside {docjson_path}")datasets=docjson["datasets"]fori,datasetinenumerate(datasets):try:validate(instance=dataset,schema=schema)exceptValidationErrorase:raiseValidationError(f"doc schema validation error for dataset idx {i}: {e.message}")returndocjson,schema
[docs]defjson_to_mdtxt(self,json_data,full_schema,data_path,doc_out_dir):"""Convert json data to markdown text with schemas provided Args: json_data (dict): Json data as dictionary full_schema (dict): Full schema file as dictionary data_path (str | os.PathLike): Path to data folder for any relative file paths doc_out_dir (str | os.PathLike): Path to documentation file output folder for any relative file paths """schema_id=full_schema["$id"]json_to_md_generator=DefaultJsonToMarkdownGeneratorifschema_id=="https://docs.riskdatalibrary.org/en/0__2__0/rdls_schema.json":# RDLS v0.2json_to_md_generator=RDLS_0_2_0_JsonToMarkdownGeneratorelse:self.logger.warning(f"WARN: Unsupported formatting for following schema: {schema_id}. Using DefaultJsonToMarkdownGenerator output")gen=json_to_md_generator(full_schema,data_path,doc_out_dir)returngen.generate(json_data,generate_toc=True)
[docs]defrun(self):ifnotos.path.exists(self.doc_json):raiseFileNotFoundError(f'Could not locate doc_json file: {self.doc_json}, Cannot generate documentation')ifnotself.doc_schema_info:self.doc_schema_info=resources.files('rdls').joinpath('rdls_schema.json')ifnotos.path.exists(self.doc_schema_info):raiseFileNotFoundError(f'Could not locate doc_schema_info file: {self.doc_schema_info}, Cannot generate documentation')doc_out_dir=Path(self.doc_out_dir)doc_json=Path(self.doc_json)data_path=doc_json.parentdoc_schema_info=Path(self.doc_schema_info)doc_file=Path(doc_out_dir,'doc.md')json_data,schema=self.validate_doc_schema(doc_schema_info,doc_json)withopen(doc_file,"w")asf:mdtxt=self.json_to_mdtxt(json_data,schema,data_path,doc_out_dir)f.write(mdtxt)