Ejemplo n.º 1
0
def _send_data(relative_url, obj):
    """
    Helper method that POSTs an object to URL as JSON.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @param obj:          The object to be parsed to JSON and sent.
    @type obj:           object

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("POST",
                        SERVER_URL + "/" + relative_url,
                        headers={"Accept": "application/json"},
                        data=json.dumps(obj))
    buf = resp.read()
    if resp.getStatus() >= 200 and resp.getStatus() <= 300:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Ejemplo n.º 2
0
    def writeResourceToStorage(self, resource_name, resource_def, is_specialized=False):
        """
        Store a resource definition.
        
        No return value, but raises RestxException if there is an issue.
        
        @param resource_name:  The storage name for this resource
        @type  resource_name:  string
        
        @param resource_def:   The dictionary containing the resource definition.
        @type  resource_def:   dict

        @param is_specialized: Flag indicates whether a specialized component resource should
                               be created. Those can only serve as base for other resources
                               and also carry a different file extension.
        @type  is_specialized: boolean
        
        @raise RestxException: If the resource cannot be stored.
            
        """
        if is_specialized:
            extension = PARTIAL_RESOURCE_EXTENSION
        else:
            extension = RESOURCE_EXTENSION
        try:
            buf = json.dumps(resource_def, indent=4)
            self.storeFile(resource_name + extension, buf)
        except Exception, e:
            raise RestxException("Problems storing new resource: " + str(e))
Ejemplo n.º 3
0
 def writeResourceToStorage(self, resource_name, resource_def):
     """
     Store a resource definition.
     
     No return value, but raises RestxException if there is an issue.
     
     @param resource_name: The storage name for this resource
     @type  resource_name: string
     
     @param resource_def: The dictionary containing the resource definition.
     @type  resource_def: dict
     
     @raise RestxException: If the resource cannot be stored.
         
     """
     try:
         existing_resources = ResourceStorage.gql("WHERE name = :1", resource_name)
         try:
             # Make sure we update old ones
             resource = existing_resources[0]
         except Exception, e:
             # No old ones? Make a new one.
             resource = ResourceStorage()
         resource.name = resource_name
         resource.data = json.dumps(resource_def)
         resource.put()
         return "No error"
Ejemplo n.º 4
0
    def render(self, data, top_level=False):
        """
        Render the provided data for output.
        
        @param data:        An object containing the data to be rendered.
        @param data:        object
        
        @param top_level:   Flag indicating whether this we are at the
                            top level for output (this function is called
                            recursively and therefore may not always find
                            itself at the top level). This is important for
                            some renderers, since they can insert any framing
                            elements that might be required at the top level.
                            However, for the JSON renderer this is just
                            ignored.
        @param top_level:   boolean
        
        @return:            Output buffer with completed representation.
        @rtype:             string
        
        """
        # simplejson can only handle some of the base Python datatypes.
        # Since we also have other types in the output dictionaries (URIs
        # for example), we need to provide a 'default' method, which
        # simplejson calls in case it doesn't know what to do.

        # Need to use our newly defined Url encoder, since otherwise
        # json wouldn't know how to encode a URL
        if PLATFORM == PLATFORM_GAE:
            # That doesn't seem to be supported when running in
            # GAE, though. So, in that case we first perform a very
            # manual fixup of the object, replacing all occurrances
            # of unusual types with their string representations.
            data = _recursive_type_fixer(data)
            out = json.dumps(data, sort_keys=True, indent=4)
        else:
            out = json.dumps(data, default=_default, sort_keys=True, indent=4)

        return out
Ejemplo n.º 5
0
    def render(self, data, top_level=False):
        """
        Render the provided data for output.
        
        @param data:        An object containing the data to be rendered.
        @param data:        object
        
        @param top_level:   Flag indicating whether this we are at the
                            top level for output (this function is called
                            recursively and therefore may not always find
                            itself at the top level). This is important for
                            some renderers, since they can insert any framing
                            elements that might be required at the top level.
                            However, for the JSON renderer this is just
                            ignored.
        @param top_level:   boolean
        
        @return:            Output buffer with completed representation.
        @rtype:             string
        
        """
        # simplejson can only handle some of the base Python datatypes.
        # Since we also have other types in the output dictionaries (URIs
        # for example), we need to provide a 'default' method, which
        # simplejson calls in case it doesn't know what to do.

        # Need to use our newly defined Url encoder, since otherwise
        # json wouldn't know how to encode a URL
        if PLATFORM == PLATFORM_GAE:
            # That doesn't seem to be supported when running in
            # GAE, though. So, in that case we first perform a very
            # manual fixup of the object, replacing all occurrances
            # of unusual types with their string representations.
            data = _recursive_type_fixer(data)
            out = json.dumps(data, sort_keys=True, indent=4)
        else:
            out = json.dumps(data, default=_default, sort_keys=True, indent=4)

        return out
Ejemplo n.º 6
0
 def writeResourceToStorage(self, resource_name, resource_def):
     """
     Store a resource definition.
     
     No return value, but raises RestxException if there is an issue.
     
     @param resource_name: The storage name for this resource
     @type  resource_name: string
     
     @param resource_def: The dictionary containing the resource definition.
     @type  resource_def: dict
     
     @raise RestxException: If the resource cannot be stored.
         
     """
     try:
         buf = json.dumps(resource_def, indent=4)
         self.storeFile(resource_name, buf)
     except Exception, e:
         raise RestxException("Problems storing new resource: " + str(e))
Ejemplo n.º 7
0
def _send_data(relative_url, obj):
    """
    Helper method that POSTs an object to URL as JSON.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @param obj:          The object to be parsed to JSON and sent.
    @type obj:           object

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("POST", SERVER_URL + "/" + relative_url, headers={"Accept" : "application/json"}, data=json.dumps(obj))
    buf = resp.read()
    if resp.getStatus() >= 200 and resp.getStatus() <= 300:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Ejemplo n.º 8
0
def _pretty_json(obj):
    """
    Output an object as pretty-printed JSON.

    """
    return json.dumps(obj, indent=4)
Ejemplo n.º 9
0
def _pretty_json(obj):
    """
    Output an object as pretty-printed JSON.

    """
    return json.dumps(obj, indent=4)