Skip to content

xml_utils

knime2py.xml_utils

parse_settings_xml(node_dir)

Parses the settings.xml file to extract the node name and factory.

Returns:

Type Description
Optional[str]

A tuple containing the node name and factory. If not found, returns (None, None).

Optional[str]

Accepts a node directory or a direct settings.xml path.

Parameters:

Name Type Description Default
node_dir Path

The directory containing the settings.xml file or the path to the settings.xml file.

required
Source code in src/knime2py/xml_utils.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def parse_settings_xml(node_dir: Path) -> Tuple[Optional[str], Optional[str]]:
    """
    Parses the settings.xml file to extract the node name and factory.

    Returns:
        A tuple containing the node name and factory. If not found, returns (None, None).
        Accepts a node directory or a direct settings.xml path.

    Args:
        node_dir: The directory containing the settings.xml file or the path to the settings.xml file.

    """
    settings = node_dir / "settings.xml"
    if not settings.exists():
        if node_dir.name.endswith(".xml") and node_dir.exists():
            settings = node_dir
        else:
            return (None, None)

    root = ET.parse(str(settings), parser=XML_PARSER).getroot()

    # name candidates
    name_vals = root.xpath(
        ".//*[local-name()='entry' and (@key='name' or @key='label' or @key='node_name')]/@value"
    )
    # factory candidates
    fac_vals = root.xpath(
        ".//*[local-name()='entry' and (@key='factory' or @key='node_factory')]/@value"
    )

    return (name_vals[0] if name_vals else None, fac_vals[0] if fac_vals else None)