summaryrefslogtreecommitdiffstats
path: root/testsuites/vstf/vstf_scripts/vstf/controller/reporters/report/pdf/pdfcreator.py
blob: 67f988c90fa30e884d3e89a942f11f3091296512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from vstf.controller.reporters.report.pdf.pdftemplate import PdfVswitch
from vstf.controller.reporters.report.pdf.story import TitleStory, SpaceStory, ImageStory, TableStory, \
    LinePlotStory, Story, TableOfContentsStory, PageBreakStory, ParagraphStory, BarChartStory
import vstf.common.candy_text as candy
from vstf.controller.reporters.report.provider.pdf_provider import PdfProvider
from vstf.controller.settings.template_settings import TemplateSettings

import os
import logging

LOG = logging.getLogger(__name__)


class PdfCreator(object):

    def __init__(self, provider):
        self._provider = provider
        self._story = []
        self._pdf = None

    def create_pdf(self):
        theme = self._provider.get_theme
        self._pdf = PdfVswitch(theme["title"],
                               theme["logo"],
                               theme["header"],
                               theme["footer"],
                               theme["note"],
                               theme["style"])

    def save_pdf(self, ofile):
        self._pdf.generate(self._story, ofile)

    def add_coverpage(self):
        story = Story()
        story = PageBreakStory(story)
        self._story += story.storylist

    def add_contents(self):
        if self._provider.ifcontents:
            story = Story()
            story = TableOfContentsStory(story)
            self._story += story.storylist

    def create_story(self):
        self.add_coverpage()
        self.add_contents()
        self.add_context()

    def create(self, ofile):
        self.create_pdf()
        self.create_story()
        self.save_pdf(ofile)

    def add_context(self):
        context = self._provider.get_context
        self._story += self._raw_context(context)

    def _raw_context(self, context, ci=0, si=0, ui=0, level=-1):
        _story = []
        for key, value in sorted(context.items()):
            LOG.info(key)
            LOG.info(value)
            _sn, _node, _style = candy.text2tuple(key)
            if _node in candy.dom:
                if _node == candy.chapter:
                    ci = _style
                elif _node == candy.section:
                    si = _style
                else:
                    ui = _style
                _story += self._raw_context(value, ci, si, ui, level + 1)

            else:
                story = Story()
                LOG.info("node: %s  %s" % (_node, candy.title))
                if _node == candy.title:
                    assert value
                    if level in range(len(candy.dom)):
                        if level == 0:
                            value[0] = "Chapter %s %s" % (ci, value[0])
                            story = PageBreakStory(story)
                        elif level == 1:
                            value[0] = "%s.%s %s" % (ci, si, value[0])
                        else:
                            value[0] = "%s.%s.%s %s" % (ci, si, ui, value[0])
                        LOG.info(value)
                        story = TitleStory(story, data=value, style=_style)
                elif _node == candy.table:
                    story = TableStory(story, data=value, style=_style)
                elif _node == candy.figure:
                    story = ImageStory(story, data=value, style=_style)
                elif _node == candy.paragraph:
                    story = ParagraphStory(story, data=value, style=_style)
                elif _node == candy.plot:
                    story = LinePlotStory(story, data=value, style=_style)
                elif _node == candy.chart:
                    story = BarChartStory(story, data=value, style=_style)
                elif _node == candy.space:
                    assert isinstance(value, int)
                    for i in range(value):
                        story = SpaceStory(story)
                _story += story.storylist
        return _story


def main():
    from vstf.common.log import setup_logging
    setup_logging(
        level=logging.DEBUG,
        log_file="/var/log/pdf-creator.log",
        clevel=logging.INFO)

    out_file = "vstf_report.pdf"

    info = TemplateSettings()
    provider = PdfProvider(info.settings)
    reporter = PdfCreator(provider)
    reporter.create(out_file)

if __name__ == '__main__':
    main()
{'template': 'NETWORK_uri', 'params': {'NETWORK': {'get_param': ['ServiceNetMap', net_param]}}}}]}, SUBST_CLOUDNAME: {'get_param': [PARAM_CLOUD_ENDPOINTS, {'get_param': ['ServiceNetMap', net_param]}]}, }) ]) } uri_fields = [protocol, '://', copy.deepcopy(host), ':', port] uri_fields_suffix = (copy.deepcopy(uri_fields) + ([uri_suffix] if uri_suffix is not None else [])) name = name_override if name_override is not None else (endpoint_name + endpoint_variant + endpoint_type) return name, { 'host_nobrackets': host_nobrackets, 'host': host, 'port': extract_field('port'), 'protocol': extract_field('protocol'), 'uri': { 'list_join': ['', uri_fields_suffix] }, 'uri_no_suffix': { 'list_join': ['', uri_fields] }, } def template_endpoint_items(config): def get_svc_endpoints(ep_name, svc): for ep_type in set(svc) & ENDPOINT_TYPES: defn = svc[ep_type] for variant, suffix in defn.get('uri_suffixes', {'': None}).items(): name_override = defn.get('names', {}).get(variant) yield template_output_definition(ep_name, variant, ep_type, net_param_name(defn), suffix, name_override) return itertools.chain.from_iterable(sorted(get_svc_endpoints(ep_name, svc)) for (ep_name, svc) in sorted(config.items())) def generate_endpoint_map_template(config): return collections.OrderedDict([ ('heat_template_version', 'pike'), ('description', 'A map of OpenStack endpoints. Since the endpoints ' 'are URLs, we need to have brackets around IPv6 IP addresses. The ' 'inputs to these parameters come from net_ip_uri_map, which will ' 'include these brackets in IPv6 addresses.'), ('parameters', template_parameters(config)), ('outputs', { 'endpoint_map': { 'value': collections.OrderedDict(template_endpoint_items(config)) } }), ]) autogen_warning = """### DO NOT MODIFY THIS FILE ### This file is automatically generated from endpoint_data.yaml ### by the script build_endpoint_map.py """ class TemplateDumper(yaml.SafeDumper): def represent_ordered_dict(self, data): return self.represent_dict(data.items()) TemplateDumper.add_representer(collections.OrderedDict, TemplateDumper.represent_ordered_dict) def write_template(template, filename=None): with get_file(OUT_FILE, filename, writable=True) as f: f.write(autogen_warning) yaml.dump(template, f, TemplateDumper, width=68) def read_template(template, filename=None): with get_file(OUT_FILE, filename) as f: return yaml.safe_load(f) def build_endpoint_map(output_filename=None, input_filename=None): if output_filename is not None and output_filename == input_filename: raise Exception('Cannot read from and write to the same file') config = load_endpoint_data(input_filename) template = generate_endpoint_map_template(config) write_template(template, output_filename) def check_up_to_date(output_filename=None, input_filename=None): if output_filename is not None and output_filename == input_filename: raise Exception('Input and output filenames must be different') config = load_endpoint_data(input_filename) template = generate_endpoint_map_template(config) existing_template = read_template(output_filename) return existing_template == template def get_options(): from optparse import OptionParser parser = OptionParser('usage: %prog' ' [-i INPUT_FILE] [-o OUTPUT_FILE] [--check]', description=__doc__) parser.add_option('-i', '--input', dest='input_file', action='store', default=None, help='Specify a different endpoint data file') parser.add_option('-o', '--output', dest='output_file', action='store', default=None, help='Specify a different endpoint map template file') parser.add_option('-c', '--check', dest='check', action='store_true', default=False, help='Check that the output file is ' 'up to date with the data') parser.add_option('-d', '--debug', dest='debug', action='store_true', default=False, help='Print stack traces on error') return parser.parse_args() def main(): options, args = get_options() if args: print('Warning: ignoring positional args: %s' % ' '.join(args), file=sys.stderr) try: if options.check: if not check_up_to_date(options.output_file, options.input_file): print('EndpointMap template does not match input data. Please ' 'run the build_endpoint_map.py tool to update the ' 'template.', file=sys.stderr) sys.exit(2) else: build_endpoint_map(options.output_file, options.input_file) except Exception as exc: if options.debug: raise print('%s: %s' % (type(exc).__name__, str(exc)), file=sys.stderr) sys.exit(1) if __name__ == '__main__': main()