Skip to content

Module manubot.cite.wikidata

View Source
from typing import Any, Dict

from .handlers import Handler

class Handler_Wikidata(Handler):

    prefixes = ["wikidata"]

    standard_prefix = "wikidata"

    accession_pattern = r"Q[0-9]+"

    def inspect(self, citekey):

        """

        https://www.wikidata.org/wiki/Wikidata:Identifiers

        """

        accession = citekey.accession

        if not accession.startswith("Q"):

            return "Wikidata item IDs must start with 'Q'."

        elif not self._get_pattern().fullmatch(accession):

            return (

                "Accession does not conform to the Wikidata regex. "

                "Double check the entity ID."

            )

    def get_csl_item(self, citekey):

        return get_wikidata_csl_item(citekey.standard_accession)

def get_wikidata_csl_item(identifier: str) -> Dict[str, Any]:

    """

    Get a CSL JSON item with the citation metadata for a Wikidata item.

    identifier should be a Wikidata item ID corresponding to a citeable

    work, such as Q50051684.

    """

    url = f"https://www.wikidata.org/wiki/{identifier}"

    from manubot.cite.url import get_url_csl_item_zotero

    csl_item = get_url_csl_item_zotero(url)

    if "DOI" in csl_item:

        csl_item["DOI"] = csl_item["DOI"].lower()

    if "URL" not in csl_item:

        csl_item["URL"] = url

    return csl_item

Functions

get_wikidata_csl_item

def get_wikidata_csl_item(
    identifier: str
) -> Dict[str, Any]

Get a CSL JSON item with the citation metadata for a Wikidata item.

identifier should be a Wikidata item ID corresponding to a citeable work, such as Q50051684.

View Source
def get_wikidata_csl_item(identifier: str) -> Dict[str, Any]:

    """

    Get a CSL JSON item with the citation metadata for a Wikidata item.

    identifier should be a Wikidata item ID corresponding to a citeable

    work, such as Q50051684.

    """

    url = f"https://www.wikidata.org/wiki/{identifier}"

    from manubot.cite.url import get_url_csl_item_zotero

    csl_item = get_url_csl_item_zotero(url)

    if "DOI" in csl_item:

        csl_item["DOI"] = csl_item["DOI"].lower()

    if "URL" not in csl_item:

        csl_item["URL"] = url

    return csl_item

Classes

Handler_Wikidata

class Handler_Wikidata(
    prefix_lower: str
)

A Handler is a class that provides support for a certain type of citekey.

For example, a Handler subclass could provide support for DOI citekeys. Subclasses enable custom logic for different citekey prefixes, including how to standardize the citekey and how to retrieve CSL Item metadata.

View Source
class Handler_Wikidata(Handler):

    prefixes = ["wikidata"]

    standard_prefix = "wikidata"

    accession_pattern = r"Q[0-9]+"

    def inspect(self, citekey):

        """

        https://www.wikidata.org/wiki/Wikidata:Identifiers

        """

        accession = citekey.accession

        if not accession.startswith("Q"):

            return "Wikidata item IDs must start with 'Q'."

        elif not self._get_pattern().fullmatch(accession):

            return (

                "Accession does not conform to the Wikidata regex. "

                "Double check the entity ID."

            )

    def get_csl_item(self, citekey):

        return get_wikidata_csl_item(citekey.standard_accession)

Ancestors (in MRO)

  • manubot.cite.handlers.Handler

Class variables

accession_pattern
prefixes
standard_prefix

Methods

get_csl_item

def get_csl_item(
    self,
    citekey
)

Return a CSL_Item with bibliographic details for citekey.

View Source
    def get_csl_item(self, citekey):

        return get_wikidata_csl_item(citekey.standard_accession)

inspect

def inspect(
    self,
    citekey
)

https://www.wikidata.org/wiki/Wikidata:Identifiers

View Source
    def inspect(self, citekey):

        """

        https://www.wikidata.org/wiki/Wikidata:Identifiers

        """

        accession = citekey.accession

        if not accession.startswith("Q"):

            return "Wikidata item IDs must start with 'Q'."

        elif not self._get_pattern().fullmatch(accession):

            return (

                "Accession does not conform to the Wikidata regex. "

                "Double check the entity ID."

            )

standardize_prefix_accession

def standardize_prefix_accession(
    self,
    accession: str
) -> Tuple[str, str]

Return (prefix, accession) in standardized form.

This method defaults to returning self.standard_prefix (or self.prefix_lower if standard_prefix is not defined). Subclasses can override this method with more specific standardization logic.

View Source
    def standardize_prefix_accession(self, accession: str) -> Tuple[str, str]:

        """

        Return (prefix, accession) in standardized form.

        This method defaults to returning `self.standard_prefix`

        (or `self.prefix_lower` if standard_prefix is not defined).

        Subclasses can override this method with more specific standardization logic.

        """

        standard_prefix = getattr(self, "standard_prefix", self.prefix_lower)

        standard_accession = accession

        return standard_prefix, standard_accession