Skip to content

Commit

Permalink
Make a separate base class for command and conda CLI configs.
Browse files Browse the repository at this point in the history
Right now, the conda CLI config only works on windows.
  • Loading branch information
tinevez committed Jun 23, 2024
1 parent b885d1b commit e265a91
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 79 deletions.
78 changes: 22 additions & 56 deletions src/main/java/fiji/plugin/trackmate/util/cli/CLIConfigurator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fiji.plugin.trackmate.util.cli;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -18,8 +17,6 @@ public abstract class CLIConfigurator

protected final List< Argument< ? > > arguments = new ArrayList<>();

protected final ExecutablePath executable = new ExecutablePath();

protected final List< SelectableArguments > selectables = new ArrayList<>();

protected final Map< Command< ? >, Function< Object, List< String > > > translators = new HashMap<>();
Expand All @@ -29,19 +26,21 @@ public abstract class CLIConfigurator
*/

/**
* Returns the list of arguments in this CLI config. All arguments are
* present, regardless of whether they are in {@link SelectableArguments}.
*
* Returns the list of arguments (plus the command) in this CLI config. All
* arguments are present, regardless of whether they are in
* {@link SelectableArguments}, {@link Argument#visible} or not,
* {@link Argument#inCLI} or not.
*
* @return the list of arguments.
*/
public List< Argument< ? > > getArguments()
public List< Command< ? > > getArguments()
{
return Collections.unmodifiableList( arguments );
}

/**
* Returns the list of {@link SelectableArguments} in this CLI config.
*
*
* @return the list of {@link SelectableArguments}.
*/
public List< SelectableArguments > getSelectables()
Expand All @@ -54,7 +53,7 @@ public List< SelectableArguments > getSelectables()
* only the arguments that are selected if they are in a
* {@link SelectableArguments}, and those who are not in a
* {@link SelectableArguments}.
*
*
* @return the selected arguments.
*/
public List< Argument< ? > > getSelectedArguments()
Expand All @@ -65,18 +64,6 @@ public List< SelectableArguments > getSelectables()
return selectedArguments;
}

/*
* EXECUTABLE.
*/

/**
* Exposes the executable path argument.
*/
public ExecutablePath getExecutableArg()
{
return executable;
}

/*
* VALUE TRANSLATOR.
*/
Expand Down Expand Up @@ -518,10 +505,10 @@ protected ChoiceAdder addChoiceArgument()
{
return new ChoiceAdder();
}

/**
* Adds an extra argument, defined by other means than the adder methods.
*
*
* @param extraArg
* the argument to add to this CLI config.
* @return the argument
Expand Down Expand Up @@ -978,7 +965,7 @@ public String toString()
/**
* Sets the value of this argument via the specified object. This is
* used when deserializing TrackMate settings map.
*
*
* @param val
* the object to set the value from
* @see TrackMateSettingsBuilder
Expand All @@ -988,14 +975,14 @@ public String toString()
/**
* Returns an object built from the value of this argument, if it has
* one. This is used in TrackMate settings map serialization.
*
*
* @return the value object.
* @see TrackMateSettingsBuilder
*/
public abstract Object getValueObject();
}

public static class ExecutablePath extends Command< ExecutablePath >
public static class ExecutablePath extends Argument< ExecutablePath >
{

private String value = null;
Expand Down Expand Up @@ -1087,12 +1074,12 @@ public String getArgument()
{
return argument;
}

/**
* If <code>false</code>, this argument won't be used in the command
* line generator. This is useful to add extra parameters to the GUI
* that are required by TrackMate but not by the CLI tool.
*
*
* @param inCLI
* whether this argument should be used when generating
* commands. By default: <code>true</code>.
Expand Down Expand Up @@ -1125,31 +1112,11 @@ public String toString()
{
final StringBuilder str = new StringBuilder();
str.append( super.toString() + "\n" );
str.append( executable.toString() );
arguments.forEach( str::append );
return str.toString();

}

protected String checkExecutable()
{
if ( !executable.isSet() )
{
return "Executable path is not set.\n" ;
}
else
{
final String path = executable.getValue();
final File file = new File(path);
if ( !file.exists() )
return "Executable path " + path + " does not exist.\n";
if ( !file.canExecute() )
return "Executable " + path + " cannot be run.\n";
}
return null;
}

protected String checkArguments()
public String check()
{
final StringBuilder str = new StringBuilder();
for ( final Argument< ? > arg : getSelectedArguments() )
Expand All @@ -1167,11 +1134,10 @@ protected String checkArguments()
return str.length() == 0 ? null : str.toString();
}

public String check()
{
final String out = checkExecutable();
if ( out != null )
return out;
return checkArguments();
}
/**
* Returns the command object of this tool.
*
* @return the command object.
*/
public abstract Command< ? > getCommandArg();
}
11 changes: 4 additions & 7 deletions src/main/java/fiji/plugin/trackmate/util/cli/CliGuiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import fiji.plugin.trackmate.util.cli.CLIConfigurator.Argument;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.ArgumentVisitor;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.ChoiceArgument;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.Command;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.DoubleArgument;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.ExecutablePath;
import fiji.plugin.trackmate.util.cli.CLIConfigurator.Flag;
Expand Down Expand Up @@ -80,7 +81,7 @@ public class CliGuiBuilder implements ArgumentVisitor, StyleElementVisitor

private final List< StyleElement > elements = new ArrayList<>();

private CliGuiBuilder( final ExecutablePath executableArg )
private CliGuiBuilder()
{
this.panel = new CliConfigPanel();
final GridBagLayout layout = new GridBagLayout();
Expand All @@ -92,9 +93,6 @@ private CliGuiBuilder( final ExecutablePath executableArg )
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;

if ( executableArg.isVisible() )
visit( executableArg );
}

private void setCurrentButtonGroup( final ButtonGroup buttonGroup )
Expand Down Expand Up @@ -521,7 +519,7 @@ private void addLastRow()

public static CliConfigPanel build( final CLIConfigurator cli )
{
final CliGuiBuilder builder = new CliGuiBuilder( cli.getExecutableArg() );
final CliGuiBuilder builder = new CliGuiBuilder();

/*
* Iterate over CLI arguments.
Expand All @@ -531,7 +529,7 @@ public static CliConfigPanel build( final CLIConfigurator cli )
final Map< SelectableArguments, ButtonGroup > buttonGroups = new HashMap<>();

// Iterate over arguments, taking care of selectable group.
for ( final Argument< ? > arg : cli.getArguments() )
for ( final Command< ? > arg : cli.getArguments() )
{
if ( !arg.isVisible() )
continue;
Expand Down Expand Up @@ -583,5 +581,4 @@ public void refresh()
elements.forEach( e -> e.update() );
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public class CommandBuilder implements ArgumentVisitor

private final Map< Command< ? >, Function< Object, List< String > > > translators;

protected CommandBuilder( final ExecutablePath executableArg, final Map< Command< ? >, Function< Object, List< String > > > translators )
protected CommandBuilder( final Map< Command< ? >, Function< Object, List< String > > > translators )
{
this.translators = translators;
visit( executableArg );
}

@Override
Expand Down Expand Up @@ -177,7 +176,7 @@ public void visit( final ChoiceArgument arg )

public static List< String > build( final CLIConfigurator cli )
{
final CommandBuilder cb = new CommandBuilder( cli.getExecutableArg(), cli.translators );
final CommandBuilder cb = new CommandBuilder( cli.translators );
cli.getSelectedArguments()
.stream()
.filter( a -> a.isInCLI() )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fiji.plugin.trackmate.util.cli;

import java.io.File;

/**
* Base class for CLI config that are based on an executable, reachable by a
* path.
*/
public abstract class CommandCLIConfigurator extends CLIConfigurator
{

protected final ExecutablePath executable;

protected CommandCLIConfigurator()
{
this.executable = new ExecutablePath();
}

@Override
public ExecutablePath getCommandArg()
{
return executable;
}

protected String checkExecutable()
{
if ( !executable.isSet() )
{
return "Executable path is not set.\n";
}
else
{
final String path = executable.getValue();
final File file = new File( path );
if ( !file.exists() )
return "Executable path " + path + " does not exist.\n";
if ( !file.canExecute() )
return "Executable " + path + " cannot be run.\n";
}
return null;
}

@Override
public String check()
{
final String out = checkExecutable();
if ( out != null )
return out;
return super.check();
}

@Override
public String toString()
{
final StringBuilder str = new StringBuilder();
str.append( executable.toString() );
str.append( super.toString() + "\n" );
return str.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Arrays;
import java.util.List;

public class CondaCLIConfigurator extends CLIConfigurator
public abstract class CondaCLIConfigurator extends CLIConfigurator
{

public static final String KEY_CONDA_ENV = "CONDA_ENV";
Expand All @@ -28,12 +28,11 @@ protected CondaCLIConfigurator()
.inCLI( false )
.get();

// Don't show the executable arg in the UI: must be set by subclass, and
// only conda env required configurating.
getExecutableArg().visible( false );
// Add it first to the list of arguments
this.arguments.add( condaEnv );

// Add the translator to make a proper cmd line calling conda first.
setTranslator( getExecutableArg(), s -> {
setTranslator( condaEnv, s -> {
final String executableName = ( String ) s;
// Split by spaces
final String[] split = executableName.split( " " );
Expand All @@ -46,14 +45,18 @@ protected CondaCLIConfigurator()
} );
}

public ChoiceArgument getCondaEnv()
@Override
public Command< ? > getCommandArg()
{
return condaEnv;
}

@Override
public String check()
{
return checkArguments();
}
/**
* Returns the command that must be run in the configured conda environment.
* In case the command is made of several tokens, they can be returned
* separated by space (as in a normal command line).
*
* @return the command for this tool.
*/
protected abstract String getCommand();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static void fromMap( final Map< String, Object > settings, final Command
*/
public static void toTrackMateSettings( final Map< String, Object > settings, final CLIConfigurator cli )
{
toMap( cli.getExecutableArg(), settings );
toMap( cli.getCommandArg(), settings );
cli.getArguments().forEach( arg -> toMap( arg, settings ) );
}

Expand All @@ -52,7 +52,7 @@ public static void toTrackMateSettings( final Map< String, Object > settings, fi
*/
public static final void fromTrackMateSettings( final Map< String, Object > settings, final CLIConfigurator cli )
{
fromMap( settings, cli.getExecutableArg() );
fromMap( settings, cli.getCommandArg() );
cli.getArguments().forEach( arg -> fromMap( settings, arg ) );
}
}

0 comments on commit e265a91

Please sign in to comment.