[2] | 1 | #Contains parameters that are used in Display Applications |
---|
| 2 | from galaxy.util import string_as_bool |
---|
| 3 | from galaxy.util.bunch import Bunch |
---|
| 4 | from galaxy.util.template import fill_template |
---|
| 5 | from galaxy.web import url_for |
---|
| 6 | import mimetypes |
---|
| 7 | |
---|
| 8 | DEFAULT_DATASET_NAME = 'dataset' |
---|
| 9 | |
---|
| 10 | class DisplayApplicationParameter( object ): |
---|
| 11 | """ Abstract Class for Display Application Parameters """ |
---|
| 12 | |
---|
| 13 | type = None |
---|
| 14 | |
---|
| 15 | @classmethod |
---|
| 16 | def from_elem( cls, elem, link ): |
---|
| 17 | param_type = elem.get( 'type', None ) |
---|
| 18 | assert param_type, 'DisplayApplicationParameter requires a type' |
---|
| 19 | return parameter_type_to_class[ param_type ]( elem, link ) |
---|
| 20 | def __init__( self, elem, link ): |
---|
| 21 | self.name = elem.get( 'name', None ) |
---|
| 22 | assert self.name, 'DisplayApplicationParameter requires a name' |
---|
| 23 | self.link = link |
---|
| 24 | self.url = elem.get( 'url', self.name ) #name used in url for display purposes defaults to name; e.g. want the form of file.ext, where a '.' is not allowed as python variable name/keyword |
---|
| 25 | self.mime_type = elem.get( 'mimetype', None ) |
---|
| 26 | self.guess_mime_type = string_as_bool( elem.get( 'guess_mimetype', 'False' ) ) |
---|
| 27 | self.viewable = string_as_bool( elem.get( 'viewable', 'False' ) ) #only allow these to be viewed via direct url when explicitly set to viewable |
---|
| 28 | self.strip = string_as_bool( elem.get( 'strip', 'False' ) ) |
---|
| 29 | self.strip_https = string_as_bool( elem.get( 'strip_https', 'False' ) ) |
---|
| 30 | def get_value( self, other_values, dataset_hash, user_hash, trans ): |
---|
| 31 | raise Exception, 'Unimplemented' |
---|
| 32 | def prepare( self, other_values, dataset_hash, user_hash, trans ): |
---|
| 33 | return self.get_value( other_values, dataset_hash, user_hash, trans ) |
---|
| 34 | def ready( self, other_values ): |
---|
| 35 | return True |
---|
| 36 | def is_preparing( self, other_values ): |
---|
| 37 | return False |
---|
| 38 | def build_url( self, other_values ): |
---|
| 39 | return fill_template( self.url, context = other_values ) |
---|
| 40 | |
---|
| 41 | class DisplayApplicationDataParameter( DisplayApplicationParameter ): |
---|
| 42 | """ Parameter that returns a file_name containing the requested content """ |
---|
| 43 | |
---|
| 44 | type = 'data' |
---|
| 45 | |
---|
| 46 | def __init__( self, elem, link ): |
---|
| 47 | DisplayApplicationParameter.__init__( self, elem, link ) |
---|
| 48 | self.extensions = elem.get( 'format', None ) |
---|
| 49 | if self.extensions: |
---|
| 50 | self.extensions = self.extensions.split( "," ) |
---|
| 51 | self.metadata = elem.get( 'metadata', None ) |
---|
| 52 | self.dataset = elem.get( 'dataset', DEFAULT_DATASET_NAME ) # 'dataset' is default name assigned to dataset to be displayed |
---|
| 53 | assert not ( self.extensions and self.metadata ), 'A format or a metadata can be defined for a DisplayApplicationParameter, but not both.' |
---|
| 54 | self.viewable = string_as_bool( elem.get( 'viewable', 'True' ) ) #data params should be viewable |
---|
| 55 | self.force_url_param = string_as_bool( elem.get( 'force_url_param', 'False' ) ) |
---|
| 56 | self.force_conversion = string_as_bool( elem.get( 'force_conversion', 'False' ) ) |
---|
| 57 | @property |
---|
| 58 | def formats( self ): |
---|
| 59 | if self.extensions: |
---|
| 60 | return tuple( map( type, map( self.link.display_application.datatypes_registry.get_datatype_by_extension, self.extensions ) ) ) |
---|
| 61 | return None |
---|
| 62 | def _get_dataset_like_object( self, other_values ): |
---|
| 63 | #this returned object has file_name, state, and states attributes equivalent to a DatasetAssociation |
---|
| 64 | data = other_values.get( self.dataset, None ) |
---|
| 65 | assert data, 'Base dataset could not be found in values provided to DisplayApplicationDataParameter' |
---|
| 66 | if isinstance( data, DisplayDataValueWrapper ): |
---|
| 67 | data = data.value |
---|
| 68 | if self.metadata: |
---|
| 69 | rval = getattr( data.metadata, self.metadata, None ) |
---|
| 70 | assert rval, 'Unknown metadata name (%s) provided for dataset type (%s).' % ( self.metadata, data.datatype.__class__.name ) |
---|
| 71 | return Bunch( file_name = rval.file_name, state = data.state, states = data.states, extension='data' ) |
---|
| 72 | elif self.extensions and ( self.force_conversion or not isinstance( data.datatype, self.formats ) ): |
---|
| 73 | for ext in self.extensions: |
---|
| 74 | rval = data.get_converted_files_by_type( ext ) |
---|
| 75 | if rval: |
---|
| 76 | return rval |
---|
| 77 | assert data.find_conversion_destination( self.formats )[0] is not None, "No conversion path found for data param: %s" % self.name |
---|
| 78 | return None |
---|
| 79 | return data |
---|
| 80 | def get_value( self, other_values, dataset_hash, user_hash, trans ): |
---|
| 81 | data = self._get_dataset_like_object( other_values ) |
---|
| 82 | if data: |
---|
| 83 | return DisplayDataValueWrapper( data, self, other_values, dataset_hash, user_hash, trans ) |
---|
| 84 | return None |
---|
| 85 | def prepare( self, other_values, dataset_hash, user_hash, trans ): |
---|
| 86 | data = self._get_dataset_like_object( other_values ) |
---|
| 87 | if not data and self.formats: |
---|
| 88 | data = other_values.get( self.dataset, None ) |
---|
| 89 | trans.sa_session.refresh( data ) |
---|
| 90 | #start conversion |
---|
| 91 | #FIXME: Much of this is copied (more than once...); should be some abstract method elsewhere called from here |
---|
| 92 | #find target ext |
---|
| 93 | target_ext, converted_dataset = data.find_conversion_destination( self.formats, converter_safe = True ) |
---|
| 94 | if target_ext and not converted_dataset: |
---|
| 95 | assoc = trans.app.model.ImplicitlyConvertedDatasetAssociation( parent = data, file_type = target_ext, metadata_safe = False ) |
---|
| 96 | new_data = data.datatype.convert_dataset( trans, data, target_ext, return_output = True, visible = False ).values()[0] |
---|
| 97 | new_data.hid = data.hid |
---|
| 98 | new_data.name = data.name |
---|
| 99 | trans.sa_session.add( new_data ) |
---|
| 100 | trans.sa_session.flush() |
---|
| 101 | assoc.dataset = new_data |
---|
| 102 | trans.sa_session.add( assoc ) |
---|
| 103 | trans.sa_session.flush() |
---|
| 104 | elif converted_dataset and converted_dataset.state == converted_dataset.states.ERROR: |
---|
| 105 | raise Exception, "Dataset conversion failed for data parameter: %s" % self.name |
---|
| 106 | return self.get_value( other_values, dataset_hash, user_hash, trans ) |
---|
| 107 | def is_preparing( self, other_values ): |
---|
| 108 | value = self._get_dataset_like_object( other_values ) |
---|
| 109 | if value and value.state in ( value.states.NEW, value.states.UPLOAD, value.states.QUEUED, value.states.RUNNING ): |
---|
| 110 | return True |
---|
| 111 | return False |
---|
| 112 | def ready( self, other_values ): |
---|
| 113 | value = self._get_dataset_like_object( other_values ) |
---|
| 114 | if value: |
---|
| 115 | if value.state == value.states.OK: |
---|
| 116 | return True |
---|
| 117 | elif value.state == value.states.ERROR: |
---|
| 118 | raise Exception( 'A data display parameter is in the error state: %s' % ( self.name ) ) |
---|
| 119 | return False |
---|
| 120 | |
---|
| 121 | class DisplayApplicationTemplateParameter( DisplayApplicationParameter ): |
---|
| 122 | """ Parameter that returns a string containing the requested content """ |
---|
| 123 | |
---|
| 124 | type = 'template' |
---|
| 125 | |
---|
| 126 | def __init__( self, elem, link ): |
---|
| 127 | DisplayApplicationParameter.__init__( self, elem, link ) |
---|
| 128 | self.text = elem.text |
---|
| 129 | def get_value( self, other_values, dataset_hash, user_hash, trans ): |
---|
| 130 | value = fill_template( self.text, context = other_values ) |
---|
| 131 | if self.strip: |
---|
| 132 | value = value.strip() |
---|
| 133 | return DisplayParameterValueWrapper( value, self, other_values, dataset_hash, user_hash, trans ) |
---|
| 134 | |
---|
| 135 | parameter_type_to_class = { DisplayApplicationDataParameter.type:DisplayApplicationDataParameter, DisplayApplicationTemplateParameter.type:DisplayApplicationTemplateParameter } |
---|
| 136 | |
---|
| 137 | class DisplayParameterValueWrapper( object ): |
---|
| 138 | ACTION_NAME = 'param' |
---|
| 139 | def __init__( self, value, parameter, other_values, dataset_hash, user_hash, trans ): |
---|
| 140 | self.value = value |
---|
| 141 | self.parameter = parameter |
---|
| 142 | self.other_values = other_values |
---|
| 143 | self.trans = trans |
---|
| 144 | self._dataset_hash = dataset_hash |
---|
| 145 | self._user_hash = user_hash |
---|
| 146 | self._url = self.parameter.build_url( self.other_values ) |
---|
| 147 | def __str__( self ): |
---|
| 148 | return str( self.value ) |
---|
| 149 | def mime_type( self ): |
---|
| 150 | if self.parameter.mime_type is not None: |
---|
| 151 | return self.parameter.mime_type |
---|
| 152 | if self.parameter.guess_mime_type: |
---|
| 153 | mime, encoding = mimetypes.guess_type( self._url ) |
---|
| 154 | if not mime: |
---|
| 155 | mime = self.trans.app.datatypes_registry.get_mimetype_by_extension( ".".split( self._url )[ -1 ], None ) |
---|
| 156 | if mime: |
---|
| 157 | return mime |
---|
| 158 | return 'text/plain' |
---|
| 159 | @property |
---|
| 160 | def url( self ): |
---|
| 161 | base_url = self.trans.request.base |
---|
| 162 | if self.parameter.strip_https and base_url[ : 5].lower() == 'https': |
---|
| 163 | base_url = "http%s" % base_url[ 5: ] |
---|
| 164 | return "%s%s" % ( base_url, url_for( controller = '/dataset', action = "display_application", dataset_id = self._dataset_hash, user_id = self._user_hash, app_name = self.parameter.link.display_application.id, link_name = self.parameter.link.id, app_action = self.action_name, action_param = self._url ) ) |
---|
| 165 | @property |
---|
| 166 | def action_name( self ): |
---|
| 167 | return self.ACTION_NAME |
---|
| 168 | @property |
---|
| 169 | def qp( self ): |
---|
| 170 | #returns quoted str contents |
---|
| 171 | return self.other_values[ 'qp' ]( str( self ) ) |
---|
| 172 | def __getattr__( self, key ): |
---|
| 173 | return getattr( self.value, key ) |
---|
| 174 | |
---|
| 175 | class DisplayDataValueWrapper( DisplayParameterValueWrapper ): |
---|
| 176 | ACTION_NAME = 'data' |
---|
| 177 | def __str__( self ): |
---|
| 178 | #string of data param is filename |
---|
| 179 | return str( self.value.file_name ) |
---|
| 180 | def mime_type( self ): |
---|
| 181 | if self.parameter.mime_type is not None: |
---|
| 182 | return self.parameter.mime_type |
---|
| 183 | if self.parameter.guess_mime_type: |
---|
| 184 | mime, encoding = mimetypes.guess_type( self._url ) |
---|
| 185 | if not mime: |
---|
| 186 | mime = self.trans.app.datatypes_registry.get_mimetype_by_extension( ".".split( self._url )[ -1 ], None ) |
---|
| 187 | if mime: |
---|
| 188 | return mime |
---|
| 189 | return self.other_values[ DEFAULT_DATASET_NAME ].get_mime() |
---|
| 190 | @property |
---|
| 191 | def action_name( self ): |
---|
| 192 | if self.parameter.force_url_param: |
---|
| 193 | return super( DisplayParameterValueWrapper, self ).action_name |
---|
| 194 | return self.ACTION_NAME |
---|
| 195 | @property |
---|
| 196 | def qp( self ): |
---|
| 197 | #returns quoted url contents |
---|
| 198 | return self.other_values[ 'qp' ]( self.url ) |
---|