def test_path_to_file_content_type(tmp_trestle_dir: Path) -> None: """Test path_to_file_content_type method.""" tmp_stem = tmp_trestle_dir / 'content_type_test' tmp_file = tmp_stem.with_suffix('.json') tmp_file.touch() assert FileContentType.JSON == FileContentType.path_to_content_type( tmp_file) assert FileContentType.path_to_file_extension(tmp_file) == '.json' tmp_file.unlink() tmp_file = tmp_stem.with_suffix('.yaml') tmp_file.touch() assert FileContentType.YAML == FileContentType.path_to_content_type( tmp_file) assert FileContentType.path_to_file_extension(tmp_file) == '.yaml' tmp_file.unlink() tmp_file = tmp_stem.with_suffix('.yml') tmp_file.touch() assert FileContentType.YAML == FileContentType.path_to_content_type( tmp_file) assert FileContentType.path_to_file_extension(tmp_file) == '.yml' tmp_file.unlink() assert FileContentType.UNKNOWN == FileContentType.path_to_content_type( tmp_file) assert FileContentType.path_to_file_extension(tmp_file) == ''
def __init__(self, root_dir: pathlib.Path, model_type: Type[OscalBaseModel], name: str) -> None: """Initialize repository OSCAL model object.""" if not file_utils.is_valid_project_root(root_dir): raise TrestleError(f'Provided root directory {str(root_dir)} is not a valid Trestle root directory.') self._root_dir = root_dir self._model_type = model_type self._model_name = name # set model alais and dir self.model_alias = classname_to_alias(self._model_type.__name__, AliasMode.JSON) if parser.to_full_model_name(self.model_alias) is None: raise TrestleError(f'Given model {self.model_alias} is not a top level model.') plural_path = ModelUtils.model_type_to_model_dir(self.model_alias) self.model_dir = self._root_dir / plural_path / self._model_name if not self.model_dir.exists() or not self.model_dir.is_dir(): raise TrestleError(f'Model dir {self._model_name} does not exist.') file_content_type = FileContentType.path_to_content_type(self.model_dir / self.model_alias) if file_content_type == FileContentType.UNKNOWN: raise TrestleError(f'Model file for model {self._model_name} does not exist.') self.file_content_type = file_content_type filepath = pathlib.Path( self.model_dir, self.model_alias + FileContentType.path_to_file_extension(self.model_dir / self.model_alias) ) self.filepath = filepath
def delete_model(self, model_type: Type[OscalBaseModel], name: str) -> bool: """Delete an OSCAL model from repository.""" logger.debug(f'Deleting model {name} of type {model_type.__name__}.') model_alias = classname_to_alias(model_type.__name__, AliasMode.JSON) if parser.to_full_model_name(model_alias) is None: raise TrestleError(f'Given model {model_alias} is not a top level model.') plural_path = ModelUtils.model_type_to_model_dir(model_alias) desired_model_dir = self._root_dir / plural_path / name if not desired_model_dir.exists() or not desired_model_dir.is_dir(): raise TrestleError(f'Model {name} does not exist.') shutil.rmtree(desired_model_dir) # remove model from dist directory if it exists dist_model_dir = self._root_dir / const.TRESTLE_DIST_DIR / plural_path file_content_type = FileContentType.path_to_content_type(dist_model_dir / name) if file_content_type != FileContentType.UNKNOWN: file_path = pathlib.Path( dist_model_dir, name + FileContentType.path_to_file_extension(dist_model_dir / name) ) logger.debug(f'Deleting model {name} from dist directory.') os.remove(file_path) logger.debug(f'Model {name} deleted successfully.') return True