11package edu .kit .datamanager .semplugin ;
22
3- import edu .kit .datamanager .mappingservice .exception .PluginInitializationFailedException ;
4- import edu .kit .datamanager .mappingservice .plugins .*;
5- import edu .kit .datamanager .mappingservice .util .*;
6- import org .slf4j .Logger ;
7- import org .slf4j .LoggerFactory ;
8- import org .springframework .util .MimeType ;
9- import org .springframework .util .MimeTypeUtils ;
10-
11- import java .io .IOException ;
12- import java .io .InputStream ;
13- import java .net .URL ;
3+ import edu .kit .datamanager .mappingservice .plugins .AbstractPythonMappingPlugin ;
144import java .nio .file .Path ;
15- import java .util .Properties ;
16-
17- public class SEMImagePlugin implements IMappingPlugin {
185
19- private static String version ;
6+ public class SEMImagePlugin extends AbstractPythonMappingPlugin {
207
21- private final Logger LOGGER = LoggerFactory .getLogger (SEMImagePlugin .class );
22- private final String REPOSITORY = "https://github.com/kit-data-manager/tomo_mapper" ;
23- private String TAG ;
24- private Path dir ;
8+ private static final String REPOSITORY = "https://github.com/kit-data-manager/tomo_mapper" ;
259
26- private String pluginVenv = "venv/PluginVenv" ;
27- private String venvInterpreter ;
2810
2911 public SEMImagePlugin () {
30- try {
31- // Get the context class loader
32- ClassLoader classLoader = this .getClass ().getClassLoader ();
33- // TODO: do we need to make sure that the resource path is somehow related to the current plugin to avoid loading the wrong property file in case of identical property names?
34- URL resource = classLoader .getResource ("sempluginversion.properties" );
35- LOGGER .info ("Resource file: {}" , resource );
36- if (resource != null ) {
37- // Load the properties file
38- try (InputStream input = resource .openStream ()) {
39- Properties properties = new Properties ();
40- properties .load (input );
41- version = properties .getProperty ("version" );
42- TAG = version ;
43- }
44- } else {
45- System .err .println ("Properties file not found!" );
46- version = "unavailable" ;
47- TAG = "unavailable" ;
48- }
49-
50- if (System .getProperty ("os.name" ).startsWith ("Windows" )) {
51- venvInterpreter = pluginVenv + "/Scripts/python.exe" ;
52- } else {
53- venvInterpreter = pluginVenv + "/bin/python3" ;
54- }
55-
56- } catch (IOException e ) {
57- throw new PluginInitializationFailedException ("Failed to instantiate plugin class." , e );
58- }
12+ super ("GenericSEMtoJSON" , REPOSITORY );
5913 }
6014
6115 @ Override
@@ -69,53 +23,26 @@ public String description() {
6923 }
7024
7125 @ Override
72- public String version () {
73- return version ;
74- }
75-
76- @ Override
77- public String uri () {
78- return REPOSITORY ;
79- }
80-
81- @ Override
82- public MimeType [] inputTypes () {
83- return new MimeType []{MimeTypeUtils .parseMimeType ("image/tiff" )}; //should currently be IMAGE/TIFF
84- }
85-
86- @ Override
87- public MimeType [] outputTypes () {
88- return new MimeType []{MimeTypeUtils .APPLICATION_JSON };
26+ public String [] inputTypes () {
27+ return new String []{"image/tiff" };
8928 }
9029
9130 @ Override
92- public void setup () {
93- LOGGER .trace ("Setting up mapping plugin {} {}" , name (), version ());
94- //TODO: test for minimal python version?
95- try {
96- LOGGER .info ("Cloning git repository {}, Tag {}" , REPOSITORY , TAG );
97- dir = FileUtil .cloneGitRepository (REPOSITORY , TAG );
98- // Install Python dependencies
99- MappingPluginState venvState = PythonRunnerUtil .runPythonScript ("-m" , "venv" , "--system-site-packages" , dir + "/" + pluginVenv );
100- if (MappingPluginState .SUCCESS ().getState ().equals (venvState .getState ())) {
101- LOGGER .info ("Venv for plugin installed successfully. Installing packages." );
102- ShellRunnerUtil .run (dir + "/" + venvInterpreter , "-m" , "pip" , "install" , "-r" , dir + "/" + "requirements.dist.txt" );
103- } else {
104- throw new PluginInitializationFailedException ("Venv installation was not successful. Status: " + venvState .getState ());
105- }
106- } catch (MappingPluginException e ) {
107- throw new PluginInitializationFailedException ("Unexpected error during plugin setup." , e );
108- }
31+ public String [] outputTypes () {
32+ return new String []{"application/json" };
10933 }
11034
11135 @ Override
112- public MappingPluginState mapFile (Path mappingFile , Path inputFile , Path outputFile ) throws MappingPluginException {
113- long startTime = System .currentTimeMillis ();
114- LOGGER .trace ("Run SEM-Mapping-Tool on '{}' with mapping '{}' -> '{}'" , mappingFile , inputFile , outputFile );
115- MappingPluginState result = ShellRunnerUtil .run (dir + "/" + venvInterpreter , dir + "/plugin_wrapper.py" , "sem" , "-m" , mappingFile .toString (), "-i" , inputFile .toString (), "-o" , outputFile .toString ());
116- long endTime = System .currentTimeMillis ();
117- long totalTime = endTime - startTime ;
118- LOGGER .info ("Execution time of mapFile: {} milliseconds" , totalTime );
119- return result ;
36+ public String [] getCommandArray (Path workingDir , Path mappingFile , Path inputFile , Path outputFile ) {
37+ return new String []{
38+ workingDir + "/plugin_wrapper.py" ,
39+ "sem" ,
40+ "-m" ,
41+ mappingFile .toString (),
42+ "-i" ,
43+ inputFile .toString (),
44+ "-o" ,
45+ outputFile .toString ()
46+ };
12047 }
12148}
0 commit comments