Commits

Joel Rivera committed 7dd6c60

Reorganization of a bunch of code related to the modifications
for GO, also all the skeletons for the GO catalog browser.

Comments (0)

Files changed (14)

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

     DestEndpoint
 )
 from .aws import AWSCredentials
-# Currently the oauth modules is not exposed.
+from .oauth import OAuthAccessToken
+from .catalog import Catalog
 
-__all__ = ['SourceEndpoint', 'SourceEndpointPath',
-           'DestEndpoint', 'AWSCredentials']
+__all__ = ['SourceEndpoint', 'SourceEndpointPath', 'DestEndpoint',
+           'AWSCredentials', 'OAuthAccessToken', 'Catalog']
     
 
     

lib/galaxy/tools/parameters/globus/aws.py

         This field is meant to be uses only trough javascript.
         """
         return ""
-        
     def get_html_field(self, trans=None, value=None, other_values=None):
         return AWSCredentialVerifier(trans, self.name)
 

lib/galaxy/tools/parameters/globus/catalog.py

+import json
+import logging
+
+from galaxy.web import form_builder
+from galaxy.tools.parameters.basic import ToolParameter
+from ._utils import HTMLUtils, RSRS_BASE_URL
+
+
+class CatalogBrowser(form_builder.HiddenField):
+
+    _ask_credentials_modal = """\
+    <style>
+        #askpasswd label {
+           font-weight: bold;
+         }
+         #askpasswd tr > td:first-child {
+            text-align: right;
+          }
+      </style>  
+      <div class="modal" id="askpasswd">
+         <p class="promsg"><!-- placeholder for prompt message --> </p>
+         <table border="0">
+           <tr>
+             <td><label for="gouser">Username:</label></td> 
+             <td><input type="text" value="" id="gouser"  name="gouser"/></td>
+           </tr>
+            <tr>
+             <td><label for="gopasswd">Password:</label></td>
+             <td><input type="password" value="" id="gopasswd" name="gopasswd" /> </td>
+           </tr>
+            <tr>
+              <td></td>  <td><input type="submit" value="Catalog login" /></td>
+            </tr>           
+          </table>
+         <p class="rspmsg"><!-- placeholder for the reponse message --></p>
+      </div>    
+    """
+
+
+
+    def get_html(self, prefix=''):
+        html_parts = []
+        hidden_field = super(CatalogBrowser, self).get_html(prefix)
+        append = html_parts.append
+        append(hidden_field)
+        append(HTMLUtils.jquery_modal_css_and_js())
+        append(self._ask_credentials_modal)
+        append('<script src="%s/catalog.js"> </script>' % RSRS_BASE_URL)
+        return '\n'.join(html_parts)
+        
+
+class Catalog(ToolParameter):
+
+    def get_label(self):
+        """
+        This field is meant to be uses only trough javascript.
+        """
+        return ""
+
+
+    def get_html_field(self, trans=None, value=None, other_values=None):
+        return CatalogBrowser(self.name)
+        

lib/galaxy/tools/parameters/globus/oauth.py

 from galaxy.web import form_builder
 from galaxy.tools.parameters.basic import ToolParameter
 
+from galaxy.web.framework.ext.globus import storage
+
         
 def get_basespace_auth_url(trans):
     config = trans.app.config
         self.provider = param.get('provider')
         self.scope = param.get('scope', None) # TODO implement scope
 
-
     def _has_access_token(self, trans):
         # TODO: add validation of the expires field
-        OAuthToken = trans.app.model.OAuthToken
-        atoken = (trans.sa_session.query(OAuthToken)
-                  .filter_by(user_id=trans.user.id,
-                             provider=self.provider).scalar())
-        return atoken is not None
-
+        try:
+            storage.get(trans, 'oauth:%s' % self.provider)
+        except KeyError:
+            return False
+        else:
+            return True
         
     def get_html_field(self, trans, value=None, other_values=None):
         if self._has_access_token(trans):

lib/galaxy/web/framework/__init__.py

             if not( fname.startswith( "_" ) ) and fname.endswith( ".py" ):
                 name = fname[:-3]
                 module_name = package_name + "." + name
+                #DEBUG
+                print module_name
                 try:
                     module = import_module( module_name )
                 except ControllerUnavailable, exc:

lib/galaxy/web/framework/ext/globus/__init__.py

-from . import middleware
+from . import (
+    middleware,
+    storage
+)
 
-__all__ = ['middleware', ]
+__all__ = ['middleware', 'storage']

lib/galaxy/web/framework/ext/globus/storage.py

+"""
+Convenient functions for the Galaxy Storage.
+"""
+
+
+def _get_storage(trans):
+    try:
+        storage = trans.environ['globus.storage']
+    except KeyError:
+        raise Exception('There is no in-memory storage for the current '
+                        'transaction, make sure that the globus middleware '
+                        'is in place.')
+    else:
+        return storage
+
+def get(trans, key):
+    storage = _get_storage(trans)
+    return storage.fetch(trans.get_cookie(), key)
+
+def set(trans, key, value):
+    storage = _get_storage(trans)
+    return storage.store(trans.get_cookie(), key, value)
+
+def pop(trans, key):
+    storage = _get_storage(trans)
+    return storage.pop(trans.get_cookie(), key)
+    
+    

lib/galaxy/webapps/globusonline/__init__.py

-from  galaxy.web.framework.ext import globus
-
-#from galaxy.webapps.globusonline import extoauth
+from galaxy.web.framework.ext import globus
 
 def add_controllers(webapp, app, kwargs):
     if globus.middleware.is_goauth_enabled(kwargs):
-        webapp.add_ui_controllers('galaxy.webapps.globusonline.transfer_api', app)
-        webapp.add_route('/globusonline/transfer_api/browser/:action', controller='gobrowser')
+        _add_epbrowser(webapp, app)
+    _add_s3_browser(webapp, app)
+    _add_catalog(webapp, app)
+    _add_oauth(webapp, app)
+
+def _add_catalog(webapp, app):
+    webapp.add_ui_controllers('galaxy.webapps.globusonline', app)
+    webapp.add_route('/globusonline/catalog/browser/:action',
+                     controller='catalogbrw')
+
+def _add_epbrowser(webapp, app):
+    webapp.add_ui_controllers('galaxy.webapps.globusonline.transfer_api', app)
+    webapp.add_route('/globusonline/transfer_api/browser/:action',
+                     controller='gobrowser')
+
+def _add_oauth(webapp, app):
+    from galaxy.webapps.globusonline import extoauth
+    webapp.add_ui_controller('oauth_register', extoauth.OAuthRegister(app))
+    webapp.add_route('/globusonline/extoauth/:provider/:action',
+                     controller='oauth_register')
+
+def _add_s3_browser(webapp, app):
     webapp.add_ui_controllers('galaxy.webapps.globusonline.s3', app)
-    webapp.add_route('/globusonline/s3/browser/:action', controller='s3browser')
-    # currently the OAuth controllers are disabled.
-    #webapp.add_ui_controller('oauth_register', extoauth.OAuthRegister(app))
-    #webapp.add_route('/globusonline/extoauth/:provider/:action', controller='oauth_register')
+    webapp.add_route('/globusonline/s3/browser/:action',
+                     controller='s3browser')
+

lib/galaxy/webapps/globusonline/catalogbrw.py

+import logging
+
+from galaxy import web
+from galaxy.web.base.controller import BaseUIController
+
+log = logging.getLogger(__name__)
+
+
+class CatalogAPI(object):
+    DEFAULT_SERVER = 'graph.api.test.globuscs.info'
+    AUTH_URL = '/goauth/token?grant_type=client_credentials'
+
+
+    def __init__(self, catalog, user, passwd, catalog_id=1, server=None,
+                 validate_certs=True):
+        self.user = user
+        self.passwd = passwd
+        self.catalog_id = catalog_id
+        self.goauth = None
+        self.validate_certs = validate_certs
+        if server is None:
+            self.server = self.DEFAULT_SERVER
+        self.server_url = 'https://{0}{1}'.format(self.server, self.AUTH_URL)
+
+    def _login(self):
+        pass
+
+    def login(self):
+        if self.goauth is None:
+            self._login()
+
+    def list(self):
+        pass
+
+
+class CatalogBrowser(BaseUIController):
+
+    @web.expose_api
+    def credentials(self, trans, token):
+        return {'go_user': 'leuser', 
+                'go_token': 'letoken',
+                'bs_access': 'basespace access'}
+
+    
+    @web.expose_api
+    def authenticate(self, trans, gouser, gopasswd, payload=None):
+        return "Authenticate"
+
+
+    @web.expose_api
+    def get_dataset(self, trans):
+        pass
+
+    @web.expose_api        
+    def list_datasets(self, query=None):
+        pass
+  
+     

lib/galaxy/webapps/globusonline/extoauth.py

 from galaxy import web
 from galaxy.web.base.controller import BaseUIController
 
+from galaxy.web.framework.ext.globus import storage
+
 
 def gen_basespace_atoken(code):
     return "dummy token"
 
 
-
 class OAuthRegister(BaseUIController):
     PROVIDERS = {'basespace': gen_basespace_atoken}
 
         if handler is None:
             raise HTTPNotFound('The provider "%s" does not exists.' % provider)
         else:
-            OAuthToken = self.app.model.OAuthToken
             access_token = handler(code)
-            ctoken = (trans.sa_session.query(OAuthToken)
-                      .filter_by(user_id=trans.user.id,
-                                 provider=provider).scalar())
-            if ctoken is not None:
-                ctoken.access_code = access_token
-            else:
-                oatoken = OAuthToken(trans.user.id, provider, access_token)
-                trans.sa_session.add(oatoken)
-            trans.sa_session.flush()
+            storage.set(trans, 'oauth:%s' % provider, access_token)
             return "Registered"
 

lib/galaxy/webapps/globusonline/s3/s3browser.py

 from galaxy import web
 from galaxy.web.base.controller import BaseUIController
 
+from galaxy.web.framework.ext.globus import storage 
+
 log = logging.getLogger(__name__)
 
 
     DELIMITER = '/'
 
     def _get_credentials(self, trans):
-        galaxy_cookie = trans.get_cookie()
-        storage = trans.environ['globus.storage']
-        return storage.fetch(galaxy_cookie, self.STORAGE_KEY)
+        return storage.get(trans, self.STORAGE_KEY)
 
     def _valid_credentials(self, key, secret):
         try:
     def credentials(self, trans, token=None):
         if token is None:
             return None
-        storage = trans.environ['globus.storage']
         key = 'AWS_TOKEN_%s' % token
         try:
-            if storage.pop(trans.get_cookie(), key):
+            if storage.pop(trans, key):
                 return self._get_credentials(trans)
         except KeyError:
             return None
     @web.expose_api
     def set_credentials(self, trans, aws_keyid, aws_secret, payload):
         if self._valid_credentials(aws_keyid, aws_secret):
-            galaxy_cookie = trans.get_cookie()
-            storage = trans.environ['globus.storage']
-            storage.store(galaxy_cookie, self.STORAGE_KEY, 
-                          {'aws_access_key_id': aws_keyid,
-                           'aws_secret_access_key': aws_secret})
+            storage.set(trans, self.STORAGE_KEY, 
+                        {'aws_access_key_id': aws_keyid,
+                         'aws_secret_access_key': aws_secret})
             return True
         return False
 
     @web.expose_api
     def gen_token(self, trans):
         token = uuid.uuid4().hex
-        galaxy_cookie = trans.get_cookie()
-        storage = trans.environ['globus.storage']
-        storage.store(galaxy_cookie,
-                      'AWS_TOKEN_%s' % token, True)
+        storage.set(trans, 'AWS_TOKEN_%s' % token, True)
         return {'token': token}
 
    

static/globus/catalog.js

+(function($, window, globalwin, undefined) {
+
+
+    function inform_of_current_credentials() {
+
+    }
+
+    function show_ask_for_credentials () {
+
+    }
+    
+    function _have_credentials(cb) {
+
+    }
+
+    function _hide_exec(){
+	$('input[name=runtool_btn]').hide()
+    }
+
+    function _show_exec(){
+	$('input[name=runtool_btn]').show()
+    }
+
+    function check_credentials () {
+	$.get('/globus/catalog/credentials',
+	      function (resp){
+		  if (resp !== null) {
+		      inform_of_current_credentials(resp);
+		  } else {
+		      show_ask_for_credentials();
+		  }
+	      });
+    }
+
+    function login(){
+	
+    }
+
+    function setup() {
+	check_credentials();
+
+    }
+
+    $(document).ready(setup);
+})($, this, window);

tools/globus/catalog.xml

+<tool id="globus_catalog" name="Catalog"
+      tool_type="globus_datasource" >
+   <description>Transfer a single file from illumina</description>
+   <command interpreter="python">
+   </command>
+   <inputs>
+<!--     <param name="atoken" type="oauth_token" provider="basespace" /> -->
+     <param name="query" type="text" label="Query" size="25"/>
+     <param name="path" type="globus_catalog" /> 
+   </inputs>
+   <outputs>
+     <data name="outfile" type="data" format="txt" />
+   </outputs>
+   <help>
+     Browse and fetch datasets from the GlobusOnline catalog.
+   </help>
+</tool>

tools/globus/transfer_from_catalog.py

+import urllib
+from argparse import ArgumentParser
+import requests
+
+
+def parse_cli():
+    description = 'Fetch a dataset from the Globus Online Catalog'
+    parser = ArgumentParser(description=description)
+    parser.add_argument('--session', required=True)
+    parser.add_argument('--token', required=True)
+    parser.add_argument('--host', required=True)
+    parser.add_argument('--ext', required=False)
+    parser.add_argument('catalog', nargs=1)
+    parser.add_argument('dataset_ref', nargs=1)
+    parser.add_argument('output_path', nargs=1)
+    return parser.parse_args()
+
+
+def get_real_path(output_path, ext):
+    if not ext:
+        return output_path
+    else:
+        if output_path.endswith('.data'):
+            return output_path[:-4] + ext
+        else:
+            return output_path
+
+
+def fetch_credentials(host, session, token):
+    url = urllib.basejoin(host, '/globusonline/catalog/browser/credentials')
+    response = requests.get(url, params={'token': token},
+                            cookies={'galaxysession': session})
+    if response.ok:
+        return {k: v.encode() for k, v in response.json.items()}
+    else:
+        raise Exception('Unable to fetch the GO Catalog credentials.')
+
+
+def make_transfer(gouser, gotoken, bs_access, catalog, dataset_ref, output_path):
+    print "Making transfer with", gouser, gotoken, catalog
+
+
+def main():
+    args = parse_cli()
+    catalog = args.catalog[0]
+    dataset_ref = args.dataset_ref[0]
+    output_path = args.output_path[0]
+    cred = fetch_credentials(args.host, args.session, args.token)
+    make_transfer(cred['go_user'], cred['go_token'], cred['bs_access'],
+                  catalog, dataset_ref, get_real_path(output_path, args.ext))
+
+
+if __name__ == '__main__':
+    main()