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