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

53 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 add a feature extractor to the Sparkle platform.""" 

3import os 

4import sys 

5import shutil 

6import argparse 

7from pathlib import Path 

8 

9from sparkle.platform import file_help as sfh 

10from sparkle.CLI.help import global_variables as gv 

11from sparkle.structures import FeatureDataFrame 

12from sparkle.CLI.help import logging as sl 

13from sparkle.CLI.initialise import check_for_initialise 

14from sparkle.CLI.help import argparse_custom as ac 

15from sparkle.solver import Extractor 

16 

17 

18def parser_function() -> argparse.ArgumentParser: 

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

20 # Define command line arguments 

21 parser = argparse.ArgumentParser( 

22 description="Add a feature extractor to the platform.") 

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

24 **ac.ExtractorPathArgument.kwargs) 

25 parser.add_argument(*ac.NicknameFeatureExtractorArgument.names, 

26 **ac.NicknameFeatureExtractorArgument.kwargs) 

27 parser.add_argument(*ac.NoCopyArgument.names, **ac.NoCopyArgument.kwargs) 

28 return parser 

29 

30 

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

32 """Main function of the add feature extractor command.""" 

33 # Log command call 

34 sl.log_command(sys.argv) 

35 check_for_initialise() 

36 

37 parser = parser_function() 

38 

39 # Process command line arguments 

40 args = parser.parse_args(argv) 

41 

42 extractor_source = Path(args.extractor_path) 

43 if not extractor_source.exists(): 

44 print(f'Feature extractor path "{extractor_source}" does not exist!') 

45 sys.exit(-1) 

46 

47 nickname_str = args.nickname 

48 

49 # Start add feature extractor 

50 extractor_target_path = gv.settings().DEFAULT_extractor_dir / extractor_source.name 

51 

52 if extractor_target_path.exists(): 

53 print(f"Feature extractor {extractor_source.name} already exists! " 

54 "Can not add feature extractor.") 

55 sys.exit(-1) 

56 

57 if args.no_copy: 

58 print(f"Creating symbolic link from {extractor_source} " 

59 f"to {extractor_target_path}...") 

60 extractor_target_path.symlink_to(extractor_source.absolute()) 

61 else: 

62 print(f"Copying feature extractor {extractor_source.name} ...") 

63 extractor_target_path.mkdir() 

64 shutil.copytree(extractor_source, extractor_target_path, dirs_exist_ok=True) 

65 

66 # Check execution permissions for wrapper 

67 extractor_wrapper = extractor_target_path / Extractor.wrapper 

68 if not extractor_wrapper.is_file() or not os.access(extractor_wrapper, os.X_OK): 

69 print(f"The file {extractor_wrapper} does not exist or is \ 

70 not executable.") 

71 sys.exit(-1) 

72 

73 # Get the extractor features groups and names from the wrapper 

74 extractor = Extractor(extractor_target_path) 

75 feature_dataframe = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path) 

76 feature_dataframe.add_extractor(extractor.name, extractor.features) 

77 feature_dataframe.save_csv() 

78 

79 print(f"Adding feature extractor {extractor_target_path.name} done!") 

80 

81 if nickname_str is not None: 

82 sfh.add_remove_platform_item( 

83 extractor_target_path, 

84 gv.extractor_nickname_list_path, 

85 gv.file_storage_data_mapping[gv.extractor_nickname_list_path], 

86 key=nickname_str) 

87 

88 # Write used settings to file 

89 gv.settings().write_used_settings() 

90 sys.exit(0) 

91 

92 

93if __name__ == "__main__": 

94 main(sys.argv[1:])