Source code for httomo_backends.scripts.yaml_unsupported_tomopy_remove
#!/usr/bin/env python3# -*- coding: utf-8 -*-# ---------------------------------------------------------------------------# Copyright 2022 Diamond Light Source Ltd.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# ---------------------------------------------------------------------------# Created By : Daniil Kazantsev <scientificsoftware@diamond.ac.uk># Created Date: 13/April/2023# version ='0.1'# ---------------------------------------------------------------------------"""After _all_ templates have been generated for TomoPy we need to remove the onesthat are not currently supported by httomo. We do that by looking intothe library file for TomoPy."""importargparseimportimportlibimportinspectimportosimportreimportshutilfrompathlibimportPathimportyaml
[docs]deftemplates_filter(path_to_modules:str,library_file:str)->int:"""function that removes unsupported by httomo YAML templates in TomoPy Args: path_to_modules (str): path to the list of modules yaml file library_file (str): path to the library with the supported functions of TomoPy Returns: int: returns zero if the processing is succesfull """software_name="tomopy"yaml_info_path=Path(library_file)ifnotyaml_info_path.exists():err_str=f"The YAML file {yaml_info_path} doesn't exist."raiseValueError(err_str)withopen(yaml_info_path,"r")asf:yaml_library=yaml.safe_load(f)methods_list:list=[]formodule,module_dictinyaml_library.items():formodule2,module_dict2inmodule_dict.items():formethod_nameinmodule_dict2:methods_list.append(method_name)subfolders=[f.pathforfinos.scandir(path_to_modules)iff.is_dir()]forfolderinsubfolders:forfilenameinos.listdir(folder):filename_short=filename.split(".")iffilename_short[0]notinmethods_list:print(f"Removed template: {filename_short[0]}")file_path=os.path.join(folder,filename)try:ifos.path.isfile(file_path)oros.path.islink(file_path):os.unlink(file_path)exceptExceptionase:print("Failed to delete %s. Reason: %s"%(file_path,e))return0
[docs]defget_args():parser=argparse.ArgumentParser(description="Removes unsupported by httomo templates in TomoPy.")parser.add_argument("-t","--templates",type=str,default=None,help="A path to the folder where generated templates stored.",)parser.add_argument("-l","--library",type=str,default=None,help="A path to the library YAML file with the supported functions.",)returnparser.parse_args()
if__name__=="__main__":current_dir=os.path.basename(os.path.abspath(os.curdir))args=get_args()path_to_modules=args.templateslibrary_file=args.libraryreturn_val=templates_filter(path_to_modules,library_file)ifreturn_val==0:print("The templates have been filtered!")