Beispiel #1
0
def create_pdf(source, config):
    """Entry point for PDF generation."""
    if stored_exc is not None:
        # We cannot generate a PDF if there was an exception importing the
        # dependencies, so raise and abort here.
        #
        # Need to explicitly disable the raising-bad-type check here since
        # pylint isn't smart enough to know that we explicitly checked that
        # the stored_exc is not None above.
        raise stored_exc  # pylint: disable=raising-bad-type

    # Depth values will be used for indentation on PDF, however
    # we want first level children to have depth = 0 (otherwise we'll have to
    # do `depth + 1` everywhere in the renderers.
    # The renderer for root will discard the negative depth.
    data = [(depth - 1, rep) for depth, rep in source.flatten(depths=True)]

    reportlab_data = []
    reportlab_styles = []
    row_idx = 0

    for depth, obj in data:

        registry = (report_registry
                    if isinstance(obj, Report) else serialized_entry_registry)

        renderer = registry[obj](style=config.pdf_style)
        if renderer.should_display(source=obj):
            row_data = renderer.get_row_data(source=obj,
                                             depth=depth,
                                             row_idx=row_idx)

            row_idx = row_data.end

            reportlab_data.extend(row_data.content)
            reportlab_styles.extend(row_data.style)

    template = SimpleDocTemplate(
        filename=config.pdf_path,
        pageSize=const.PAGE_SIZE,
        topMargin=const.PAGE_MARGIN,
        bottomMargin=const.PAGE_MARGIN,
        leftMargin=const.PAGE_MARGIN,
        rightMargin=const.PAGE_MARGIN,
        title="Testplan report - {}".format(source.name),
    )

    tables = create_base_tables(
        data=reportlab_data,
        style=const.TABLE_STYLE + reportlab_styles,
        col_widths=[width * template.width for width in const.COL_WIDTHS],
    )

    template.build(tables)
Beispiel #2
0
def create_pdf(source, config):
    """Entry point for PDF generation."""
    # Depth values will be used for indentation on PDF, however
    # we want first level children to have depth = 0 (otherwise we'll have to
    # do `depth + 1` everywhere in the renderers.
    # The renderer for root will discard the negative depth.
    data = [(depth - 1, rep) for depth, rep in source.flatten(depths=True)]

    reportlab_data = []
    reportlab_styles = []
    row_idx = 0

    for depth, obj in data:

        registry = report_registry if isinstance(
            obj, Report) else serialized_entry_registry

        renderer = registry[obj](style=config.pdf_style)
        if renderer.should_display(source=obj):
            row_data = renderer.get_row_data(source=obj,
                                             depth=depth,
                                             row_idx=row_idx)

            row_idx = row_data.end

            reportlab_data.extend(row_data.content)
            reportlab_styles.extend(row_data.style)

    template = SimpleDocTemplate(filename=config.pdf_path,
                                 pageSize=const.PAGE_SIZE,
                                 topMargin=const.PAGE_MARGIN,
                                 bottomMargin=const.PAGE_MARGIN,
                                 leftMargin=const.PAGE_MARGIN,
                                 rightMargin=const.PAGE_MARGIN,
                                 title='Testplan report - {}'.format(
                                     source.name))

    tables = create_base_tables(
        data=reportlab_data,
        style=const.TABLE_STYLE + reportlab_styles,
        col_widths=[width * template.width for width in const.COL_WIDTHS])

    template.build(tables)
Beispiel #3
0
    def create_pdf(self, source):
        """Entry point for PDF generation."""
        if stored_exc is not None:
            # We cannot generate a PDF if there was an exception importing the
            # dependencies, so raise and abort here.
            #
            # Need to explicitly disable the raising-bad-type check here since
            # pylint isn't smart enough to know that we explicitly checked that
            # the stored_exc is not None above.
            raise stored_exc  # pylint: disable=raising-bad-type

        # Depth values will be used for indentation on PDF, however
        # we want first level children to have depth = 0 (otherwise we'll have to
        # do `depth + 1` everywhere in the renderers.
        # The renderer for root will discard the negative depth.
        data = [(depth - 1, rep) for depth, rep in source.flatten(depths=True)]

        reportlab_data = []
        reportlab_styles = []
        row_idx = 0

        for depth, obj in data:

            registry = (report_registry if isinstance(obj, Report) else
                        serialized_entry_registry)

            renderer = registry[obj](style=self.cfg.pdf_style)
            if renderer.should_display(source=obj):
                row_data = renderer.get_row_data(source=obj,
                                                 depth=depth,
                                                 row_idx=row_idx)
                row_idx = row_data.end

                reportlab_data.extend(row_data.content)
                reportlab_styles.extend(row_data.style)

        pdf_path = pathlib.Path(self.cfg.pdf_path).resolve()
        pdf_path.parent.mkdir(parents=True, exist_ok=True)
        try:
            if self.cfg.interactive_port is None:
                override = True
            else:
                override = False
        except AttributeError:
            override = True
        if not override and pdf_path.exists():
            pdf_path = pdf_path.with_name(
                f"{pdf_path.stem}_{int(time.time())}{pdf_path.suffix}")
            self.logger.exporter_info("File %s exists!", self.cfg.pdf_path)

        template = SimpleDocTemplate(
            filename=str(pdf_path),
            pagesize=const.PAGE_SIZE,
            topMargin=const.PAGE_MARGIN,
            bottomMargin=const.PAGE_MARGIN,
            leftMargin=const.PAGE_MARGIN,
            rightMargin=const.PAGE_MARGIN,
            title="Testplan report - {}".format(source.name),
        )

        tables = create_base_tables(
            data=reportlab_data,
            style=const.TABLE_STYLE + reportlab_styles,
            col_widths=[width * template.width for width in const.COL_WIDTHS],
        )

        template.build(tables)
        return str(pdf_path)