Coverage for sparkle/CLI/remove_feature_extractor.py: 85%
41 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 10:17 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 10:17 +0000
1#!/usr/bin/env python3
2"""Sparkle command to remove a feature extractor from the Sparkle platform."""
4import sys
5import argparse
6import shutil
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.selector import Extractor
18def parser_function() -> argparse.ArgumentParser:
19 """Define the command line arguments."""
20 parser = argparse.ArgumentParser(
21 description="Remove a feature extractor from the platform."
22 )
23 parser.add_argument(
24 *ac.ExtractorPathArgument.names, **ac.ExtractorPathArgument.kwargs
25 )
26 return parser
29def main(argv: list[str]) -> None:
30 """Main function of the remove feature extractor command."""
31 # Log command call
32 sl.log_command(sys.argv, gv.settings().random_state)
33 check_for_initialise()
35 # Define command line arguments
36 parser = parser_function()
38 # Process command line arguments
39 args = parser.parse_args(argv)
40 extractor_nicknames = gv.file_storage_data_mapping[gv.extractor_nickname_list_path]
41 extractor = resolve_object_name(
42 args.extractor_path,
43 extractor_nicknames,
44 gv.settings().DEFAULT_extractor_dir,
45 class_name=Extractor,
46 )
48 if extractor is None:
49 print(f'Feature extractor path "{args.extractor_path}" does not exist!')
50 sys.exit(-1)
52 print(f"Starting removing feature extractor {extractor.name} ...")
54 for key in extractor_nicknames:
55 if extractor_nicknames == extractor.directory:
56 sfh.add_remove_platform_item(
57 None,
58 gv.extractor_nickname_list_path,
59 extractor_nicknames,
60 key=key,
61 remove=True,
62 )
63 break
65 if gv.settings().DEFAULT_feature_data_path.exists():
66 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path)
67 feature_data.remove_extractor(extractor.name)
68 feature_data.save_csv()
70 # We unlink symbolics links, erase copies
71 if extractor.directory.is_symlink():
72 extractor.directory.unlink()
73 else:
74 # Remove the directory and all its files
75 shutil.rmtree(extractor.directory)
77 print(f"Removing feature extractor {extractor.name} done!")
78 sys.exit(0)
81if __name__ == "__main__":
82 main(sys.argv[1:])