Commits

jeremy goecks  committed 54fcce1

Enable Tool, ToolPanelLabel, and ToolPanelSection to be dict-ified and setup tools controller for RESTful interactions.

  • Participants
  • Parent commits d018b4c

Comments (0)

Files changed (3)

File lib/galaxy/tools/__init__.py

         self.id = elem.get( "id" )
         self.version = elem.get( "version" ) or ''
         self.elems = odict()
+        
+    def to_dict( self ):
+        section_elts = []
+        for key, val in self.elems.items():
+            section_elts.append( val.to_dict() )
+        return { 'type': 'section', 'id': self.id, 'name': self.name, 'elems': section_elts }
 
 class ToolSectionLabel( object ):
     """
         self.text = elem.get( "text" )
         self.id = elem.get( "id" )
         self.version = elem.get( "version" ) or ''
+        
+    def to_dict( self ):
+        return { 'type': 'label', 'id': self.id, 'name': self.text }
 
 class DefaultToolState( object ):
     """
                     self.sa_session.add( new_data )
                     self.sa_session.flush()
         return primary_datasets
+    
+    def to_dict( self, **kwds ):
+        """ Return tool as dictionary. """
+        return  { 'type': 'tool', 'id': self.id, 'name': self.name, 
+                  'description': self.description }
 
 class DataSourceTool( Tool ):
     """

File lib/galaxy/web/api/tools.py

 class ToolsController( BaseAPIController ):
     
     @web.json
-    def index( self, trans, tool_id=None ):
+    def index( self, trans, **kwds ):
         """
-        GET: if no id, returns list of available tools; if id, returns tool details
-        POST: execute tool
+        GET /api/tools: returns a list of tools defined by parameters
+            parameters: 
+                in_panel - if true, tools are returned in panel structure, including sections and labels
         """
-        tools = []
-        for id, tool in self.app.toolbox.tools_by_id.items():
-            tools.append( { 'id': id, 'name': tool.name, 'description': tool.description } )
+        in_panel = util.string_as_bool( kwds.get( 'in_panel', 'True' ) )
+        if in_panel:
+            panel_elts = []
+            # Taken from tool_menu.mako:
+            for key, val in self.app.toolbox.tool_panel.items():
+                panel_elts.append( val.to_dict() )
+            rval = panel_elts
+        else:
+            tools = []
+            for id, tool in self.app.toolbox.tools_by_id.items():
+                tools.append( tool.to_dict() )
+            rval = tools
 
-        return tools
-        
-    @web.json
-    def panel( self, trans ):
-        """
-        Returns tool panel elements.
-        """
-        
-        panel_elts = []
-        # Taken from tool_menu.mako:
-        for key, val in self.app.toolbox.tool_panel.items():
-            if key.startswith( 'tool' ):
-                panel_elts.append( { 'type': 'tool', 'id': val.id, 'name': val.name, 'description': val.description } )
-            elif key.startswith( 'section' ):
-                section = val
-                # Include the section only if it is not empty.
-                if section.elems:
-                    section_elts = []
-                    for section_key, section_val in section.elems.items():
-                        if section_key.startswith( 'tool' ):
-                            section_elts.append( { 'type': 'tool', 'id': section_val.id, 
-                                                   'name': section_val.name, 'description': section_val.description } )
-                        elif section_key.startswith( 'label' ):
-                            section_elts.append( { 'type': 'label', 'id': section_val.id, 'name': section_val.text } )
-                    panel_elts.append( { 'type': 'section', 'id': section.id, 'name': section.name, 'elems': section_elts } )
-            elif key.startswith( 'label' ):
-                panel_elts.append( { 'type': 'label', 'id': val.id, 'name': val.text } )
-        return panel_elts
-        
-    @web.json
-    def search( self, trans, query ):
-        """
-        Returns tool ids that match the query.
-        """
-        return trans.app.toolbox_search.search( query )        
+        return rval

File static/scripts/model/tools.js

 });
 
 var ToolPanel = Backbone.Collection.extend({
-    url: "/tools/panel",
+    url: "/api/tools/panel",
     parse: function(response) {
         // Recursive function to parse tool panel elements.
         var parse_elt = function(elt_dict) {