1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from vstf.controller.reporters.report.pdf.pdftemplate import PdfVswitch
12 from vstf.controller.reporters.report.pdf.story import TitleStory, SpaceStory, ImageStory, TableStory, \
13 LinePlotStory, Story, TableOfContentsStory, PageBreakStory, ParagraphStory, BarChartStory
14 import vstf.common.candy_text as candy
15 from vstf.controller.reporters.report.provider.pdf_provider import PdfProvider
16 from vstf.controller.settings.template_settings import TemplateSettings
21 LOG = logging.getLogger(__name__)
24 class PdfCreator(object):
26 def __init__(self, provider):
27 self._provider = provider
32 theme = self._provider.get_theme
33 self._pdf = PdfVswitch(theme["title"],
40 def save_pdf(self, ofile):
41 self._pdf.generate(self._story, ofile)
43 def add_coverpage(self):
45 story = PageBreakStory(story)
46 self._story += story.storylist
48 def add_contents(self):
49 if self._provider.ifcontents:
51 story = TableOfContentsStory(story)
52 self._story += story.storylist
54 def create_story(self):
59 def create(self, ofile):
64 def add_context(self):
65 context = self._provider.get_context
66 self._story += self._raw_context(context)
68 def _raw_context(self, context, ci=0, si=0, ui=0, level=-1):
70 for key, value in sorted(context.items()):
73 _sn, _node, _style = candy.text2tuple(key)
74 if _node in candy.dom:
75 if _node == candy.chapter:
77 elif _node == candy.section:
81 _story += self._raw_context(value, ci, si, ui, level + 1)
85 LOG.info("node: %s %s" % (_node, candy.title))
86 if _node == candy.title:
88 if level in range(len(candy.dom)):
90 value[0] = "Chapter %s %s" % (ci, value[0])
91 story = PageBreakStory(story)
93 value[0] = "%s.%s %s" % (ci, si, value[0])
95 value[0] = "%s.%s.%s %s" % (ci, si, ui, value[0])
97 story = TitleStory(story, data=value, style=_style)
98 elif _node == candy.table:
99 story = TableStory(story, data=value, style=_style)
100 elif _node == candy.figure:
101 story = ImageStory(story, data=value, style=_style)
102 elif _node == candy.paragraph:
103 story = ParagraphStory(story, data=value, style=_style)
104 elif _node == candy.plot:
105 story = LinePlotStory(story, data=value, style=_style)
106 elif _node == candy.chart:
107 story = BarChartStory(story, data=value, style=_style)
108 elif _node == candy.space:
109 assert isinstance(value, int)
110 for i in range(value):
111 story = SpaceStory(story)
112 _story += story.storylist
117 from vstf.common.log import setup_logging
120 log_file="/var/log/pdf-creator.log",
123 out_file = "vstf_report.pdf"
125 info = TemplateSettings()
126 provider = PdfProvider(info.settings)
127 reporter = PdfCreator(provider)
128 reporter.create(out_file)
130 if __name__ == '__main__':