Commits

James Taylor committed 20e1f9f

Cleaning up controller imports

Comments (0)

Files changed (13)

lib/galaxy/util/backports/__init__.py

+"""
+Modules for providing backward compatibility with future versions of Python
+""" 

lib/galaxy/util/backports/importlib/__init__.py

+"""Backport of importlib.import_module from 3.x."""
+# While not critical (and in no way guaranteed!), it would be nice to keep this
+# code compatible with Python 2.3.
+import sys
+
+def _resolve_name(name, package, level):
+    """Return the absolute name of the module to be imported."""
+    if not hasattr(package, 'rindex'):
+        raise ValueError("'package' not set to a string")
+    dot = len(package)
+    for x in xrange(level, 1, -1):
+        try:
+            dot = package.rindex('.', 0, dot)
+        except ValueError:
+            raise ValueError("attempted relative import beyond top-level "
+                              "package")
+    return "%s.%s" % (package[:dot], name)
+
+
+def import_module(name, package=None):
+    """Import a module.
+
+    The 'package' argument is required when performing a relative import. It
+    specifies the package to use as the anchor point from which to resolve the
+    relative import to an absolute import.
+
+    """
+    if name.startswith('.'):
+        if not package:
+            raise TypeError("relative imports require the 'package' argument")
+        level = 0
+        for character in name:
+            if character != '.':
+                break
+            level += 1
+        name = _resolve_name(name[level:], package, level)
+    __import__(name)
+    return sys.modules[name]
+
+    ## Note: this was copied from 
+    ## http://svn.python.org/projects/python/trunk/Lib/importlib/__init__.py
+    ## on 24 September 2012

lib/galaxy/web/buildapp.py

+"""For backward compatibility only, pulls app_factor from galaxy.webapps.main"""
+
+from galaxy.webapps.main.buildapp import app_factory

lib/galaxy/web/framework/__init__.py

 import os, sys, time, socket, random, string
 import inspect
 
-from galaxy.web.base.controller import BaseUIController
-from galaxy.web.base.controller import BaseAPIController
-from galaxy.web.base.controller import ControllerUnavailable
-
-
 pkg_resources.require( "Cheetah" )
 from Cheetah.Template import Template
 import base
 from galaxy import util
 from galaxy.exceptions import MessageException
 from galaxy.util.json import to_json_string, from_json_string
+from galaxy.util.backports.importlib import import_module
 
 pkg_resources.require( "simplejson" )
 import simplejson
         else:
             return GalaxyWebUITransaction( environ, galaxy_app, self, session_cookie )
 
-    def add_ui_controllers( self, app, package_name ):
+    def add_ui_controllers( self, package_name, app ):
         """
         Search for UI controllers in `package_name` and add 
         them to the webapp.
         """
-        package = __import__( package_name )
+        from galaxy.web.base.controller import BaseUIController
+        from galaxy.web.base.controller import ControllerUnavailable
+        package = import_module( package_name )
+        controller_dir = package.__path__[0]
+        print ">>>", controller_dir, package.__path__
+        for fname in os.listdir( controller_dir ):
+            if not( fname.startswith( "_" ) ) and fname.endswith( ".py" ):
+                name = fname[:-3]
+                module_name = package_name + "." + name
+                print package_name, name, module_name
+                try:
+                    module = import_module( module_name )
+                except ControllerUnavailable, exc:
+                    log.debug("%s could not be loaded: %s" % (module_name, str(exc)))
+                    continue
+                # Look for a controller inside the modules
+                for key in dir( module ):
+                    T = getattr( module, key )
+                    if inspect.isclass( T ) and T is not BaseUIController and issubclass( T, BaseUIController ):
+                        self.add_ui_controller( name, T( app ) )
+
+    def add_api_controllers( self, package_name, app ):
+        """
+        Search for UI controllers in `package_name` and add 
+        them to the webapp.
+        """
+        from galaxy.web.base.controller import BaseAPIController
+        from galaxy.web.base.controller import ControllerUnavailable
+        package = import_module( package_name )
         controller_dir = package.__path__[0]
         for fname in os.listdir( controller_dir ):
             if not( fname.startswith( "_" ) ) and fname.endswith( ".py" ):
                 name = fname[:-3]
                 module_name = package_name + "." + name
                 try:
-                    module = __import__( module_name )
+                    module = import_module( module_name )
                 except ControllerUnavailable, exc:
                     log.debug("%s could not be loaded: %s" % (module_name, str(exc)))
                     continue
-                for comp in module_name.split( "." )[1:]:
-                    module = getattr( module, comp )
-                # Look for a controller inside the modules
-                for key in dir( module ):
-                    T = getattr( module, key )
-                    if inspect.isclass( T ) and T is not BaseUIController and issubclass( T, BaseUIController ):
-                        self.add_ui_controller( name, T( app ) )
-
-    def add_api_controllers( self, app, package_name ):
-        """
-        Search for UI controllers in `package_name` and add 
-        them to the webapp.
-        """
-        package = __import__( package_name )
-        controller_dir = package.__path__[0]
-        for fname in os.listdir( controller_dir ):
-            if not( fname.startswith( "_" ) ) and fname.endswith( ".py" ):
-                name = fname[:-3]
-                module_name = package_name + "." + name
-                try:
-                    module = __import__( module_name )
-                except ControllerUnavailable, exc:
-                    log.debug("%s could not be loaded: %s" % (module_name, str(exc)))
-                    continue
-                for comp in module_name.split( "." )[1:]:
-                    module = getattr( module, comp )
                 for key in dir( module ):
                     T = getattr( module, key )
                     if inspect.isclass( T ) and T is not BaseAPIController and issubclass( T, BaseAPIController ):

lib/galaxy/webapps/main/__init__.py

Empty file added.

lib/galaxy/webapps/main/api/workflows.py

+from __future__ import absolute_import
+
 """
 API operations for Workflows
 """
 from galaxy.workflow.modules import module_factory
 from galaxy.jobs.actions.post import ActionBox
 from galaxy.model.item_attrs import UsesAnnotations
-from galaxy.web.controllers.workflow import attach_ordered_steps
+
+from ..controllers.workflow import attach_ordered_steps
 
 log = logging.getLogger(__name__)
 

lib/galaxy/webapps/main/buildapp.py

 import os, os.path
 import sys, warnings
 
-from inspect import isclass
-
 from paste.request import parse_formvars
 from paste.util import import_string
 from paste import httpexceptions
         log.debug( "Enabling 'error' middleware" )
     # Transaction logging (apache access.log style)
     if asbool( conf.get( 'use_translogger', True ) ):
-        from framework.middleware.translogger import TransLogger
+        from galaxy.web.framework.middleware.translogger import TransLogger
         app = TransLogger( app )
         log.debug( "Enabling 'trans logger' middleware" )
     # Config middleware just stores the paste config along with the request,

lib/galaxy/webapps/main/controllers/admin_toolshed.py

 import urllib2, tempfile
-from galaxy.web.controllers.admin import *
+from admin import *
 from galaxy.util.json import from_json_string, to_json_string
 from galaxy.util.shed_util import *
 from galaxy.tool_shed.encoding_util import *

lib/galaxy/webapps/main/controllers/external_service.py

+from __future__ import absolute_import
+
 from galaxy.web.base.controller import *
 from galaxy.web.framework.helpers import time_ago, iff, grids
 from galaxy.model.orm import *
 from galaxy import model, util
 from galaxy.web.form_builder import *
-from galaxy.web.controllers.requests_common import invalid_id_redirect
+from .requests_common import invalid_id_redirect
 import logging, os
 
 log = logging.getLogger( __name__ )

lib/galaxy/webapps/main/controllers/request_type.py

+from __future__ import absolute_import
+
 from galaxy.web.base.controller import *
 from galaxy.web.framework.helpers import time_ago, iff, grids
 from galaxy.model.orm import *
 from galaxy import model, util
 from galaxy.web.form_builder import *
-from galaxy.web.controllers.requests_common import invalid_id_redirect
+from .requests_common import invalid_id_redirect
 import logging, os
 
 log = logging.getLogger( __name__ )

lib/galaxy/webapps/main/controllers/requests.py

+from __future__ import absolute_import
+
 from galaxy.web.base.controller import *
 from galaxy.web.framework.helpers import grids
 from galaxy.model.orm import *
 from galaxy.web.form_builder import *
-from galaxy.web.controllers.requests_common import RequestsGrid
+from .requests_common import RequestsGrid
 import logging
 
 log = logging.getLogger( __name__ )

lib/galaxy/webapps/main/controllers/requests_admin.py

+from __future__ import absolute_import
+
 from galaxy.web.base.controller import *
 from galaxy.web.framework.helpers import time_ago, iff, grids
 from galaxy.model.orm import *
 from galaxy import model, util
 from galaxy.web.form_builder import *
-from galaxy.web.controllers.requests_common import RequestsGrid, invalid_id_redirect
+from .requests_common import RequestsGrid, invalid_id_redirect
 from amqplib import client_0_8 as amqp
 import logging, os, pexpect, ConfigParser
 

lib/galaxy/webapps/main/controllers/visualization.py

+from __future__ import absolute_import
+
 from galaxy import model
 from galaxy.model.item_attrs import *
 from galaxy.web.base.controller import *
 from galaxy.web.framework.helpers import time_ago, grids, iff
 from galaxy.util.sanitize_html import sanitize_html
-from galaxy.web.controllers.library import LibraryListGrid
 from galaxy.visualization.genomes import decode_dbkey
 from galaxy.visualization.genome.visual_analytics import get_dataset_job
 from galaxy.visualization.data_providers.basic import ColumnDataProvider
 
+from .library import LibraryListGrid
+
 #
 # -- Grids --
 #