classMetaData:""" JSON metadata object :param folder: path to the dataherb folder """def__init__(self,folder:Path):self.dataherb_folder=folderself.metadata_file="dataherb.json"self.metadata:dict={}defload(self)->dict:"""load the existing datapackage file"""metadata_full_path=self.dataherb_folder/self.metadata_filelogger.debug(f"Load metadata from file: {metadata_full_path}")withopen(metadata_full_path,"r")asfp:self.metadata=json.load(fp)logger.debug(f"Loaded metadata: {self.metadata}")returnself.metadatadefcreate(self,overwrite:bool=False)->None:"""creates .dataherb folder"""try:self.dataherb_folder.mkdir(parents=True,exist_ok=False)logger.info("Created ",self.dataherb_folder)exceptFileExistsError:logger.warning(f"{self.dataherb_folder} already exists! Will use the folder.""Pass the flag `overwrite=True` to recreate it, if one desires.")metadata_full_path=self.dataherb_folder/self.metadata_fileifmetadata_full_path.is_file():ifnotoverwrite:raiseFileExistsError(f"File {metadata_full_path} already exists!")else:logger.warning(f"Will overwrite {metadata_full_path}")withopen(metadata_full_path,"w")asfp:json.dump(self.metadata,fp,indent=4)logger.debug(f"written to {metadata_full_path}")defvalidate(self)->None:"""validate the existing metadata file"""metadata_full_path=self.dataherb_folder/self.metadata_fileself._validate_paths()withopen(metadata_full_path,"r")asfp:metadata=json.load(fp)logger.info("loaded metadata ",self.dataherb_folder)logger.debug(f"loaded metadata {metadata}")def_validate_paths(self)->None:"""Check if the metadata path exists"""metadata_full_path=self.dataherb_folder/self.metadata_fileifnotself.dataherb_folder.exists():raiseException(f"Path {self.dataherb_folder} doesn't exist!")else:logger.info(f"Path {self.dataherb_folder} exists.")ifnotmetadata_full_path.is_file():raiseFileNotFoundError(f"File {metadata_full_path} doesn't exist!")else:logger.info(f"File {metadata_full_path} exists!")
defcreate(self,overwrite:bool=False)->None:"""creates .dataherb folder"""try:self.dataherb_folder.mkdir(parents=True,exist_ok=False)logger.info("Created ",self.dataherb_folder)exceptFileExistsError:logger.warning(f"{self.dataherb_folder} already exists! Will use the folder.""Pass the flag `overwrite=True` to recreate it, if one desires.")metadata_full_path=self.dataherb_folder/self.metadata_fileifmetadata_full_path.is_file():ifnotoverwrite:raiseFileExistsError(f"File {metadata_full_path} already exists!")else:logger.warning(f"Will overwrite {metadata_full_path}")withopen(metadata_full_path,"w")asfp:json.dump(self.metadata,fp,indent=4)logger.debug(f"written to {metadata_full_path}")