예제 #1
0
def test_xml_set_text(load_inpxml):

    from masci_tools.util.xml.common_functions import eval_xpath
    from masci_tools.util.xml.xml_setters_xpaths import xml_set_text

    xmltree, schema_dict = load_inpxml(TEST_INPXML_PATH)
    root = xmltree.getroot()

    xml_set_text(xmltree, schema_dict, '/fleurInput/comment', '/fleurInput/comment', 'TEST_COMMENT')

    assert str(eval_xpath(root, '/fleurInput/comment/text()')) == 'TEST_COMMENT'
예제 #2
0
def test_xml_set_text_create(load_inpxml):

    from masci_tools.util.xml.common_functions import eval_xpath
    from masci_tools.util.xml.xml_setters_xpaths import xml_set_text

    xmltree, schema_dict = load_inpxml(TEST_INPXML_PATH)
    root = xmltree.getroot()

    with pytest.raises(ValueError, match='Could not set text on path'):
        xml_set_text(xmltree, schema_dict,
                     "/fleurInput/atomSpecies/species[@name='Fe-1']/torgueCalculation/greensfElements/s",
                     '/fleurInput/atomSpecies/species/torgueCalculation/greensfElements/s', [False, False, True, False])

    xml_set_text(xmltree,
                 schema_dict,
                 "/fleurInput/atomSpecies/species[@name='Fe-1']/torgueCalculation/greensfElements/s",
                 '/fleurInput/atomSpecies/species/torgueCalculation/greensfElements/s', [False, False, True, False],
                 create=True)

    assert eval_xpath(root, '/fleurInput/atomSpecies/species/torgueCalculation/greensfElements/s/text()') == 'F F T F'
예제 #3
0
def test_xml_set_text_differing_xpaths(load_inpxml):

    from masci_tools.util.xml.common_functions import eval_xpath
    from masci_tools.util.xml.xml_setters_xpaths import xml_set_text

    xmltree, schema_dict = load_inpxml(TEST_INPXML_PATH)
    root = xmltree.getroot()

    kpoints_xpath = '/fleurInput/cell/bzIntegration/kPointLists/kPointList/kPoint'

    assert eval_xpath(root, f'{kpoints_xpath}/text()') == [
        '   -0.250000     0.250000     0.000000', '    0.250000     0.250000     0.000000'
    ]

    xmltree = xml_set_text(xmltree, schema_dict, f'{kpoints_xpath}[2]', kpoints_xpath, ['1.0', '1.0', '1.0'])

    assert eval_xpath(root, f'{kpoints_xpath}/text()') == ['   -0.250000     0.250000     0.000000', '1.0 1.0 1.0']
예제 #4
0
def test_xml_set_text_all(load_inpxml, text, expected_result, occurrences):

    from masci_tools.util.xml.common_functions import eval_xpath
    from masci_tools.util.xml.xml_setters_xpaths import xml_set_text

    xmltree, schema_dict = load_inpxml(TEST_INPXML_PATH)
    root = xmltree.getroot()

    kpoints_xpath = '/fleurInput/cell/bzIntegration/kPointLists/kPointList/kPoint'

    assert eval_xpath(root, f'{kpoints_xpath}/text()') == [
        '   -0.250000     0.250000     0.000000', '    0.250000     0.250000     0.000000'
    ]

    xmltree = xml_set_text(xmltree, schema_dict, kpoints_xpath, kpoints_xpath, text, occurrences=occurrences)

    assert eval_xpath(root, f'{kpoints_xpath}/text()') == expected_result
예제 #5
0
def set_text(xmltree, schema_dict, tag_name, text, complex_xpath=None, occurrences=None, create=False, **kwargs):
    """
    Sets the text on tags in a xmltree to a given value, specified by the name of the tag and
    further specifications. By default the text will be set on all nodes returned for the specified xpath.
    If there are no nodes under the specified xpath a tag can be created with `create=True`.
    The text values are converted automatically according to the types
    with :py:func:`~masci_tools.util.xml.converters.convert_text_to_xml()` if they
    are not `str` already.

    :param xmltree: an xmltree that represents inp.xml
    :param schema_dict: InputSchemaDict containing all information about the structure of the input
    :param tag_name: str name of the tag, where the text should be set
    :param text: value or list of values to set
    :param complex_xpath: an optional xpath to use instead of the simple xpath for the evaluation
    :param occurrences: int or list of int. Which occurence of the node to set. By default all are set.
    :param create: bool optional (default False), if True the tag is created if is missing

    Kwargs:
        :param contains: str, this string has to be in the final path
        :param not_contains: str, this string has to NOT be in the final path

    :returns: xmltree with set text
    """
    from masci_tools.util.xml.xml_setters_xpaths import xml_set_text

    base_xpath = get_tag_xpath(schema_dict, tag_name, **kwargs)

    if complex_xpath is None:
        complex_xpath = base_xpath

    xmltree = xml_set_text(xmltree,
                           schema_dict,
                           complex_xpath,
                           base_xpath,
                           text,
                           occurrences=occurrences,
                           create=create)

    return xmltree