示例#1
0
文件: tools.py 项目: kmalyjur/pcs
def get_cib_crm_feature_set(cib: _Element) -> Version:
    """
    Return crm_feature_set as pcs.common.tools.Version or raise LibraryError

    etree cib -- cib etree
    """
    return _get_cib_version(cib.getroottree(), "crm_feature_set",
                            re.compile(_VERSION_FORMAT))
示例#2
0
    def from_urs(
        cls,
        elt: etree._Element,
        id_getter: ty.Optional[ty.Callable[[str], ty.Optional[etree._Element]]] = None,
        fs_getter: ty.Optional[
            ty.Callable[[str], ty.Optional[FeatureStructure]]
        ] = None,
    ):
        """Build a `Mention` from an XML URS span element.

        You can pass an id getter and a feature structure getter or the document where
        they live.
        In last resort, we will try to find them in the document where `elt` resides.
        """
        if elt.tag != f"{TEI}span":
            raise ValueError(
                f"Attempting to build a mention from a {elt.tag!r} element"
            )
        document = elt.getroottree()
        if id_getter is None:
            id_store = {xmlid(elt): elt for elt in document.iter()}
            id_getter = id_store.get
        if fs_getter is None:
            fs_store = get_fs(document)
            fs_getter = fs_store.get

        targets = targets_from_span(elt, id_getter)
        elt_id = xmlid(elt)
        ana_target = elt.get(f"{TEI}ana")
        if ana_target is None:
            raise ValueError(f"Span {elt_id!r} has no `ana` attribute")
        fs = fs_getter(target_to_id(ana_target))
        if fs is None:
            raise ValueError(f"Span {elt_id!r} has no features")
        span_type = fs.get("type")
        if span_type is None:
            raise ValueError(f"Span {elt_id!r} has no `type` feature")
        elif not isinstance(span_type, str):
            raise ValueError(f"Span {elt_id!r} `type` feature is not a string")

        parents_set = set(t.getparent() for t in targets)
        if len(parents_set) > 1:
            raise ValueError(
                f"The targets of spans {elt_id!r} have more than one parent"
            )
        parent = next(iter(parents_set))
        return cls(
            identifier=elt_id,
            span_type=span_type,
            targets_parent=parent,
            speaker=parent.get(f"{TEI}who"),
            content=[t.text for t in targets],
            features=fs,
            corresp=elt,
            targets=targets,
        )
示例#3
0
文件: tools.py 项目: kmalyjur/pcs
def get_pacemaker_version_by_which_cib_was_validated(cib: _Element) -> Version:
    """
    Return version of pacemaker which validated specified cib as tree.
    Version is returned as an instance of pcs.common.tools.Version.
    Raises LibraryError on any failure.

    cib -- cib etree
    """
    return _get_cib_version(
        cib.getroottree(),
        "validate-with",
        re.compile(r"pacemaker-{0}".format(_VERSION_FORMAT)),
    )
示例#4
0
    def __init__(self, el: etree._Element):
        """
        Initializes the assertion from the given XML element.

        This base class methods mainly initializes the properties items, sources, comments, xmlsource, and details

        Args:
            el: The basic assertion element. This will usually be <relation> or <date>.
        """
        self.items: List[Reference] = [
            Witness.get(uri)
            for uri in el.xpath('f:item/@uri', namespaces=config.namespaces)
        ]
        self.sources = tuple(
            BiblSource(source.get('uri'), source.text)
            for source in el.xpath('f:source', namespaces=config.namespaces))
        self.comments = tuple(
            comment.text
            for comment in el.xpath('f:comment', namespaces=config.namespaces))
        self.xmlsource: Tuple[str, int] = (config.relative_path(
            el.getroottree().docinfo.URL), el.sourceline)
        self.ignore = el.get('ignore', 'no') == 'yes'
示例#5
0
def serialize_xml(xml: etree._Element) -> BytesIO:
    io = BytesIO()
    xml.getroottree().write(io, encoding="utf-8")
    io.seek(0)
    return io
示例#6
0
def is_root_element(element: etree._Element) -> bool:
    """ Tests whether the given element is the root of the tree object.
        Not to be mixed up with the root element of a possible sub-document a transformation may
        be called with.
    """
    return element is element.getroottree().getroot()