def add(self, document):
        """Add a document to the list of bulk uploads"""
        if not isinstance(document, Documents):
            raise InvalidAPIRequest(
                "You can only pass documents to bulkloader")

        if document.content() is None:
            raise InvalidAPIRequest("You must specify the document content")

        target = document.uris()
        if len(target) != 1:
            raise InvalidAPIRequest("You must specify a single URI")
        else:
            target = target[0]

        self.field_count += 1
        metaname = "meta{}".format(self.field_count)
        dataname = "data{}".format(self.field_count)

        self.logger.debug("Bulk[{}] = {}".format(self.field_count, target))

        rf = RequestField(name=metaname,
                          data=document.metadata(),
                          filename=target)
        rf.make_multipart(content_disposition='attachment; category=metadata', \
                              content_type=document.metadata_content_type())
        self.fields.append(rf)

        rf = RequestField(name=dataname,
                          data=document.content(),
                          filename=target)
        rf.make_multipart(content_disposition='attachment', \
                              content_type=document.content_type())
        self.fields.append(rf)
示例#2
0
    def set_javascript(self, code):
        """Set the JavaScript code to evaluate"""
        if 'xquery' in self._config:
            raise InvalidAPIRequest(
                "Cannot specify both xquery and javascript")

        self._config['javascript'] = code
        return self
 def add_transform_params(self, tuples):
     """
     Add a group of transformation parameters. Each name/value pair must
     be provided as a tuple.
     """
     for pair in tuples:
         if type(pair) is not tuple or len(pair) != 2:
             raise InvalidAPIRequest("Not a tuple")
         self.transparams.append(pair)
     return self
示例#4
0
 def add_property(self, name, value):
     """
     Add a single property.
     """
     if isinstance(value, str) or isinstance(value, int) or isinstance(
             value, float):
         self.properties.append((name, value))
     else:
         raise InvalidAPIRequest("Property value is not a string")
     return self
示例#5
0
    def add_properties(self, tuples):
        """
        Add a group of properties.

        Each name/value pair must be provided as a tuple.
        """
        for pair in tuples:
            if type(pair) is not tuple or len(pair) != 2:
                raise InvalidAPIRequest("Not a tuple")
            self.add_property(pair[0], pair[1])
        return self
示例#6
0
    def add_permissions(self, tuples):
        """
        Add a group of permissions.

        Each name/value pair must be provided as a tuple.
        """
        for pair in tuples:
            if type(pair) is not tuple or len(pair) != 2:
                raise InvalidAPIRequest("Not a tuple")
            self.permissions.append(pair)
        return self
示例#7
0
    def set_metadata(self, metadata, meta_content_type):
        """Set arbitrary metadata.

        You must format it in XML or JSON according to the rules of the
        Client API. If you set arbitrary metadata, you cannot call any
        other methods that set metadata properties. You also cannot
        specify the forest.
        """
        self._metadata = metadata
        if meta_content_type in ["application/json", "application/xml"]:
            self._metadata_content_type = meta_content_type
        else:
            raise InvalidAPIRequest(
                "Metadata format must be 'application/json' or 'application/xml'"
            )
示例#8
0
    def put(self, data=None, uri=None, connection=None):
        """
        Perform an HTTP PUT on the document described by this object.

        If a URI is specified, it will be used irrespective of the URI setting
        in the object. If it isn't specified, the object must specify a single
        URI.

        The data must be specified. For application/json data, a Python
        dictionary may be specified.
        """
        if connection is None:
            connection = self.connection

        if data is None:
            if self._content is None:
                raise InvalidAPIRequest(
                    "Attempt to upload doc with no content")
            data = self._content

        if uri is None:
            if len(self._config['uri']) != 1:
                raise InvalidAPIRequest("Must PUT with exactly one URI")
            uri = self._config['uri'][0]

        # You must not mix individual metadata properties with arbitrary metadata
        if self._metadata is not None:
            for key in self._config:
                if key == 'category' or key == 'collection':
                    if self._config[key]:
                        raise InvalidAPIRequest(
                            "Cannot mix individual and bulk metadata1")
                elif key == 'uri':
                    if self._config[key] and len(self._config[key]) != 1:
                        raise InvalidAPIRequest(
                            "Cannot mix individual and bulk metadata4")
                elif (key == 'accept' or key == 'content-type' or key == 'database' \
                          or key == 'transform' or key == 'txid' \
                          or key == 'temporal-collection' or key == 'system-time'):
                    pass
                else:
                    raise InvalidAPIRequest(
                        "Cannot mix individual and bulk metadata2")
            if self.permissions or self.properties:
                raise InvalidAPIRequest(
                    "Cannot mix individual and bulk metadata3")
            return self._put_mixed(data, uri, connection)
        else:
            return self._put_uriparams(data, uri, connection)