Skip to content

Commit

Permalink
ttools: use all available metadata in ConfigParameter
Browse files Browse the repository at this point in the history
ConfigParameter now acquires enough metadata from the ConfigMeta object
of its ConfigKey to be a fully documented Parameter.
ConfigParameters can now be used as STILTS task parameters
for auto-generation of documentation, as long as the relevant
ConfigMeta objects are properly populated.
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent 04062ee commit 14a18ac
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
Expand Up @@ -1035,10 +1035,9 @@ private ConfigMap createConfigMap( Environment env, String suffix,
private <T> void putConfigValue( Environment env, String suffix,
final ConfigKey<T> key, ConfigMap map )
throws TaskException {
final String pbase = key.getMeta().getShortName();
ConfigParameter<T> param = new ParameterFinder<ConfigParameter<T>>() {
protected ConfigParameter<T> createParameter( String sfix ) {
return new ConfigParameter<T>( pbase + sfix, key );
return ConfigParameter.createSuffixedParameter( key, sfix );
}
}.getParameter( env, suffix );
T value = param.objectValue( env );
Expand Down
Expand Up @@ -6,6 +6,7 @@
import uk.ac.starlink.task.TaskException;
import uk.ac.starlink.ttools.plot2.config.ConfigException;
import uk.ac.starlink.ttools.plot2.config.ConfigKey;
import uk.ac.starlink.ttools.plot2.config.ConfigMeta;

/**
* Typed parameter subclass intended to get the value for a ConfigKey.
Expand All @@ -20,11 +21,13 @@ public class ConfigParameter<T> extends Parameter<T> {
/**
* Constructor.
*
* @param name parameter name
* @param key config key
* @param baseName parameter name excluding suffix
* @param layerSuffix layer suffix, may be empty
*/
public ConfigParameter( String name, ConfigKey<T> key ) {
super( name, key.getValueClass(), true );
private ConfigParameter( ConfigKey<T> key,
String baseName, String layerSuffix ) {
super( baseName + layerSuffix, key.getValueClass(), true );
key_ = key;
setStringDefault( key.valueToString( key.getDefaultValue() ) );
boolean nullPermitted;
Expand All @@ -36,6 +39,40 @@ public ConfigParameter( String name, ConfigKey<T> key ) {
nullPermitted = false;
}
setNullPermitted( nullPermitted );

ConfigMeta meta = key.getMeta();
String usage = meta.getStringUsage();
String prompt = meta.getShortDescription();
String descrip = meta.getXmlDescription();
if ( layerSuffix != null && layerSuffix.length() > 0 ) {
if ( prompt != null && prompt.length() > 0 ) {
prompt += " for layer " + layerSuffix;
}
if ( descrip != null && descrip.length() > 0 ) {
descrip = new StringBuffer()
.append( descrip )
.append( "<p>This parameter affects layer " )
.append( layerSuffix )
.append( "." )
.append( "</p>" )
.append( "\n" )
.toString();
}
}
if ( usage != null ) {
setUsage( usage );
}
setPrompt( prompt );
setDescription( descrip );
}

/**
* Constructs an un-suffixed config parameter.
*
* @param key config key
*/
public ConfigParameter( ConfigKey<T> key ) {
this( key, key.getMeta().getShortName(), "" );
}

public T stringToObject( Environment env, String stringval )
Expand All @@ -47,4 +84,23 @@ public T stringToObject( Environment env, String stringval )
throw new ParameterValueException( this, e );
}
}

@Override
public String objectToString( Environment env, T objval ) {
return key_.valueToString( objval );
}

/**
* Returns a config parameter with a given suffix.
* The name is construted from the key name followed by the suffix.
*
* @param key config key
* @param layerSuffix suffix part of name
* @return new parameter
*/
public static <T> ConfigParameter<T>
createSuffixedParameter( ConfigKey<T> key, String layerSuffix ) {
return new ConfigParameter<T>( key, key.getMeta().getShortName(),
layerSuffix );
}
}
Expand Up @@ -472,9 +472,9 @@ private static List<String> getConfigUsage( ConfigKey[] configKeys,
String suffix ) {
List<String> wordList = new ArrayList<String>();
for ( int ik = 0; ik < configKeys.length; ik++ ) {
ConfigKey key = configKeys[ ik ];
String pname = key.getMeta().getShortName() + suffix;
ConfigParameter param = new ConfigParameter( pname, key );
ConfigParameter param = ConfigParameter
.createSuffixedParameter( configKeys[ ik ],
suffix );
wordList.add( usageWord( param ) );
}
return wordList;
Expand Down
Expand Up @@ -98,9 +98,7 @@ public PlotContext getPlotContext() {
private static List<Parameter> getKeyParams( ConfigKey[] keys ) {
List<Parameter> plist = new ArrayList<Parameter>();
for ( int ik = 0; ik < keys.length; ik++ ) {
ConfigKey key = keys[ ik ];
plist.add( new ConfigParameter( key.getMeta().getShortName(),
key ) );
plist.add( new ConfigParameter( keys[ ik ] ) );
}
return plist;
}
Expand Down

0 comments on commit 14a18ac

Please sign in to comment.