示例#1
0
    def test_remove_soft_breaks_without_tail(self):
        template_xml = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_page_break_without_tail.odt')
        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.remove_soft_breaks()
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0

        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.render(
            data={
                "items": [
                    {
                        'Amount': 3,
                        'Currency': 'D'
                    },
                    {
                        'Amount': 2,
                        'Currency': 'E'
                    },
                    {
                        'Amount': 1,
                        'Currency': 'C'
                    },
                ]
            })
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0
示例#2
0
    def test_remove_soft_breaks_without_tail(self):
        template_xml = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_page_break_without_tail.odt"
        )
        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.remove_soft_breaks()
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0

        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.render(
            data={
                "items": [
                    {"Amount": 3, "Currency": "D"},
                    {"Amount": 2, "Currency": "E"},
                    {"Amount": 1, "Currency": "C"},
                ]
            }
        )
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0
示例#3
0
    def test_escape_false_template(self):
        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/test_false_value.odt"
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.render({"false_value": False})
        outodt = zipfile.ZipFile(outname, "r")

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0]))
        )

        result_a = lxml.etree.tostring(content_list, pretty_print=True).decode(
            "utf-8"
        )

        result_e = open(
            pkg_resources.resource_filename(
                "py3o.template",
                "tests/templates/template_test_false_value_result.xml",
            )
        ).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        self.assertEqual(result_a, result_e)

        outname = get_secure_filename()

        template = Template(template_name, outname, escape_false=True)
        template.render({"false_value": False})
        outodt = zipfile.ZipFile(outname, "r")

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0]))
        )

        result_a = lxml.etree.tostring(content_list, pretty_print=True).decode(
            "utf-8"
        )

        result_e = open(
            pkg_resources.resource_filename(
                "py3o.template",
                "tests/templates/template_test_escape_false_value_result.xml",
            )
        ).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        self.assertEqual(result_a, result_e)
示例#4
0
    def test_render_apply_style(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_style1_template.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.set_image_path('logo', pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/images/new_logo.png'
        ))

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = 12345.35
        item1.InvoiceRef = '#1234'

        items.append(item1)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(items=items, document=document)
        template.render(data)

        # reuse the template engine just to open our odt file content...
        tempout = get_secure_filename()
        t2 = Template(outname, tempout)
        os.unlink(tempout)

        result_content = t2.content_trees[0]
        expr = "//text:p[contains(text(), 'Invoice')]"
        paragraphs = result_content.xpath(
            expr,
            namespaces=t2.namespaces
        )
        assert len(paragraphs) == 1, "Only one paragraph should have been found"
        p = paragraphs[0]
        result_text = p.text
        print(result_text)
        assert result_text == "Invoice #1234 for a total of 12345,35 EUR"
示例#5
0
    def test_variable_type_checking(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template', 'tests/templates/py3o_ods_variable_type.ods')

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data_dict = {
            'items': [
                Mock(val1=10, val2='toto'),
                Mock(val1=50.12, val2='titi'),
            ]
        }

        template.render(data_dict)
        outodt = zipfile.ZipFile(outname, 'r')

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0])))

        result_a = lxml.etree.tostring(
            content_list,
            pretty_print=True,
        ).decode('utf-8')

        result_e = open(
            pkg_resources.resource_filename(
                'py3o.template',
                'tests/templates/py3o_ods_variable_type_result.xml')).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        assert result_a == result_e
示例#6
0
    def test_missing_opening(self):
        """test orphaned /for raises a TemplateException"""
        template_name = pkg_resources.resource_filename(
            'py3o.template', 'tests/templates/py3o_missing_open_template.odt')
        outname = get_secure_filename()
        try:
            template = Template(template_name, outname)

        finally:
            os.remove(outname)

        class Item(object):
            def __init__(self, val):
                self.val = val

        data_dict = {"items": [Item(1), Item(2), Item(3), Item(4)]}

        template.set_image_path(
            'logo',
            pkg_resources.resource_filename(
                'py3o.template', 'tests/templates/images/new_logo.png'))
        # this will raise a TemplateException... or the test will fail
        error_occured = False
        try:
            template.render(data_dict)

        except TemplateException as e:
            error_occured = True
            # make sure this is the correct TemplateException that pops
            assert e.message == "No open instruction for /for"

        # and make sure we raised
        assert error_occured is True
示例#7
0
    def test_convertor(self):
        source_odt_filename = pkg_resources.resource_filename(
            'py3o.template', 'tests/templates/py3o_if_parser.odt')
        outfilename = get_secure_filename()

        template = Template(source_odt_filename, outfilename)

        user_vars = template.get_user_variables()
        expressions = template.get_all_user_python_expression()
        py_expression = template.convert_py3o_to_python_ast(expressions)
        convertor = Py3oConvertor()
        data_struct = convertor(py_expression)

        assert 'objects' in data_struct
        objs = data_struct['objects']
        assert 'company_label' in objs
        assert 'name' in objs
        assert isinstance(objs['company_label'], Py3oName)
        assert isinstance(objs['name'], Py3oName)
        #         assert data_struct == Py3oModule(
        #             {
        #                 'objects': Py3oArray(
        #                             {
        #                                 'company_label': Py3oName({}),
        #                                 'name': Py3oName({})
        #                             }
        #                 )
        #             }
        #         )
        expected_vars = [
            'registration.name',
            'registration.company_label',
        ]
        assert set(user_vars) == set(expected_vars)
示例#8
0
    def test_input_fields_with_control(self):
        template_name = pkg_resources.resource_filename(
            "py3o.template",
            "tests/templates/py3o_template_for_loop_input_field.odt",
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data_dict = {"items": ["one", "two", "three"]}

        template.render(data_dict)
        outodt = zipfile.ZipFile(outname, "r")

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0]))
        )

        result_a = lxml.etree.tostring(content_list, pretty_print=True).decode(
            "utf-8"
        )

        result_e = open(
            pkg_resources.resource_filename(
                "py3o.template",
                "tests/templates/input_fields_for_loop_result.xml",
            )
        ).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        assert result_a == result_e
示例#9
0
    def test_get_user_instruction_mapping(self):
        """test the jsonified result of get_user_instruction_mapping"""
        source_odt_filename = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )
        outfilename = get_secure_filename()

        template = Template(source_odt_filename, outfilename)
        for_lists, vars = template.get_user_instructions_mapping()
        expected_res = {
            'document': {
                'total': 0
            },
            'items': [{
                'val1': 1,
                'val2': 2,
                'val3': 3,
                'Amount': 4,
                'Currency': 5,
                'InvoiceRef': 6,
            }]
        }
        data = {
            'document': Mock(total=0),
            'items': [
                Mock(
                    val1=1, val2=2, val3=3, Amount=4, Currency=5, InvoiceRef=6
                )
            ]
        }
        res = ForList.to_dict(for_lists, vars, data)
        assert res == expected_res
示例#10
0
    def test_ignore_undefined_variables_image_injection(self):
        """Test ignore undefined variables for injected image"""

        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_image_injection.odt"
        )

        outname = get_secure_filename()

        data = {"items": [], "document": Mock(total=6)}

        template = Template(template_name, outname)
        error = True
        try:
            template.render(data)
            print(
                "Error: template contains variables that must be " "replaced"
            )
        except TemplateError:
            error = False

        self.assertFalse(error)

        template = Template(
            template_name, outname, ignore_undefined_variables=True
        )
        error = False
        try:
            template.render(data)
        except Exception:
            traceback.print_exc()
            error = True

        self.assertFalse(error)
示例#11
0
    def test_text_template(self):

        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_text_template"
        )

        user_data = {
            "mylist": [
                Mock(var0=1, var1="1", var2=1.0),
                Mock(var0=2, var1="2", var2=2.0),
                Mock(var0=3, var1="3", var2=3.0),
            ]
        }

        outname = get_secure_filename()

        template = TextTemplate(template_name, outname)
        template.render(user_data)
        result = open(outname, "rb").read()

        expected = u"".join(
            u"{} {} {}\n".format(line.var0, line.var1, line.var2)
            for line in user_data["mylist"]
        ).encode("utf-8")

        self.assertEqual(result, expected)
示例#12
0
    def test_ignore_undefined_variables_text_template(self):

        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_text_template"
        )

        user_data = {}

        outname = get_secure_filename()

        template = TextTemplate(template_name, outname)
        error = True
        try:
            template.render(user_data)
            print(
                "Error: template contains variables that must be " "replaced"
            )
        except TemplateError:
            error = False

        self.assertFalse(error)

        template = TextTemplate(
            template_name, outname, ignore_undefined_variables=True
        )
        error = False
        try:
            template.render(user_data)
        except Exception:
            traceback.print_exc()
            error = True

        self.assertFalse(error)
示例#13
0
    def test_link_validation_missing_equal(self):
        """test a missing equal sign in a link raises a template error"""
        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_missing_eq_in_link.odt"
        )
        outname = get_secure_filename()

        template = Template(template_name, outname)

        class Item(object):
            def __init__(self, val):
                self.val = val

        data_dict = {"items": [Item(1), Item(2), Item(3), Item(4)]}

        template.set_image_path(
            "staticimage.logo",
            pkg_resources.resource_filename(
                "py3o.template", "tests/templates/images/new_logo.png"
            ),
        )
        except_occured = False
        error_text = ""
        try:
            template.render(data_dict)
        except TemplateException as e:
            except_occured = True
            error_text = "{}".format(e)

        assert except_occured is True
        assert error_text == (
            "Missing '=' in instruction 'for \"item in items\"'"
        )
示例#14
0
    def test_ignore_undefined_variables_1(self):

        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_undefined_variables_1.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data = {}

        error = True
        try:
            template.render(data)
            print("Error: template contains variables that must be "
                  "replaced")
        except TemplateError:
            error = False

        assert error is False

        template = Template(template_name, outname,
                            ignore_undefined_variables=True)

        error = False
        try:
            template.render(data)
        except:
            traceback.print_exc()
            error = True

        assert error is False
示例#15
0
    def test_template_with_function_call(self):
        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_template_function_call.odt"
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data_dict = {"amount": 32.123}

        template.render(data_dict)
        outodt = zipfile.ZipFile(outname, "r")

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0]))
        )

        result_a = lxml.etree.tostring(content_list, pretty_print=True).decode(
            "utf-8"
        )

        result_e = open(
            pkg_resources.resource_filename(
                "py3o.template",
                "tests/templates/template_with_function_call_result.xml",
            )
        ).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        assert result_a == result_e
示例#16
0
    def test_input_fields_with_function(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_template_input_fields_for_function.odt')

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data_dict = {
            'datestring': '2017-10-02',
        }

        template.render(data_dict)
        outodt = zipfile.ZipFile(outname, 'r')

        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0])))

        result_a = lxml.etree.tostring(
            content_list,
            pretty_print=True,
        ).decode('utf-8')

        result_e = open(
            pkg_resources.resource_filename(
                'py3o.template',
                'tests/templates/input_fields_function_result.xml')).read()

        result_a = result_a.replace("\n", "").replace(" ", "")
        result_e = result_e.replace("\n", "").replace(" ", "")

        assert result_a == result_e
示例#17
0
    def test_ignore_undefined_variables_1(self):

        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_undefined_variables_1.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data = {}

        error = True
        try:
            template.render(data)
            print("Error: template contains variables that must be "
                  "replaced")
        except TemplateError:
            error = False

        assert error is False

        template = Template(template_name, outname,
                            ignore_undefined_variables=True)

        error = False
        try:
            template.render(data)
        except:
            traceback.print_exc()
            error = True

        assert error is False
示例#18
0
    def test_render_apply_style(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template', 'tests/templates/py3o_style1_template.odt')

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.set_image_path(
            'logo',
            pkg_resources.resource_filename(
                'py3o.template', 'tests/templates/images/new_logo.png'))

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = 12345.35
        item1.InvoiceRef = '#1234'

        items.append(item1)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(items=items, document=document)
        template.render(data)

        # reuse the template engine just to open our odt file content...
        tempout = get_secure_filename()
        t2 = Template(outname, tempout)
        os.unlink(tempout)

        result_content = t2.content_trees[0]
        expr = "//text:p[contains(text(), 'Invoice')]"
        paragraphs = result_content.xpath(expr, namespaces=t2.namespaces)
        assert len(
            paragraphs) == 1, "Only one paragraph should have been found"
        p = paragraphs[0]
        result_text = p.text
        print(result_text)
        assert result_text == "Invoice #1234 for a total of 12345,35 EUR"
示例#19
0
    def setUp(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template', 'tests/templates/py3o_example_template.odt')

        outname = get_secure_filename()

        self.reference_template = Template(template_name, outname)
        os.unlink(outname)
示例#20
0
    def test_list_duplicate(self):
        """test duplicated listed get a unique id"""
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_list_template.odt'
        )
        outname = get_secure_filename()

        template = Template(template_name, outname)

        class Item(object):
            def __init__(self, val):
                self.val = val
        data_dict = {
            "items": [Item(1), Item(2), Item(3), Item(4)]
        }

        error = False

        template.set_image_path('logo', pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/images/new_logo.png'
        ))
        template.render(data_dict)

        outodt = zipfile.ZipFile(outname, 'r')
        try:
            content_list = [
                lxml.etree.parse(BytesIO(outodt.read(filename)))
                for filename in template.templated_files
            ]
        except lxml.etree.XMLSyntaxError as e:
            error = True
            print(
                "List was not deduplicated->{}".format(e)
            )

        # remove end file
        os.unlink(outname)

        assert error is False

        # first content is the content.xml
        content = content_list[0]
        list_expr = '//text:list'
        list_items = content.xpath(
            list_expr,
            namespaces=template.namespaces
        )
        ids = []
        for list_item in list_items:
            ids.append(
                list_item.get(
                    '{}id'.format(XML_NS)
                )
            )
        assert ids, "this list of ids should not be empty"
        assert len(ids) == len(set(ids)), "all ids should have been unique"
示例#21
0
    def test_remove_soft_page_breaks(self):
        template_xml = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_soft_page_break.odt"
        )
        outname = get_secure_filename()

        template = Template(template_xml, outname)
        soft_breaks = get_soft_breaks(
            template.content_trees[0], template.namespaces
        )
        self.assertEqual(len(soft_breaks), 2)

        template.remove_soft_breaks()
        soft_breaks = get_soft_breaks(
            template.content_trees[0], template.namespaces
        )
        self.assertEqual(len(soft_breaks), 0)

        template = Template(template_xml, outname)
        soft_breaks = get_soft_breaks(
            template.content_trees[0], template.namespaces
        )
        self.assertEqual(len(soft_breaks), 2)

        template.render(data={"list1": [1, 2, 3]})
        soft_breaks = get_soft_breaks(
            template.content_trees[0], template.namespaces
        )
        self.assertEqual(len(soft_breaks), 0)

        outodt = zipfile.ZipFile(outname, "r")
        content_list = lxml.etree.parse(
            BytesIO(outodt.read(template.templated_files[0]))
        )

        nmspc = template.namespaces
        paragraphs = content_list.findall("//text:p", nmspc)
        bottom_break_paragraphs, middle_break_paragraphs = 0, 0
        for p in paragraphs:
            if not p.text:
                continue
            text = p.text.strip()
            if text == (
                u"This is a text with a margin at the bottom and a "
                u"soft-page-break"
            ):
                bottom_break_paragraphs += 1
            elif text == (
                u"This is a paragraph that is cut in half by a "
                u"soft-page-break. This text should not remain cut "
                u"in half after rendering."
            ):
                middle_break_paragraphs += 1
            else:
                self.fail(u"Unidentified text in result: {}".format(text))

        self.assertEqual(bottom_break_paragraphs, 3)
        self.assertEqual(middle_break_paragraphs, 3)
示例#22
0
 def test_content_tree_with_child_instruction(self):
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_example_invalid_template.odt'
     )
     t = Template(template_xml, get_secure_filename())
     usr_insts = t.get_user_instructions()
     assert usr_insts == ['for="item in items"', '/for',
                          'for="item in items', '2', '"', '/for']
示例#23
0
 def __load_and_convert_template(self, path):
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         path
     )
     t = Template(template_xml, get_secure_filename())
     expressions = t.get_all_user_python_expression()
     py_expr = t.convert_py3o_to_python_ast(expressions)
     return py_expr
示例#24
0
 def test_content_tree_with_child_instruction(self):
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_example_invalid_template.odt'
     )
     t = Template(template_xml, get_secure_filename())
     usr_insts = t.get_user_instructions()
     assert usr_insts == ['for="item in items"', '/for',
                          'for="item in items', '2', '"', '/for']
示例#25
0
    def __save_output(self):
        """Saves the output into a native OOo document format.
        """
        out = zipfile.ZipFile(self.outputfilename, 'w', allowZip64=True)

        manifest_info = None
        for info_zip in self.infile.infolist():
            if "manifest.xml" in info_zip.filename:
                manifest_info = info_zip
                continue

            if info_zip.filename in self.templated_files:
                # get a temp file
                streamout = open(get_secure_filename(), "w+b")

                # Template file - we have edited these.
                fname, output_stream = self.output_streams[
                    self.templated_files.index(info_zip.filename)]

                transformer = get_list_transformer(self.namespaces)
                nstream = output_stream | transformer

                # write the whole stream to it
                for chunk in nstream.serialize():
                    streamout.write(chunk.encode('utf-8'))
                    yield True

                streamout.seek(0)

                # close the temp file to flush all data and make sure we get
                # it back when writing to the zip archive.
                streamout.close()

                # write the full file to archive
                out.write(streamout.name, fname)

                # remove temp file
                os.unlink(streamout.name)

            else:
                # Copy other files straight from the source archive.
                out.writestr(info_zip, self.infile.read(info_zip.filename))

        # the manifest must be processed at the end since its content
        # depends on the processing of others files (ie: content.xml)
        if manifest_info:
            manifest_e = self.__add_images_to_manifest()
            out.writestr(manifest_info, lxml.etree.tostring(manifest_e))

        # Save images in the "Pictures" sub-directory of the archive.
        for identifier, im_struct in self.images.items():
            out.writestr(identifier, im_struct.get('data'))

        # close the zipfile before leaving
        out.close()
        yield True
示例#26
0
    def dump_input_flow(in_flow, sep):
        """Dump all the items in a file."""
        filename = get_secure_filename()
        file_data = open(filename, 'wb')

        for item in in_flow:
            file_data.write(pickle.dumps(item, protocol=protocol) + sep)

        file_data.close()
        return filename
示例#27
0
    def setUp(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )

        outname = get_secure_filename()

        self.reference_template = Template(template_name, outname)
        os.unlink(outname)
示例#28
0
    def test_invalid_template_1(self):
        """a template should not try to define a /for and a for on the same
        paragraph
        """

        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_invalid_template.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = '12345.35'
        item1.InvoiceRef = '#1234'
        items.append(item1)

        # if you are using python 2.x you should use xrange
        for i in range(1000):
            item = Item()
            item.val1 = 'Item%s Value1' % i
            item.val2 = 'Item%s Value2' % i
            item.val3 = 'Item%s Value3' % i
            item.Currency = 'EUR'
            item.Amount = '6666.77'
            item.InvoiceRef = 'Reference #%04d' % i
            items.append(item)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(
            items=items,
            items2=copy.copy(items),
            document=document
        )

        error = False
        try:
            template.render(data)
        except TemplateException:
            error = True

        assert error is True, "This template should have been refused"
示例#29
0
    def test_example_1(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.set_image_path(
            'staticimage.logo',
            pkg_resources.resource_filename(
                'py3o.template',
                'tests/templates/images/new_logo.png'
            )
        )

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = '12345.35'
        item1.InvoiceRef = '#1234'
        items.append(item1)

        # if you are using python 2.x you should use xrange
        for i in range(1000):
            item = Item()
            item.val1 = 'Item%s Value1' % i
            item.val2 = 'Item%s Value2' % i
            item.val3 = 'Item%s Value3' % i
            item.Currency = 'EUR'
            item.Amount = '6666.77'
            item.InvoiceRef = 'Reference #%04d' % i
            items.append(item)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(items=items, document=document)
        error = False
        try:
            template.render(data)
        except ValueError as e:
            print('The template did not render properly...')
            traceback.print_exc()
            error = True

        assert error is False
示例#30
0
    def test_invalid_template_1(self):
        """a template should not try to define a /for and a for on the same
        paragraph
        """

        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_invalid_template.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = '12345.35'
        item1.InvoiceRef = '#1234'
        items.append(item1)

        # if you are using python 2.x you should use xrange
        for i in range(1000):
            item = Item()
            item.val1 = 'Item%s Value1' % i
            item.val2 = 'Item%s Value2' % i
            item.val3 = 'Item%s Value3' % i
            item.Currency = 'EUR'
            item.Amount = '6666.77'
            item.InvoiceRef = 'Reference #%04d' % i
            items.append(item)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(
            items=items,
            items2=copy.copy(items),
            document=document
        )

        error = False
        try:
            template.render(data)
        except TemplateException:
            error = True

        assert error is True, "This template should have been refused"
示例#31
0
 def test_jsonify_empty_for_loop(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template', 'tests/templates/py3o_empty_for_loop.odt')
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {'my1list': []}
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'my1list': []}
     assert res == expected
示例#32
0
    def test_remove_soft_page_breaks(self):
        template_xml = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_soft_page_break.odt'
        )
        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.remove_soft_breaks()
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0

        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.render(data={"list1": [1, 2, 3]})
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0
示例#33
0
    def test_remove_soft_page_breaks(self):
        template_xml = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_soft_page_break.odt'
        )
        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.remove_soft_breaks()
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0

        t = Template(template_xml, get_secure_filename())
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) > 0

        t.render(data={"list1": [1, 2, 3]})
        soft_breaks = get_soft_breaks(t.content_trees[0], t.namespaces)
        assert len(soft_breaks) == 0
示例#34
0
 def test_jsonify_global_variable_inside_loop(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_access_global_variable_inside_loop.odt')
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {'global_var': Mock(val='global_val')}
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'global_var': {'val': 'global_val'}, 'my4list': []}
     assert res == expected
示例#35
0
 def test_jsonify_in_loop_variable_with_attribute(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_access_in_loop_variable_with_attribute.odt')
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {'my3list': [Mock(val='val1'), Mock(val='val2')]}
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'my3list': [{'val': 'val1'}, {'val': 'val2'}]}
     assert res == expected
示例#36
0
    def test_example_1(self):
        template_name = pkg_resources.resource_filename(
            "py3o.template", "tests/templates/py3o_example_template.odt"
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.set_image_path(
            "staticimage.logo",
            pkg_resources.resource_filename(
                "py3o.template", "tests/templates/images/new_logo.png"
            ),
        )

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = "Item1 Value1"
        item1.val2 = "Item1 Value2"
        item1.val3 = "Item1 Value3"
        item1.Currency = "EUR"
        item1.Amount = "12345.35"
        item1.InvoiceRef = "#1234"
        items.append(item1)

        # if you are using python 2.x you should use xrange
        for i in range(1000):
            item = Item()
            item.val1 = "Item%s Value1" % i
            item.val2 = "Item%s Value2" % i
            item.val3 = "Item%s Value3" % i
            item.Currency = "EUR"
            item.Amount = "6666.77"
            item.InvoiceRef = "Reference #%04d" % i
            items.append(item)

        document = Item()
        document.total = "9999999999999.999"

        data = dict(items=items, document=document)
        error = False
        try:
            template.render(data)
        except ValueError:
            print("The template did not render properly...")
            traceback.print_exc()
            error = True

        assert error is False
示例#37
0
    def __open_bucket(self, reference_value, ref_value_count):
        """Open a new bucket and return the bucket filename
        """
        filename = get_secure_filename()
        self.bucket_filenames.append(filename)

        data_file = open(filename, 'wb')
        self.bucket_files[filename] = data_file
        self.bucket_size[filename] = ref_value_count
        self.bucket_reference_value[reference_value] = filename

        return filename
示例#38
0
    def test_example_1(self):
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)
        template.set_image_path('logo', pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/images/new_logo.png'
        ))

        class Item(object):
            pass

        items = list()

        item1 = Item()
        item1.val1 = 'Item1 Value1'
        item1.val2 = 'Item1 Value2'
        item1.val3 = 'Item1 Value3'
        item1.Currency = 'EUR'
        item1.Amount = '12345.35'
        item1.InvoiceRef = '#1234'
        items.append(item1)

        # if you are using python 2.x you should use xrange
        for i in range(1000):
            item = Item()
            item.val1 = 'Item%s Value1' % i
            item.val2 = 'Item%s Value2' % i
            item.val3 = 'Item%s Value3' % i
            item.Currency = 'EUR'
            item.Amount = '6666.77'
            item.InvoiceRef = 'Reference #%04d' % i
            items.append(item)

        document = Item()
        document.total = '9999999999999.999'

        data = dict(items=items, document=document)
        error = False
        try:
            template.render(data)
        except ValueError as e:
            print('The template did not render properly...')
            traceback.print_exc()
            error = True

        assert error is False
示例#39
0
 def test_jsonify_global_variable_inside_loop(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_access_global_variable_inside_loop.odt'
     )
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {
         'global_var': Mock(val='global_val')
     }
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'global_var': {'val': 'global_val'}, 'my4list': []}
     assert res == expected
示例#40
0
 def test_jsonify_iterator_with_attribute_and_in_loop_variable(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_iterator_with_attribute_and_in_loop_variable.odt'
     )
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {
         'global_var': Mock(my6list=['val1', 'val2'])
     }
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'global_var': {'my6list': ['val1', 'val2']}}
     assert res == expected
示例#41
0
 def test_jsonify_in_loop_variable_with_attribute(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_access_in_loop_variable_with_attribute.odt'
     )
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {
         'my3list': [Mock(val='val1'), Mock(val='val2')]
     }
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'my3list': [{'val': 'val1'}, {'val': 'val2'}]}
     assert res == expected
示例#42
0
 def test_jsonify_access_in_loop_variable(self):
     """ Test the jsonify function
     """
     template_xml = pkg_resources.resource_filename(
         'py3o.template',
         'tests/templates/py3o_access_in_loop_variable.odt'
     )
     t = Template(template_xml, get_secure_filename())
     for_lists, variables = t.get_user_instructions_mapping()
     data = {
         'my2list': ['val1', 'val2']
     }
     res = ForList.to_dict(for_lists, variables, data)
     expected = {'my2list': ['val1', 'val2']}
     assert res == expected
示例#43
0
    def __save_output(self):
        """Saves the output into a native OOo document format.
        """
        out = zipfile.ZipFile(self.outputfilename, 'w')

        for info_zip in self.infile.infolist():

            if info_zip.filename in self.templated_files:
                # Template file - we have edited these.

                # get a temp file
                streamout = open(get_secure_filename(), "w+b")
                fname, output_stream = self.output_streams[
                    self.templated_files.index(info_zip.filename)
                ]

                transformer = get_list_transformer(self.namespaces)
                remapped_stream = output_stream | transformer

                # write the whole stream to it
                for chunk in remapped_stream.serialize():
                    streamout.write(chunk.encode('utf-8'))
                    yield True

                # close the temp file to flush all data and make sure we get
                # it back when writing to the zip archive.
                streamout.close()

                # write the full file to archive
                out.write(streamout.name, fname)

                # remove temp file
                os.unlink(streamout.name)

            else:
                # Copy other files straight from the source archive.
                out.writestr(info_zip, self.infile.read(info_zip.filename))

        # Save images in the "Pictures" sub-directory of the archive.
        for identifier, data in self.images.items():
            out.writestr(PY3O_IMAGE_PREFIX + identifier, data)

        # close the zipfile before leaving
        out.close()
        yield True
示例#44
0
    def test_get_user_instructions(self):
        source_odt_filename = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )
        outfilename = get_secure_filename()

        template = Template(source_odt_filename, outfilename)

        user_instructions = template.get_user_instructions()

        expected_vars = [
            'for="line in items"',
            '/for',
            'for="item in items"',
            'if="item.InvoiceRef==\'#1234\'"',
            '/if',
            '/for',
        ]
        assert set(user_instructions) == set(expected_vars)
示例#45
0
    def test_get_user_variables(self):
        source_odt_filename = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_example_template.odt'
        )
        outfilename = get_secure_filename()

        template = Template(source_odt_filename, outfilename)

        user_vars = template.get_user_variables()

        expected_vars = [
            'line.val1',
            'line.val2',
            'line.val3',
            'item.Amount',
            'item.Currency',
            'item.InvoiceRef',
            'document.total',
        ]
        assert set(user_vars) == set(expected_vars)
示例#46
0
    def test_ignore_undefined_variables_2(self):
        """
        Test ignore undefined variables for template with dotted variables like
        py3o.document.value
        """

        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_undefined_variables_2.odt'
        )

        outname = get_secure_filename()

        template = Template(template_name, outname)

        data = {}

        error = True
        try:
            template.render(data)
            print("Error: template contains variables that must be "
                  "replaced")
        except TemplateError:
            error = False

        assert error is False

        template = Template(template_name, outname,
                            ignore_undefined_variables=True)

        error = True
        try:
            template.render(data)
            print("Error: template contains dotted variables that must be "
                  "replaced")
        except TemplateError:
            error = False

        assert error is False
示例#47
0
    def test_missing_opening(self):
        """test orphaned /for raises a TemplateException"""
        template_name = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_missing_open_template.odt'
        )
        outname = get_secure_filename()
        try:
            template = Template(template_name, outname)

        finally:
            os.remove(outname)

        class Item(object):
            def __init__(self, val):
                self.val = val
        data_dict = {
            "items": [Item(1), Item(2), Item(3), Item(4)]
        }

        template.set_image_path('logo', pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/images/new_logo.png'
        ))
        # this will raise a TemplateException... or the test will fail
        error_occured = False
        try:
            template.render(data_dict)

        except TemplateException as e:
            error_occured = True
            # make sure this is the correct TemplateException that pops
            assert e.message == "No open instruction for /for"

        # and make sure we raised
        assert error_occured is True
示例#48
0
    def test_jsonify_iterator_with_attribute_and_in_loop_variable_with_attribute(self):
        """ Test the jsonify function
        """
        template_xml = pkg_resources.resource_filename(
            'py3o.template',
            'tests/templates/py3o_iterator_with_attribute_and_in_loop_variable_with_attribute.odt'
        )
        t = Template(template_xml, get_secure_filename())
        for_lists, variables = t.get_user_instructions_mapping()
        data = {
            'global_var': Mock(my7list=[Mock(val='val1'), Mock(val='val2')])
        }
        res = ForList.to_dict(for_lists, variables, data)
        expected = {'global_var': {'my7list': [{'val': 'val1'}, {'val': 'val2'}]}}
        assert res == expected

#    def test_jsonify_access_variable_in_nested_loop(self):
#        """ Test the jsonify function
#        """
#        template_xml = pkg_resources.resource_filename(
#            'py3o.template',
#            'tests/templates/py3o_access_variable_in_nested_loop.odt'
#        )
#        t = Template(template_xml, get_secure_filename())
#        for_lists, variables = t.get_user_instructions_mapping()
#        data = {
#            'my8list': [['val1', 'val2'], ['val3']]
#        }
#        res = ForList.to_dict(for_lists, variables, data)
#        expected = {'my8list': [['val1', 'val2'], ['val3']]}
#        assert res == expected

#    def test_jsonify_access_parent_variable_in_nested_loop(self):
#        """ Test the jsonify function
#        """
#        template_xml = pkg_resources.resource_filename(
#            'py3o.template',
#            'tests/templates/py3o_access_parent_variable_in_nested_loop.odt'
#        )
#        t = Template(template_xml, get_secure_filename())
#        for_lists, variables = t.get_user_instructions_mapping()
#        data = {
#            'my9list': [Mock(val='val1', mylist=[]), Mock(val='val2', mylist=[])]
#        }
#        res = ForList.jsonify(for_lists, variables, data)
#        expected = "{'my9list': [{'val': 'val1', 'mylist': []}, {'val': 'val2', 'mylist': []}]}"
#        assert res == expected
#
#    def test_jsonify_access_variable_in_nested_loop_with_attribute(self):
#        """ Test the jsonify function
#        """
#        template_xml = pkg_resources.resource_filename(
#            'py3o.template',
#            'tests/templates/py3o_access_variable_in_nested_loop_with_attribute.odt'
#        )
#        t = Template(template_xml, get_secure_filename())
#        for_lists, variables = t.get_user_instructions_mapping()
#        data = {
#            'my10list': [Mock(my_list=[Mock(val='val1'), Mock(val='val2')]), Mock(my_list=[Mock(val='val3')])]
#        }
#        res = ForList.jsonify(for_lists, variables, data)
#        expected = "{'my10list': [{'my_list': [{'val': 'val1'}, {'val': 'val2'}]}, {'my_list': [{'val': 'val3'}]}]}"
#        assert res == expected