Skip to content

extras

Serialize caller-supplied extension metadata into GKG XML fragments.

Citation dataclass

Citation(authors: tuple[str, ...] = (), title: str | None = None, book_title: str | None = None, date: str | None = None, journal: str | None = None, volume: str | None = None, issue: str | None = None, pages: str | None = None, institution: str | None = None, publisher: str | None = None, location: str | None = None, marker: str | None = None)

Caller-supplied reference metadata for the extras XML field.

Simple Surname, Given author names are reordered. Dates, volumes, issues, and pages retain their string values. Omitted values produce no XML element.

citation_elements

citation_elements(citations: Iterable[Citation]) -> dict[str, XMLValue]

Convert citation metadata into an extras XML element mapping.

Parameters:

Name Type Description Default
citations Iterable[Citation]

References in output order.

required

Returns:

Type Description
dict[str, XMLValue]

A cited-reference block for format_extras_xml, or an empty mapping.

Raises:

Type Description
TypeError

If an entry is not a Citation.

Source code in src/gdelt_regkg/fields/extras.py
def citation_elements(citations: Iterable[Citation]) -> dict[str, XMLValue]:
    """Convert citation metadata into an extras XML element mapping.

    Args:
        citations: References in output order.

    Returns:
        A cited-reference block for `format_extras_xml`, or an empty mapping.

    Raises:
        TypeError: If an entry is not a `Citation`.
    """
    references: list[XMLValue] = []
    for citation in citations:
        if not isinstance(citation, Citation):
            raise TypeError("citations must contain Citation instances")
        reference: dict[str, XMLValue] = {}
        if citation.authors:
            reference["AUTHORS"] = {"AUTHOR": citation.authors}
        for name, tag in _TAGS.items():
            value = getattr(citation, name)
            if value is not None:
                reference[tag] = value
        references.append(reference)
    return {"CITEDREFERENCESLIST": {"CITATION": references}} if references else {}

format_extras_xml

format_extras_xml(elements: Mapping[str, XMLValue]) -> str

Serialize extension metadata as a rootless XML fragment.

Strings become escaped text, mappings become nested elements, and sequences repeat elements. None values omit elements. Insertion order is preserved.

Parameters:

Name Type Description Default
elements Mapping[str, XMLValue]

XML tag names mapped to text, nested mappings, or sequences.

required

Returns:

Type Description
str

XML suitable for one GKG cell, or an empty string for no elements.

Raises:

Type Description
TypeError

If elements or text values have invalid types.

ValueError

If tag names or characters are invalid in XML.

Source code in src/gdelt_regkg/fields/extras.py
def format_extras_xml(elements: Mapping[str, XMLValue]) -> str:
    """Serialize extension metadata as a rootless XML fragment.

    Strings become escaped text, mappings become nested elements, and sequences
    repeat elements. `None` values omit elements. Insertion order is preserved.

    Args:
        elements: XML tag names mapped to text, nested mappings, or sequences.

    Returns:
        XML suitable for one GKG cell, or an empty string for no elements.

    Raises:
        TypeError: If elements or text values have invalid types.
        ValueError: If tag names or characters are invalid in XML.
    """
    if not isinstance(elements, Mapping):
        raise TypeError("elements must be a mapping of XML tag names to values")
    root = Element("_fragment")
    for name, value in elements.items():
        _append(root, name, value)
    return (
        "".join(tostring(node, encoding="unicode", short_empty_elements=False) for node in root)
        .replace("\t", "	")
        .replace("\r", "
")
        .replace("\n", "
")
    )