コード例 #1
0
def route_all_controllers(app_router, plugin=None):
    """
    Called in app.routes to automatically route all controllers in the app/controllers
    folder
    """
    base_directory = os.path.abspath(
        os.path.join(
            os.path.dirname(__file__),
            '..', '..'))
    directory = os.path.join('app', 'controllers')
    if plugin:
        directory = os.path.join('plugins', plugin, 'controllers')

        # import the root plugin module first, this enables all templates and listeners
        plugin_module = __import__('plugins.%s' % plugin, fromlist=['*'])
        (plugin_module)

    directory = os.path.join(base_directory, directory)
    # the length of the path before "app/controllers"
    base_directory_path_len = len(base_directory.split(os.path.sep))
    
    # check if [base_directory]/app/controllers exists
    if not os.path.exists(directory):
        return

    # walk the app/controllers directory and sub-directories
    for root_path, _, files in os.walk(directory):
        for file in files:
            partial_path = root_path.split(os.path.sep)[base_directory_path_len:]
            if file.endswith(".py") and file != '__init__.py':
                try:
                    name = file.split('.')[0]
                    module_path = '.'.join(partial_path)
                    if plugin:
                        module_path = 'plugins.%s.controllers' % plugin
                    module = __import__('%s.%s' % (module_path, name), fromlist=['*'])

                    try:
                        cls = getattr(module, inflector.camelize(name))
                        route_controller(cls, app_router)

                    except AttributeError:
                        logging.debug("Controller %s not found, skipping" % inflector.camelize(name))

                except AttributeError as e:
                    logging.error('Thought %s was a controller, but was wrong (or ran into some weird error): %s' % (file, e))
                    raise
コード例 #2
0
ファイル: routing.py プロジェクト: ksdtech/gae-conferences
def route_all_controllers(app_router, plugin=None):
    """
    Called in app.routes to automatically route all controllers in the app/controllers
    folder
    """
    base_directory = os.path.abspath(
        os.path.join(os.path.dirname(__file__), '..', '..'))
    directory = os.path.join('app', 'controllers')
    if plugin:
        directory = os.path.join('plugins', plugin, 'controllers')

        # import the root plugin module first, this enables all templates and listeners
        plugin_module = __import__('plugins.%s' % plugin, fromlist=['*'])
        (plugin_module)

    directory = os.path.join(base_directory, directory)

    # Walk through the controllers, if the directory for controllers exists
    if not os.path.exists(directory):
        return

    for file in os.listdir(directory):
        if file.endswith(".py") and file != '__init__.py':
            try:
                name = file.split('.')[0]
                root_module = 'app.controllers'
                if plugin:
                    root_module = 'plugins.%s.controllers' % plugin
                module = __import__('%s.%s' % (root_module, name),
                                    fromlist=['*'])

                try:
                    cls = getattr(module, inflector.camelize(name))
                    route_controller(cls, app_router)

                except AttributeError:
                    logging.debug("Controller %s not found, skipping" %
                                  inflector.camelize(name))

            except AttributeError as e:
                logging.error(
                    'Thought %s was a controller, but was wrong (or ran into some weird error): %s'
                    % (file, e))
                raise
コード例 #3
0
ファイル: routing.py プロジェクト: ksdtech/gae-conferences
def route_all_controllers(app_router, plugin=None):
    """
    Called in app.routes to automatically route all controllers in the app/controllers
    folder
    """
    base_directory = os.path.abspath(
        os.path.join(
            os.path.dirname(__file__),
            '..', '..'))
    directory = os.path.join('app', 'controllers')
    if plugin:
        directory = os.path.join('plugins', plugin, 'controllers')

        # import the root plugin module first, this enables all templates and listeners
        plugin_module = __import__('plugins.%s' % plugin, fromlist=['*'])
        (plugin_module)

    directory = os.path.join(base_directory, directory)

    # Walk through the controllers, if the directory for controllers exists
    if not os.path.exists(directory):
        return

    for file in os.listdir(directory):
        if file.endswith(".py") and file != '__init__.py':
            try:
                name = file.split('.')[0]
                root_module = 'app.controllers'
                if plugin:
                    root_module = 'plugins.%s.controllers' % plugin
                module = __import__('%s.%s' % (root_module, name), fromlist=['*'])

                try:
                    cls = getattr(module, inflector.camelize(name))
                    route_controller(cls, app_router)

                except AttributeError:
                    logging.debug("Controller %s not found, skipping" % inflector.camelize(name))

            except AttributeError as e:
                logging.error('Thought %s was a controller, but was wrong (or ran into some weird error): %s' % (file, e))
                raise