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

40 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-05 14:48 +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.platform import CommandName, COMMAND_DEPENDENCIES 

13from sparkle.CLI.initialise import check_for_initialise 

14from sparkle.CLI.help import argparse_custom as ac 

15from sparkle.CLI.help.nicknames import resolve_object_name 

16from sparkle.solver import Extractor 

17 

18 

19def parser_function() -> argparse.ArgumentParser: 

20 """Define the command line arguments.""" 

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

22 "from the platform.") 

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

24 **ac.ExtractorPathArgument.kwargs) 

25 return parser 

26 

27 

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

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

30 # Log command call 

31 sl.log_command(sys.argv) 

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 check_for_initialise(COMMAND_DEPENDENCIES[CommandName.REMOVE_FEATURE_EXTRACTOR]) 

46 

47 if extractor is None: 

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

49 sys.exit(-1) 

50 

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

52 

53 for key in extractor_nicknames: 

54 if extractor_nicknames == extractor.directory: 

55 sfh.add_remove_platform_item( 

56 None, 

57 gv.extractor_nickname_list_path, 

58 extractor_nicknames, 

59 key=key, 

60 remove=True) 

61 break 

62 

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

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

65 feature_data.remove_extractor(extractor.name) 

66 feature_data.save_csv() 

67 shutil.rmtree(extractor.directory) 

68 

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

70 sys.exit(0) 

71 

72 

73if __name__ == "__main__": 

74 main(sys.argv[1:])