Beispiel #1
0
    def _gen_report(self, embedded: bool, title: str,
                    headline: str) -> t.Tuple[str, t.List[Path]]:
        """Build XML report document"""
        # convert Blocks to XML
        s = BuilderState(embedded)
        _s = self.top_block.to_xml(s)
        assert len(_s.elements) == 1

        # add main structure and Meta
        report_doc: Element = E.Report(
            E.Meta(
                E.Author("Anonymous"),  # TODO - get username from config?
                E.CreatedOn(timestamp()),
                E.Title(title),
                E.Headline(headline),
            ),
            E.Main(*_s.elements),
            version="1",
        )
        report_doc.set("{http://www.w3.org/XML/1998/namespace}id",
                       f"_{uuid.uuid4().hex}")

        # post_process and validate
        processed_report_doc = local_post_transform(
            report_doc, embedded="true()" if embedded else "false()")
        validate_report_doc(xml_doc=processed_report_doc)

        # convert to string
        report_str = etree.tounicode(processed_report_doc, pretty_print=True)
        log.debug("Built Report")
        log.info(report_str)
        return (report_str, _s.attachments)
Beispiel #2
0
def test_gen_report_with_files(datadir: Path):
    report = gen_report_with_files(datadir)
    report_str, attachments = report._gen_report(embedded=False,
                                                 title="TITLE",
                                                 description="DESCRIPTION")

    # print(report_str)
    assert len(attachments) == 6
    assert validate_report_doc(xml_str=report_str)
Beispiel #3
0
def test_gen_report_with_files(datadir: Path):
    report = gen_report_with_files(datadir)
    report_str, attachments = report._gen_report(embedded=False,
                                                 title="TITLE",
                                                 headline="HEADLINE")

    # print(report_str)
    assert len(attachments) == 5
    assert validate_report_doc(xml_str=report_str)
Beispiel #4
0
    def _gen_report(self,
                    embedded: bool,
                    title: str,
                    description: str = "Description",
                    author: str = "Anonymous") -> t.Tuple[str, t.List[Path]]:
        """Build XML report document"""
        # convert Pages to XML
        s = BuilderState(embedded)
        _s = reduce(lambda _s, p: p._to_xml(_s), self.pages, s)

        # add main structure and Meta
        report_doc: Element = E.Report(
            E.Meta(
                E.Author(author),  # TODO - get username from config?
                E.CreatedOn(timestamp()),
                E.Title(title),
                E.Description(description),
            ),
            E.Main(*_s.elements, type=self.report_type.value),
            version="1",
        )
        report_doc.set("{http://www.w3.org/XML/1998/namespace}id",
                       f"_{uuid.uuid4().hex}")

        # post_process and validate
        processed_report_doc = local_post_transform(
            report_doc, embedded="true()" if embedded else "false()")
        validate_report_doc(xml_doc=processed_report_doc)

        # check for any unsupported local features, e.g. DataTable
        # NOTE - we could eventually have different validators for local and published reports
        if embedded:
            uses_datatable: bool = processed_report_doc.xpath(
                "boolean(/Report/Main//DataTable)")
            if uses_datatable:
                raise UnsupportedFeature(
                    "DataTable component not supported when saving locally, please publish to a Datapane Server or use dp.Table"
                )

        # convert to string
        report_str = etree.tounicode(processed_report_doc, pretty_print=True)
        log.debug("Built Report")
        log.debug(report_str)
        return (report_str, _s.attachments)
Beispiel #5
0
def test_gen_report_nested_mixed():
    report = gen_report_nested_mixed()
    report_str, attachments = report._gen_report(embedded=False,
                                                 title="TITLE",
                                                 description="DESCRIPTION")

    # print(report_str)
    assert len(attachments) == 0
    assert len(report._top_block.blocks[0].blocks) == 2
    assert isinstance(report._top_block.blocks[0].blocks[0], dp.Blocks)
    assert isinstance(report._top_block.blocks[0].blocks[1], dp.Markdown)
    assert report._top_block.blocks[0].blocks[0].blocks[0].id == "test-id-1"
    assert validate_report_doc(xml_str=report_str)
Beispiel #6
0
def test_gen_report_simple():
    report = gen_report_simple()
    report_str, attachments = report._gen_report(embedded=False,
                                                 title="TITLE",
                                                 headline="HEADLINE")

    # print(report_str)
    assert len(attachments) == 0
    assert len(report.top_block.blocks) == 2
    assert report.top_block.blocks[0].id == "test-id-1"
    assert report.top_block.blocks[1].id == "block-2"
    assert isinstance(report.top_block.blocks[1], dp.Markdown)
    assert validate_report_doc(xml_str=report_str)
Beispiel #7
0
def assert_report(report: dp.Report,
                  expected_attachments: int = None,
                  expected_num_blocks: int = None):
    report_str, attachments = report._gen_report(embedded=False,
                                                 title="TITLE",
                                                 description="DESCRIPTION")
    # print(report_str)
    if expected_attachments:
        assert len(attachments) == expected_attachments
    if expected_num_blocks:
        assert num_blocks(report_str) == expected_num_blocks
    assert validate_report_doc(xml_str=report_str)
    return (report_str, attachments)
Beispiel #8
0
    def _gen_report(
        self,
        embedded: bool,
        title: str = "Title",
        description: str = "Description",
        author: str = "Anonymous",
    ) -> t.Tuple[str, t.List[Path]]:
        """Generate a report for saving/uploading"""
        report_doc, attachments = self._to_xml(embedded, title, description,
                                               author)

        # post_process and validate
        processed_report_doc = local_post_transform(
            report_doc, embedded="true()" if embedded else "false()")
        validate_report_doc(xml_doc=processed_report_doc)
        self._report_status_checks(processed_report_doc, embedded)

        # convert to string
        report_str = etree.tounicode(processed_report_doc)
        log.debug("Successfully Built Report")
        # log.debug(report_str)
        return (report_str, attachments)