コード例 #1
0
def test_convert_cals2formex(input_name, expected_name, cals_prefix, cals_ns,
                             tmpdir):
    # type: (str, str, str, str, py.path.local) -> None
    src_xml = RESOURCES_DIR.join(input_name)  # type: py.path.local
    dst_xml = tmpdir.join(src_xml.basename)
    convert_cals2formex(str(src_xml),
                        str(dst_xml),
                        width_unit="mm",
                        use_cals=True,
                        cals_prefix=cals_prefix,
                        cals_ns=cals_ns)

    # - Compare with expected
    xml_parser = etree.XMLParser(remove_blank_text=True)
    expected_xml = RESOURCES_DIR.join(expected_name)  # type: py.path.local
    if expected_xml.exists():
        expected_tree = etree.parse(str(expected_xml), parser=xml_parser)
        expected_elements = expected_tree.xpath("//TBL")
        dst_tree = etree.parse(str(dst_xml), parser=xml_parser)
        dst_elements = dst_tree.xpath("//TBL")
        assert len(expected_elements) == len(dst_elements)

        for dst_elem, expected_elem in zip(dst_elements, expected_elements):
            diff_list = xmldiff.main.diff_trees(dst_elem, expected_elem)
            assert not diff_list
    else:
        # missing resource: create it
        shutil.copy(str(dst_xml), str(expected_xml))
        print(
            "You should check and commit the file: '{}'".format(expected_xml))
コード例 #2
0
def test_convert_formex2cals(input_name, expected_name, embed_gr_notes,
                             tmpdir):
    # type: (str, str, bool, py.path.local) -> None
    src_xml = RESOURCES_DIR.join(input_name)  # type: py.path.local
    dst_xml = tmpdir.join(src_xml.basename)
    convert_formex2cals(
        str(src_xml),
        str(dst_xml),
        width_unit='mm',
        cals_prefix="cals",
        cals_ns="https://lib.benker.com/schemas/cals.xsd",
        embed_gr_notes=embed_gr_notes,
    )

    # - Compare with expected
    xml_parser = etree.XMLParser(remove_blank_text=True)
    expected_xml = RESOURCES_DIR.join(expected_name)  # type: py.path.local
    if expected_xml.exists():
        expected_tree = etree.parse(str(expected_xml), parser=xml_parser)
        NS = {"cals": "https://lib.benker.com/schemas/cals.xsd"}
        expected_elements = expected_tree.xpath("//cals:table", namespaces=NS)
        dst_tree = etree.parse(str(dst_xml), parser=xml_parser)
        dst_elements = dst_tree.xpath("//cals:table", namespaces=NS)
        assert len(expected_elements) == len(dst_elements)

        for dst_elem, expected_elem in zip(dst_elements, expected_elements):
            diff_list = xmldiff.main.diff_trees(dst_elem, expected_elem)
            assert not diff_list
    else:
        # missing resource: create it
        shutil.copy(str(dst_xml), str(expected_xml))
        print(
            "You should check and commit the file: '{}'".format(expected_xml))
コード例 #3
0
def test_convert_ooxml2cals__demo(tmpdir):
    # type: (py.path.local) -> None

    # - Unzip the ``.docx``
    src_zip = RESOURCES_DIR.join("ooxml/demo.docx")  # type: py.path.local
    with zipfile.ZipFile(str(src_zip)) as zf:
        zf.extractall(str(tmpdir))

    # - Source and destination paths
    src_xml = tmpdir.join("word/document.xml")  # type: py.path.local
    styles_xml = tmpdir.join("word/styles.xml")  # type: py.path.local
    dst_xml = tmpdir.join(src_zip.basename).new(
        ext='.cals.xml')  # type: py.path.local

    # - Create some options and convert tables
    options = {
        'encoding': 'utf-8',
        'styles_path': str(styles_xml),
        'width_unit': "mm",
        'table_in_tgroup': True
    }
    convert_ooxml2cals(str(src_xml), str(dst_xml), **options)

    # - Compare with expected
    expected_xml = RESOURCES_DIR.join(
        "ooxml2cals/demo.xml")  # type: py.path.local
    CalsComparator().compare_files(str(dst_xml), str(expected_xml))
コード例 #4
0
def test_convert_ooxml2cals(input_name, expected_name, tmpdir):
    # type: (str, str, py.path.local) -> None
    src_xml = RESOURCES_DIR.join(input_name)  # type: py.path.local
    dst_xml = tmpdir.join(src_xml.basename)
    convert_ooxml2cals(str(src_xml), str(dst_xml), width_unit='pt')

    # - Compare with expected
    xml_parser = etree.XMLParser(remove_blank_text=True)
    expected_xml = RESOURCES_DIR.join(expected_name)  # type: py.path.local
    expected_tree = etree.parse(str(expected_xml), parser=xml_parser)
    expected_elements = expected_tree.xpath("//table")
    dst_tree = etree.parse(str(dst_xml), parser=xml_parser)
    dst_elements = dst_tree.xpath("//table")
    assert len(expected_elements) == len(dst_elements)

    for dst_elem, expected_elem in zip(dst_elements, expected_elements):
        diff_list = xmldiff.main.diff_trees(dst_elem, expected_elem)
        assert not diff_list
コード例 #5
0
def test_convert_ooxml2formex4__detect_titles(input_name, expected_name,
                                              tmpdir):
    # type: (str, str, py.path.local) -> None
    src_xml = RESOURCES_DIR.join(input_name)  # type: py.path.local
    dst_xml = tmpdir.join(src_xml.basename)
    convert_ooxml2formex(str(src_xml),
                         str(dst_xml),
                         width_unit="pt",
                         detect_titles=True)
    _check_expected(dst_xml, expected_name)
コード例 #6
0
def _check_expected(dst_xml, expected_name):
    # - Compare with expected
    xml_parser = etree.XMLParser(remove_blank_text=True)
    expected_xml = RESOURCES_DIR.join(expected_name)  # type: py.path.local
    expected_tree = etree.parse(str(expected_xml), parser=xml_parser)
    expected_elements = expected_tree.xpath("//TBL")
    dst_tree = etree.parse(str(dst_xml), parser=xml_parser)
    dst_elements = dst_tree.xpath("//TBL")
    assert len(expected_elements) == len(dst_elements)
    for dst_elem, expected_elem in zip(dst_elements, expected_elements):
        diff_list = xmldiff.main.diff_trees(dst_elem, expected_elem)
        assert not diff_list
コード例 #7
0
def test_convert_ooxml2formex__demo(tmpdir):
    # type: (py.path.local) -> None

    # - Unzip the ``.docx``
    src_zip = RESOURCES_DIR.join("ooxml/demo.docx")  # type: py.path.local
    with zipfile.ZipFile(str(src_zip)) as zf:
        zf.extractall(str(tmpdir))

    # - Source and destination paths
    src_xml = tmpdir.join("word/document.xml")  # type: py.path.local
    styles_xml = tmpdir.join("word/styles.xml")  # type: py.path.local
    dst_xml = tmpdir.join(src_zip.basename).new(
        ext=".formex.xml")  # type: py.path.local

    # - Create some options and convert tables
    options = {"encoding": "utf-8", "styles_path": str(styles_xml)}
    convert_ooxml2formex(str(src_xml), str(dst_xml), **options)
    expected_name = "ooxml2formex/demo.xml"
    _check_expected(dst_xml, expected_name)