def _run(self, args: argparse.Namespace) -> int: """Remove an OSCAL component/subcomponent to the specified component. This method takes input a filename and a list of comma-seperated element path. Element paths are field aliases. The method first finds the parent model from the file and loads the file into the model. Then the method executes 'remove' for each of the element paths specified. """ try: log.set_log_level_from_args(args) args_dict = args.__dict__ file_path = pathlib.Path(args_dict[const.ARG_FILE]).resolve() relative_path = file_path.relative_to(args.trestle_root) # Get parent model and then load json into parent model parent_model, parent_alias = ModelUtils.get_relative_model_type( relative_path) parent_object = parent_model.oscal_read(file_path) parent_element = Element(parent_object, parent_alias) add_plan = Plan() # Do _remove for each element_path specified in args element_paths: List[str] = str( args_dict[const.ARG_ELEMENT]).split(',') for elm_path_str in element_paths: element_path = ElementPath(elm_path_str) remove_action, parent_element = self.remove( element_path, parent_element) add_plan.add_action(remove_action) create_action = CreatePathAction(file_path, True) write_action = WriteFileAction( file_path, parent_element, FileContentType.to_content_type(file_path.suffix)) add_plan.add_action(remove_action) add_plan.add_action(create_action) add_plan.add_action(write_action) add_plan.execute() return CmdReturnCodes.SUCCESS.value except Exception as e: return err.handle_generic_command_exception( e, logger, 'Error while removing OSCAL component')
def test_get_relative_model_type(tmp_path: pathlib.Path) -> None: """Test get model type and alias based on filesystem context.""" import trestle.common.type_utils as cutils with pytest.raises(TrestleError): ModelUtils.get_relative_model_type(pathlib.Path('invalidpath')) with pytest.raises(TrestleError): ModelUtils.get_relative_model_type(pathlib.Path('./')) catalogs_dir = pathlib.Path('catalogs') mycatalog_dir = catalogs_dir / 'mycatalog' catalog_dir = mycatalog_dir / 'catalog' metadata_dir = catalog_dir / 'metadata' roles_dir = metadata_dir / 'roles' rps_dir = metadata_dir / 'responsible-parties' props_dir = metadata_dir / 'props' groups_dir = catalog_dir / 'groups' group_dir = groups_dir / f'00000{const.IDX_SEP}group' controls_dir = group_dir / 'controls' with pytest.raises(TrestleError): ModelUtils.get_relative_model_type(catalogs_dir) assert ModelUtils.get_relative_model_type(mycatalog_dir) == ( catalog.Catalog, 'catalog') assert ModelUtils.get_relative_model_type( mycatalog_dir / 'catalog.json') == (catalog.Catalog, 'catalog') assert ModelUtils.get_relative_model_type( catalog_dir / 'back-matter.json') == (common.BackMatter, 'catalog.back-matter') assert ModelUtils.get_relative_model_type( catalog_dir / 'metadata.yaml') == (common.Metadata, 'catalog.metadata') assert ModelUtils.get_relative_model_type(metadata_dir) == ( common.Metadata, 'catalog.metadata') assert ModelUtils.get_relative_model_type(roles_dir) == ( List[common.Role], 'catalog.metadata.roles') (type_, element) = ModelUtils.get_relative_model_type(roles_dir) assert cutils.get_origin(type_) == list assert element == 'catalog.metadata.roles' assert ModelUtils.get_relative_model_type( roles_dir / '00000__role.json') == (common.Role, 'catalog.metadata.roles.role') model_type, full_alias = ModelUtils.get_relative_model_type(rps_dir) assert model_type == List[common.ResponsibleParty] assert full_alias == 'catalog.metadata.responsible-parties' assert ModelUtils.get_relative_model_type( rps_dir / 'creator__responsible-party.json') == ( common.ResponsibleParty, 'catalog.metadata.responsible-parties.responsible-party') (type_, element) = ModelUtils.get_relative_model_type(props_dir) assert cutils.get_origin(type_) == list assert cutils.get_inner_type(type_) == common.Property assert element == 'catalog.metadata.props' (expected_type, expected_json_path) = ModelUtils.get_relative_model_type( props_dir / f'00000{const.IDX_SEP}property.json') assert expected_type == common.Property assert expected_json_path == 'catalog.metadata.props.property' assert cutils.get_origin(type_) == list assert ModelUtils.get_relative_model_type( groups_dir / f'00000{const.IDX_SEP}group.json') == (catalog.Group, 'catalog.groups.group') assert ModelUtils.get_relative_model_type(group_dir) == ( catalog.Group, 'catalog.groups.group') assert ModelUtils.get_relative_model_type( controls_dir / f'00000{const.IDX_SEP}control.json') == ( catalog.Control, 'catalog.groups.group.controls.control')