예제 #1
0
    def set(self, description, language=None):
        """Update the entity's description in the specified language (or the entity's default)

        :param description: Description to replace the current one with
        :type description: str
        :param language: Language to update the description for
        :type language: str
        """
        if not language:
            language = self.language

        try:
            r = self.api.description.set(self.item_id, description, language)
            self.descriptions[language] = r["entity"]["descriptions"][language]["value"]
        except ApiError as e:
            r_dict = json.loads(str(e))
            if (
                "messages" in r_dict
                and r_dict["messages"][0]["name"]
                == "wikibase-validator-label-with-description-conflict"
            ):
                raise DuplicateError(
                    "Another entity with the same label and description already exists"
                ) from None
            else:
                raise EditError(f"Could not update description: {e}") from None
예제 #2
0
 def set_no_value(self):
     try:
         self.api.reference.update(
             self.claim_id, self.property.entity_id, self.reference_id, None, snak_type="novalue"
         )
     except ApiError as e:
         raise EditError(f"Could not update reference value: {e}") from None
예제 #3
0
    def remove(self, reference):
        """Delete the provided reference

        :param reference: Reference to delete
        :type reference: Reference
        :return: self
        :rtype: References
        """
        check_reference_param(reference)

        # Delete reference using API
        try:
            self.api.reference.remove(reference.claim_id, reference.reference_id)
        except ApiError as e:
            raise EditError(f"Could not remove reference: {e}") from None

        # Remove reference from local collection
        prop_id = reference.property.entity_id
        self.references[prop_id] = [
            c for c in self.references[prop_id] if not c.reference_id == reference.reference_id
        ]
        if len(self.references[prop_id]) == 0:
            del self.references[prop_id]

        return self
예제 #4
0
    def _create(self, content):
        """Create a new entity with the specified label and content

        :param content: Content of the new entity
        :type content: dict
        :return: self
        :rtype: Entity
        """
        # Create entity
        try:
            r = self.api.entity.add(self.entity_type, content)
        except ApiError as e:
            raise EditError(
                f"Could not create {self.entity_type}: {e}") from None
        entity = r["entity"]

        # Save entity_id and label
        self.entity_id = entity["id"]
        self.label = self.py_wb.Label().unmarshal(self.entity_id,
                                                  entity["labels"])

        # Save empty attributes
        self.description = self.py_wb.Description().unmarshal(
            self.entity_id, {})
        self.aliases = self.py_wb.Aliases().unmarshal(self.entity_id, {})
        self.claims = self.py_wb.Claims().unmarshal(self.entity_id, {})

        return self
예제 #5
0
    def _create(self, prop, value, snak_type):
        """Create the reference using the Wikibase API and save it in the local collection

        :param prop: Property of the new reference
        :type prop: Property
        :param value: Value of the new reference
        :type value: Value
        :param snak_type: Value type (one of ``["value", "novalue", "somevalue"]``)
        :type snak_type: str
        :return: New reference
        :rtype: Reference
        """
        # Create reference using API
        try:
            if value:
                value_class = value.__class__.__name__
                value_marshalled = {
                    "type": class_to_data_type[value_class],
                    "value": value.marshal(),
                }
                r = self.api.reference.add(
                    self.claim_id, prop.entity_id, value_marshalled, snak_type=snak_type
                )
            else:
                r = self.api.reference.add(self.claim_id, prop.entity_id, None, snak_type=snak_type)
        except ApiError as e:
            raise EditError(f"Could not create reference: {e}") from None

        # Save reference in local collection
        new_reference = self.py_wb.Reference().unmarshal(self.claim_id, r["reference"])
        self._add_locally(new_reference)
        return new_reference
예제 #6
0
    def _create(self, prop, value, snak_type):
        """Create the claim using the Wikibase API and save it in the local collection

        :param prop: Property of the new claim
        :type prop: Property
        :param value: Value of the new claim
        :type value: Value
        :param snak_type: Value type (one of ``["value", "novalue", "somevalue"]``)
        :type snak_type: str
        :return: New claim
        :rtype: Claim
        """
        # Create claim using API
        try:
            if value:
                r = self.api.claim.add(self.item_id,
                                       prop.entity_id,
                                       value.marshal(),
                                       snak_type=snak_type)
            else:
                r = self.api.claim.add(self.item_id,
                                       prop.entity_id,
                                       None,
                                       snak_type=snak_type)
        except ApiError as e:
            raise EditError(f"Could not create claim: {e}") from None

        # Save claim in local collection
        new_claim = self.py_wb.Claim().unmarshal(self.item_id, r["claim"])
        self._add_locally(new_claim)
        return new_claim
예제 #7
0
    def remove(self, claim):
        """Delete the provided claim

        :param claim: Claim to delete
        :type claim: Claim
        :return: self
        :rtype: Claims
        """
        check_claim_param(claim)

        # Delete claim using API
        try:
            self.api.claim.remove(claim.claim_id)
        except ApiError as e:
            raise EditError(f"Could not remove claim: {e}") from None

        # Remove claim from local collection
        prop_id = claim.property.entity_id
        self.claims[prop_id] = [
            c for c in self.claims[prop_id] if not c.claim_id == claim.claim_id
        ]
        if len(self.claims[prop_id]) == 0:
            del self.claims[prop_id]

        return self
예제 #8
0
 def set_value(self, value):
     check_data_type(value, self.property)
     try:
         self.api.claim.update(self.claim_id,
                               value.marshal(),
                               snak_type="value")
     except ApiError as e:
         raise EditError(f"Could not update claim value: {e}") from None
예제 #9
0
 def delete(self):
     """Delete the entity from Wikibase"""
     if self.entity_type == "item":
         title = "Item:" + self.entity_id
     else:
         title = "Property:" + self.entity_id
     try:
         self.api.entity.remove(title)
     except ApiError as e:
         raise EditError(
             f"Could not delete {self.entity_type}: {e}") from None
예제 #10
0
 def set_value(self, value):
     check_data_type(value, self.property)
     try:
         self.api.reference.update(
             self.claim_id,
             self.property.entity_id,
             self.reference_id,
             value.marshal(),
             snak_type="value",
         )
     except ApiError as e:
         raise EditError(f"Could not update reference value: {e}") from None
예제 #11
0
    def remove(self, alias, language=None):
        """Remove the provided alias in the specified language (or the entity's default)

        :param alias: Alias to remove
        :type alias: str
        :param language: Language of the alias to remove
        :type language: str
        """
        if not language:
            language = self.language

        try:
            self.api.alias.remove(self.item_id, alias, language)
            self.aliases[language].remove(alias)
        except ApiError as e:
            raise EditError(f"Could not remove alias: {e}") from None
예제 #12
0
    def add(self, alias, language=None):
        """Add a new alias in the specified language (or the entity's default)

        :param alias: Alias to add
        :type alias: str
        :param language: Language of the alias to add
        :type language: str
        """
        if not language:
            language = self.language

        try:
            r = self.api.alias.add(self.item_id, alias, language)
            aliases = r["entity"]["aliases"]
            for lang, alias_list in aliases.items():
                self.aliases[lang] = [
                    alias_item["value"] for alias_item in alias_list
                ]
        except ApiError as e:
            raise EditError(f"Could not add alias: {e}") from None
예제 #13
0
 def set_some_value(self):
     try:
         self.api.claim.update(self.claim_id, None, snak_type="somevalue")
     except ApiError as e:
         raise EditError(f"Could not update claim value: {e}") from None