Exemplo n.º 1
0
def _getResourceDetails(resource_name):
    """
    Extract and compute a number of importants facts about a resource.
    
    The information is returned as a dictionary.
    
    @param resource_name:    The name of the resource.
    @type resource_name:     string
    
    @return:                 Dictionary with information about the resource.
    @rtype:                  dict
    
    """
    complete_resource_def  = retrieveResourceFromStorage(getResourceUri(resource_name))
    if not complete_resource_def:
        raise RestxResourceNotFoundException("Unknown resource")
    resource_home_uri      = getResourceUri(resource_name)
    public_resource_def    = complete_resource_def['public']
    
    # Instantiate the component to get the exposed sub-services. Their info
    # is added to the public information about the resource.
    code_uri  = complete_resource_def['private']['code_uri']
    component = restx.core.codebrowser.getComponentInstance(code_uri, resource_name)
    services  = component._getServices(resource_home_uri)
    services  = languageStructToPython(component, services)
    public_resource_def['services'] = services
    
    return dict(complete_resource_def = complete_resource_def,
                resource_home_uri     = resource_home_uri,
                public_resource_def   = public_resource_def,
                code_uri              = code_uri,
                component             = component)
Exemplo n.º 2
0
def _getResourceDetails(resource_name):
    """
    Extract and compute a number of importants facts about a resource.
    
    The information is returned as a dictionary.
    
    @param resource_name:    The name of the resource.
    @type resource_name:     string
    
    @return:                 Dictionary with information about the resource.
    @rtype:                  dict
    
    """
    complete_resource_def = retrieveResourceFromStorage(
        getResourceUri(resource_name))
    if not complete_resource_def:
        raise RestxResourceNotFoundException("Unknown resource")
    resource_home_uri = getResourceUri(resource_name)
    public_resource_def = complete_resource_def['public']

    # Instantiate the component to get the exposed sub-services. Their info
    # is added to the public information about the resource.
    code_uri = complete_resource_def['private']['code_uri']
    component = restx.core.codebrowser.getComponentInstance(
        code_uri, resource_name)
    services = component._getServices(resource_home_uri)
    services = languageStructToPython(component, services)
    public_resource_def['services'] = services

    return dict(complete_resource_def=complete_resource_def,
                resource_home_uri=resource_home_uri,
                public_resource_def=public_resource_def,
                code_uri=code_uri,
                component=component)
Exemplo n.º 3
0
    def __process_post(self, is_code, prefix):
        """
        Process a POST request.
        
        The only allowed POST requests to code are requests
        to the base URI of a component. This creates a new resource.
        
        Same with spezialiced code.

        @param is_code:     Indicates whether this is a request for un-specialized code.
        @type is_code:      boolean

        @param prefix:      The prefix for this type of request.
        @type prefix:       string

        @return:  HTTP return structure.
        @rtype:   Result

        """
        if is_code:
            # If we are dealing with actual components then the path of this request
            # here is the correct path to find the component class.
            component_path        = self.request.getRequestPath()
            specialized_code_name = None
            specialized_code      = None
        else:
            # But if we are dealing with specialized components then we first need to
            # retrieve the partial resource definition and extract the component path
            # from there.
            path_elems = self.request.getRequestPath()[len(prefix):].split("/")[1:]
            specialized_code_name = path_elems[0]
            specialized_code      = retrieveResourceFromStorage(getResourceUri(specialized_code_name, is_partial=True), only_public=False, is_partial=True)
            if not specialized_code:
                return Result.notFound("Cannot find specialized component resource '%s'" % specialized_code_name)
            component_path        = specialized_code["private"]["code_uri"]

        #
        # Start by processing and sanity-checking the request.
        #
        component = getComponentObjectFromPath(component_path)
        if not component:
            return Result.notFound("Unknown component")
        #component = component_class()
        body = self.request.getRequestBody()
        try:
            param_dict = json.loads(body)
        except Exception, e:
            raise RestxException("Malformed request body: " + str(e))
Exemplo n.º 4
0
    def __process_get(self, is_code, prefix):
        """
        Respond to GET requests.
        
        When someone sends GET requests to the code then
        they want to browse the available code options.

        Same with spezialiced code.

        @param is_code:     Indicates whether this is a request for un-specialized code.
        @type is_code:      boolean

        @param prefix:      The prefix for this type of request.
        @type prefix:       string
        
        @return:  HTTP return structure.
        @rtype:   Result

        """
        # It's the responsibility of the browser class to provide breadcrumbs
        if is_code:
            dirname = "Code"
        else:
            dirname = "Specialized"
        self.breadcrumbs = [ ("Home", "/"), (dirname, prefix) ]

        if self.request.getRequestPath() == prefix:
            #
            # Just show the home page of the code browser (list of all installed (specialized) code)
            #
            if is_code:
                # Data to be taken from the code
                data = dict()
                for name in get_component_names():
                    if name[0] not in EXCLUDE_PREFIXES:
                        # component_info is a tuple, which contains the component class and its manifest info
                        component = make_component(name)
                        data[name] = { "uri" : Url(component.getCodeUri()), "desc" : component.getDesc() }
                """
                data = dict([ (name, { "uri" : Url(component_class().getCodeUri()), "desc" : component_class().getDesc() } ) \
                                    for (name, (component_class, component_config)) in get_code_map().items() \
                                        if name[0] not in EXCLUDE_PREFIXES ])
                """
            else:
                # We are looking for partial resources
                data = listResources(partials=True)
        else:
            # Path elements (the known code prefix is stripped off)
            path_elems = self.request.getRequestPath()[len(prefix):].split("/")[1:]
            if is_code:
                # We are referencing actual components here
                component_name  = path_elems[0]   # This should be the name of the code element
                component_path  = self.request.getRequestPath()
            else:
                # We are looking at a partial resource. Therefore, we need to load
                # that resource and then get the code URI from it.
                specialized_code_name = path_elems[0]
                specialized_code      = retrieveResourceFromStorage(getResourceUri(specialized_code_name, is_partial=True), only_public=False, is_partial=True)
                if not specialized_code:
                    return Result.notFound("Cannot find specialized component resource '%s'" % specialized_code_name)
                component_path        = specialized_code["private"]["code_uri"]
            
            # Instantiate the component
            component = getComponentObjectFromPath(component_path)
            if not component:
                return Result.notFound("Unknown component")
            component_home_uri = component.getCodeUri()

            if is_code:
                self.breadcrumbs.append((component_name, component_home_uri))
            else:
                self.breadcrumbs.append((specialized_code_name, specialized_code["public"]["uri"]))

            if len(path_elems) == 1:
                #
                # No sub-detail specified: We want meta info about a code segment (component)
                #
                data = component.getMetaData()

                #
                # If this is based on a specialized component then we need to overwrite some
                # of the component's meta data with the info from the specialized component
                # definition.
                #
                if not is_code:
                    data = specializedOverwrite(data, specialized_code)

                data = languageStructToPython(component, data)
                if is_code:
                    qs = ""
                    cname = component_name
                else:
                    qs = "?specialized=y"
                    cname = specialized_code_name
                self.context_header.append(("[ Create resource ]", settings.PREFIX_RESOURCE+"/_createResourceForm/form/"+cname+qs, ""))  #, "target=_blank"))
            else:
                #
                # Some sub-detail of the requested component was requested
                #
                sub_name = path_elems[1]
                if sub_name == "doc":
                    data       = component.getDocs()
                    self.breadcrumbs.append(("Doc", component_home_uri + "/doc"))
                else:
                    return Result.notFound("Unknown code detail")
                
        return Result.ok(data)