def setup_config(self, command, filename, section, vars):
        """
        Called to setup an application, given its configuration
        file/directory.

        The default implementation calls
        ``package.websetup.setup_config(command, filename, section,
        vars)`` or ``package.websetup.setup_app(command, config,
        vars)``

        With ``setup_app`` the ``config`` object is a dictionary with
        the extra attributes ``global_conf``, ``local_conf`` and
        ``filename``
        """
        modules = [
            line.strip()
            for line in self.dist.get_metadata_lines("top_level.txt")
            if line.strip() and not line.strip().startswith("#")
        ]
        if not modules:
            print "No modules are listed in top_level.txt"
            print "Try running python setup.py egg_info to regenerate that file"
        for mod_name in modules:
            mod_name = mod_name + ".websetup"
            mod = import_string.try_import_module(mod_name)
            if mod is None:
                continue
            if command.verbose:
                print "Running setup_config() from %s" % mod_name
            if hasattr(mod, "setup_app"):
                self._call_setup_app(mod.setup_app, command, filename, section, vars)
            elif hasattr(mod, "setup_config"):
                mod.setup_config(command, filename, section, vars)
            else:
                print "No setup_app() or setup_config() function in %s (%s)" % (mod.__name__, mod.__file__)
Exemplo n.º 2
0
def resource_handler(uri, request):
    """View to handle mapped resources."""

    LOG.debug(u'Resolving resource %s', uri)
    (protocol, resource) = uri.split(':')

    if protocol == 'url':
        #redirect to given URL
        raise httpexc.HTTPFound(location=uri)

    response = None
    if protocol == 'call':
        #get handler and call it with current request as parameter
        (module_path, view_name) = resource.split('#')
        module = try_import_module(module_path)
        view = getattr(module, view_name, None)
        if view:
            response = view(request)
    elif protocol == 'egg':
        #create a response with an iterator on resource file
        (app_name, file_path) = resource.split('#')
        resource_dir = get_resource_dir(app_name)
        if resource_dir:
            full_file_path = os.sep.join([resource_dir, file_path])
            if os.path.isfile(full_file_path):
                response = create_file_response(full_file_path)
            else:
                LOG.error(u'Invalid resource path: %s', full_file_path)
    else:
        LOG.error(u'Unknown resource URI: %s', uri)

    if not response:
        raise httpexc.HTTPNotFound()

    return response
Exemplo n.º 3
0
    def setup_config(self, command, filename, section, vars):
        """
        Called to setup an application, given its configuration
        file/directory.

        The default implementation calls
        ``package.websetup.setup_config(command, filename, section,
        vars)`` or ``package.websetup.setup_app(command, config,
        vars)``

        With ``setup_app`` the ``config`` object is a dictionary with
        the extra attributes ``global_conf``, ``local_conf`` and
        ``filename``
        """
        modules = [
            line.strip()
            for line in self.dist.get_metadata_lines('top_level.txt')
            if line.strip() and not line.strip().startswith('#')
        ]
        if not modules:
            print('No modules are listed in top_level.txt')
            print(
                'Try running python setup.py egg_info to regenerate that file')
        for mod_name in modules:
            mod_name = mod_name + '.websetup'
            mod = import_string.try_import_module(mod_name)
            if mod is None:
                continue
            if hasattr(mod, 'setup_app'):
                if command.verbose:
                    print('Running setup_app() from %s' % mod_name)
                self._call_setup_app(mod.setup_app, command, filename, section,
                                     vars)
            elif hasattr(mod, 'setup_config'):
                if command.verbose:
                    print('Running setup_config() from %s' % mod_name)
                mod.setup_config(command, filename, section, vars)
            else:
                print('No setup_app() or setup_config() function in %s (%s)' %
                      (mod.__name__, mod.__file__))
Exemplo n.º 4
0
    def resolve_view_handler(self, environ):
        """Get view handler for current request"""

        LOG.debug(u'Resolving view handler')
        view = None
        #tidy URL before processing
        url = environ['PATH_INFO'].strip(u'/')
        url = url.lower()

        #check if its a resource
        if url in self.resource_mapping:
            resource_uri = self.resource_mapping[url]

            return resource.get_view_handler(resource_uri)

        try:
            (app, module_path, view_name) = self.get_view_module_parts(url)
        except URLParseError:
            LOG.error(u'Unable get view handler for URL /%s', url)

            raise httpexc.HTTPNotFound()

        module_path = '%s.view.%s' % (app, module_path)
        LOG.debug(u'Request view: %s.%s()', module_path, view_name)

        #import view module and get handler
        module = try_import_module(module_path)
        view = getattr(module, view_name, None)
        #check that current view definition is part of module (is not imported)
        if not view or (getattr(view, '__module__', None) != module.__name__):
            #when handler is not found raise HTTP 404
            raise httpexc.HTTPNotFound()

        #store current application name inside environment
        environ['duende.application'] = app

        return view