Esempio n. 1
0
def OvfFile(f):
    schemaPath = os.path.join(os.path.dirname(__file__),
                              "schemas/ovf-envelope.xsd")
    doc = xobj.parsef(f, documentClass=OvfDocument, schemaf=schemaPath)
    ovf = doc.ovf_Envelope
    ovf._doc = doc
    return ovf
Esempio n. 2
0
    def _handle_request(self, method, uri, xdoc=None, parent=None, cache=True,
        redirectCount=0):
        """
        Process all types of requests.
        """

        # Normalize the URI.
        uri = self._normalize_uri(uri)

        # Check if this is a permanent redirect
        uri = self._redirects.get(uri, uri)

        # Check the cache before moving on if this is a GET.
        if method == 'GET' and uri in self.cache and cache:
            return self.cache[uri]

        xml = None
        rawdoc = False
        if method in ('POST', 'PUT', ):
            # Make sure there is a document for PUT and POST requests.
            if xdoc is None:
                raise TypeError, 'method requires document instance'

            xml = self._serialize_document(xdoc)
            if xml == xdoc:
                rawdoc = True
            args = (uri, xml)
        else:
            args = (uri, )

        # Allow custom http data to override method.
        if xml and httputil.isHTTPData(xml) and xml.method:
            method = xml.method

        # Call client method
        func = getattr(self._client, 'do_%s' % method)
        request = func(*args)

        # Wait for request to complete.
        request.wait()

        # Get the response
        response = request.response

        # Special case DELETE method.
        if method == 'DELETE':
            # Raise an exception if the resource could not be deleted.
            if response.status not in (404, 200, 204):
                raise HTTPDeleteError(uri=uri, status=response.status,
                    reason=response.reason, response=response)

            self.cache.clear(uri)

            return response

        # Handle other error codes.
        if response.status >= 400:
            return self._handle_error(uri, request, response)

        # Handle redirects.
        elif response.status >= 300:
            return self._handle_redirect(uri, request, response, parent=parent,
                redirectCount=redirectCount)

        # If the raw document was sent to the server, this is probably a file
        # upload and the response should not contain an xml document.
        if rawdoc:
            return response

        # Make sure the response looks like valid xml, otherwise assume that
        # this is a file download an return the content of the response.
        content = response.content
        if not util.isXML(content):
            return content

        # Parse XML document.
        doc = xobj.parsef(content)
        content.close()

        # Pull the top level object out of the document.
        assert isinstance(doc, xobj.Document)
        assert len(doc._xobj.elements) == 1
        root = getattr(doc, doc._xobj.elements[0])

        # If the top level object has an 'id' attribute, use that as its URI.
        # This is here to handle appending to collections, where the resource
        # you get back is the new instance, not the collection itself.
        if method != 'GET' and 'id' in root._xobj.attributes:
            uri = self._normalize_uri(root.id)

        # Cache response and return rObjProxy instance.
        return self.cache(self, uri, root, parent=parent, cache=cache)
Esempio n. 3
0
    def fromfile(cls, fn):
        """
        Deserialize from file.
        """

        return xobj.parsef(fn, documentClass=cls)