/lib/galaxy/tools/parameters/__init__.py

https://bitbucket.org/nicste/ballaxy · Python · 96 lines · 52 code · 5 blank · 39 comment · 17 complexity · 62cc8ac7a2660db1bd8daeadfb9cb1f7 MD5 · raw file

  1. """
  2. Classes encapsulating Galaxy tool parameters.
  3. """
  4. from basic import *
  5. from grouping import *
  6. from galaxy.util.json import *
  7. def visit_input_values( inputs, input_values, callback, name_prefix="", label_prefix="" ):
  8. """
  9. Given a tools parameter definition (`inputs`) and a specific set of
  10. parameter `values`, call `callback` for each non-grouping parameter,
  11. passing the parameter object, value, a constructed unique name,
  12. and a display label.
  13. If the callback returns a value, it will be replace the old value.
  14. FIXME: There is redundancy between this and the visit_inputs methods of
  15. Repeat and Group. This tracks labels and those do not. It would
  16. be nice to unify all the places that recursively visit inputs.
  17. """
  18. for input in inputs.itervalues():
  19. if isinstance( input, Repeat ) or isinstance( input, UploadDataset ):
  20. for i, d in enumerate( input_values[ input.name ] ):
  21. index = d['__index__']
  22. new_name_prefix = name_prefix + "%s_%d|" % ( input.name, index )
  23. new_label_prefix = label_prefix + "%s %d > " % ( input.title, i + 1 )
  24. visit_input_values( input.inputs, d, callback, new_name_prefix, new_label_prefix )
  25. elif isinstance( input, Conditional ):
  26. values = input_values[ input.name ]
  27. current = values["__current_case__"]
  28. label_prefix = label_prefix
  29. new_name_prefix = name_prefix + input.name + "|"
  30. visit_input_values( input.cases[current].inputs, values, callback, new_name_prefix, label_prefix )
  31. else:
  32. new_value = callback( input,
  33. input_values[input.name],
  34. prefixed_name = name_prefix + input.name,
  35. prefixed_label = label_prefix + input.label )
  36. if new_value:
  37. input_values[input.name] = new_value
  38. def check_param( trans, param, incoming_value, param_values ):
  39. """
  40. Check the value of a single parameter `param`. The value in
  41. `incoming_value` is converted from its HTML encoding and validated.
  42. The `param_values` argument contains the processed values of
  43. previous parameters (this may actually be an ExpressionContext
  44. when dealing with grouping scenarios).
  45. """
  46. value = incoming_value
  47. error = None
  48. try:
  49. if value is not None or isinstance(param, DataToolParameter):
  50. # Convert value from HTML representation
  51. value = param.from_html( value, trans, param_values )
  52. # Allow the value to be converted if neccesary
  53. filtered_value = param.filter_value( value, trans, param_values )
  54. # Then do any further validation on the value
  55. param.validate( filtered_value, trans.history )
  56. elif value is None and isinstance( param, SelectToolParameter ):
  57. # An empty select list or column list
  58. param.validate( value, trans.history )
  59. except ValueError, e:
  60. error = str( e )
  61. return value, error
  62. def params_to_strings( params, param_values, app ):
  63. """
  64. Convert a dictionary of parameter values to a dictionary of strings
  65. suitable for persisting. The `value_to_basic` method of each parameter
  66. is called to convert its value to basic types, the result of which
  67. is then json encoded (this allowing complex nested parameters and
  68. such).
  69. """
  70. rval = dict()
  71. for key, value in param_values.iteritems():
  72. if key in params:
  73. value = params[ key ].value_to_basic( value, app )
  74. rval[ key ] = str( to_json_string( value ) )
  75. return rval
  76. def params_from_strings( params, param_values, app, ignore_errors=False ):
  77. """
  78. Convert a dictionary of strings as produced by `params_to_strings`
  79. back into parameter values (decode the json representation and then
  80. allow each parameter to convert the basic types into the parameters
  81. preferred form).
  82. """
  83. rval = dict()
  84. for key, value in param_values.iteritems():
  85. value = json_fix( from_json_string( value ) )
  86. if key in params:
  87. value = params[key].value_from_basic( value, app, ignore_errors )
  88. rval[ key ] = value
  89. return rval