Coverage for sparkle/CLI/remove_feature_extractor.py: 85%

41 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-07 15:22 +0000

1#!/usr/bin/env python3 

2"""Sparkle command to remove a feature extractor from the Sparkle platform.""" 

3 

4import sys 

5import argparse 

6import shutil 

7 

8from sparkle.platform import file_help as sfh 

9from sparkle.CLI.help import global_variables as gv 

10from sparkle.structures import FeatureDataFrame 

11from sparkle.CLI.help import logging as sl 

12from sparkle.CLI.initialise import check_for_initialise 

13from sparkle.CLI.help import argparse_custom as ac 

14from sparkle.CLI.help.nicknames import resolve_object_name 

15from sparkle.solver import Extractor 

16 

17 

18def parser_function() -> argparse.ArgumentParser: 

19 """Define the command line arguments.""" 

20 parser = argparse.ArgumentParser(description="Remove a feature extractor " 

21 "from the platform.") 

22 parser.add_argument(*ac.ExtractorPathArgument.names, 

23 **ac.ExtractorPathArgument.kwargs) 

24 return parser 

25 

26 

27def main(argv: list[str]) -> None: 

28 """Main function of the remove feature extractor command.""" 

29 # Log command call 

30 sl.log_command(sys.argv) 

31 check_for_initialise() 

32 

33 # Define command line arguments 

34 parser = parser_function() 

35 

36 # Process command line arguments 

37 args = parser.parse_args(argv) 

38 extractor_nicknames = gv.file_storage_data_mapping[gv.extractor_nickname_list_path] 

39 extractor = resolve_object_name( 

40 args.extractor_path, 

41 extractor_nicknames, 

42 gv.settings().DEFAULT_extractor_dir, 

43 class_name=Extractor) 

44 

45 if extractor is None: 

46 print(f'Feature extractor path "{args.extractor_path}" does not exist!') 

47 sys.exit(-1) 

48 

49 print(f"Starting removing feature extractor {extractor.name} ...") 

50 

51 for key in extractor_nicknames: 

52 if extractor_nicknames == extractor.directory: 

53 sfh.add_remove_platform_item( 

54 None, 

55 gv.extractor_nickname_list_path, 

56 extractor_nicknames, 

57 key=key, 

58 remove=True) 

59 break 

60 

61 if gv.settings().DEFAULT_feature_data_path.exists(): 

62 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path) 

63 feature_data.remove_extractor(extractor.name) 

64 feature_data.save_csv() 

65 

66 # We unlink symbolics links, erase copies 

67 if extractor.directory.is_symlink(): 

68 extractor.directory.unlink() 

69 else: 

70 # Remove the directory and all its files 

71 shutil.rmtree(extractor.directory) 

72 

73 print(f"Removing feature extractor {extractor.name} done!") 

74 sys.exit(0) 

75 

76 

77if __name__ == "__main__": 

78 main(sys.argv[1:])