Dear Biostars community,
I am having problems developing a python function to return some custom information to a Galaxy tool's dynamic_options parameter function.
It seems like there is something wrong with the character encoding of the return value but I cannot resolve it. The custom function should return a unique string which including a unix timestamp.
And the generation of the unix timestamp seems to be the problem.
The tool xml looks like this (this works fine):
<inputs>
<param name="ftp_dynamic_upload_dir" type="select" label="FTP upload dir" dynamic_options="generate_ftp_download_dir( trans=__trans__ )"/>
</inputs>
Now the following function gets called correctly:
#!/usr/bin/env python                                                                                                                       
import time                                      
from pprint import pprint
def generate_ftp_download_dir(trans = None):
   if trans:                                                                                
      username = trans.user.username                            
      unix_timestamp = str(int(time.time()))                              
      upload_path = "galaxyuser-" + username + "-" + unix_timestamp            
      return_obj = []                                                       
      return_obj.append( ( upload_path, upload_path, True) )
      return return_obj        
   return []
If I load this tool in galaxy the correct value appears in the dropdown menu, e.g.
galaxyuser-oliverpelz-1468398607
Now if I submit the form I receive an error, here are the details from Firebug network tool:
POST:
{"tool_id":"my-ftp-downloader","tool_version":"0.0.1","inputs":{"ftp_dynamic_upload_dir":"galaxyuser-oliverpelz-1468398607"
}}
RESPONSE:
{"err_data": {"ftp_dynamic_upload_dir": "An invalid option was selected for ftp_dynamic_upload_dir, u'galaxyuser-oliverpelz-1468398607'
, please verify."}, "err_msg": "Unknown error occurred while processing request.", "err_code": 0}
By analyzing the response I see that the ftp_dynamic_upload_dir parameter is encoded as UTF8 (Have a look at the u'1468332679' error value. ). So if I change the generated unix_timestamp string from the str() function to a string literal in our python function there are no errors:
#!/usr/bin/env python                             
import time                                  
from pprint import pprint
def generate_ftp_download_dir(trans = None):
    if trans:                                                                           
       username = trans.user.username                       
       #unix_timestamp = str(int(time.time()))                         
       unix_timestamp = "12345"
       upload_path = "galaxyuser-" + username + "-" + unix_timestamp       
       return_obj = []                                                  
       return_obj.append( ( upload_path, upload_path, True) )
       return return_obj   
    return []
The submit's response works here:
POST:
{"tool_id":"my-ftp-downloader","tool_version":"0.0.1","inputs":{"ftp_dynamic_upload_dir":"galaxyuser-oliverpelz-12345"
}}
RESPONSE:
{"outputs": [], "implicit_collections": [], "jobs": [{"tool_id": "my-ftp-downloader", "update_time"
: "2016-07-13T08:34:19.929165", "exit_code": null, "state": "new", "create_time": "2016-07-13T08:34:19
.929151", "model_class": "Job", "id": "7f27ab690ef37b85"}], "output_collections": []}
So this seem like an encoding problem. If you compare both POST's parameter, there seem no difference in the String encoding though???
If I try to change encoding to say ascii in the return value of the function it does not change anything and the error remains.
upload_path = upload_path.encode('ascii', 'ignore')
Is there a bug or do I miss something? As I need this dynamic part of the unix timestamp for uniqueness is there any other way to solve this encoding problem?
Thank you very much for your help
Oliver
