File: yaml2pdf.py

package info (click to toggle)
python-reportlab 3.1.8-3%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 6,996 kB
  • ctags: 9,483
  • sloc: python: 69,727; ansic: 19,108; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (104 lines) | stat: -rw-r--r-- 3,464 bytes parent folder | download | duplicates (3)
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
#Copyright ReportLab Europe Ltd. 2000-2012
#see license.txt for license details
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/yaml2pdf.py
# yaml2pdf - turns stuff in Yet Another Markup Language
# into PDF documents.  Very crude - it assumes a
# doc template and stylesheet (hard coded for now)
# and basically cranks out paragraphs in each style
"""yaml2pdf.py - converts Yet Another Markup Language
to reasonable PDF documents.  This is ReportLab's
basic documentation tool.

Usage:
.  "yaml2pdf.py filename.ext" will create "filename.pdf"
"""

import sys
import os
import imp

from . import yaml
from .rltemplate import RLDocTemplate
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import *
from reportlab.lib.pagesizes import A4
from reportlab.platypus import *
from reportlab.lib import colors
from reportlab.lib.units import inch


from .stylesheet import getStyleSheet


def run(infilename, outfilename):
    p = yaml.Parser()
    results = p.parseFile(infilename)

    ss = getStyleSheet()

    #now make flowables from the results
    story = []
    for thingy in results:
        typ = thingy[0]
        if typ == 'Paragraph':
            (typ2, stylename, text) = thingy
            if stylename == 'bu':
                bulletText='\267'
            else:
                bulletText=None
            try:
                style = ss[stylename]
            except KeyError:
                print('Paragraph style "%s" not found in stylesheet, using Normal instead' % stylename)
                style = ss['Normal']
            story.append(Paragraph(text, style, bulletText=bulletText))
        elif typ == 'Preformatted':
            (typ2, stylename, text) = thingy
            try:
                style = ss[stylename]
            except KeyError:
                print('Preformatted style "%s" not found in stylesheet, using Normal instead' % stylename)
                style = ss['Normal']
            story.append(Preformatted(text, style, bulletText=bulletText))
        elif typ == 'Image':
            filename = thingy[1]
            img = Image(filename)
            story.append(img)
        elif typ == 'PageBreak':
            story.append(PageBreak())
        elif typ == 'VSpace':
            height = thingy[1]
            story.append(Spacer(0, height))
        elif typ == 'NextPageTemplate':
            story.append(NextPageTemplate(thingy[1]))
        elif typ == 'Custom':
            # go find it
            searchPath = [os.getcwd()+'\\']
            (typ2, moduleName, funcName) = thingy
            found = imp.find_module(moduleName, searchPath)
            assert found, "Custom object module %s not found" % moduleName
            (file, pathname, description) = found
            mod = imp.load_module(moduleName, file, pathname, description)

            #now get the function
            func = getattr(mod, funcName)
            story.append(func())

        else:
            print('skipping',typ, 'for now')


    #print it
    doc = RLDocTemplate(outfilename, pagesize=A4)
    doc.build(story)

if __name__ == '__main__': #NORUNTESTS
    if len(sys.argv) == 2:
        infilename = sys.argv[1]
        outfilename = os.path.splitext(infilename)[0] + '.pdf'
        if os.path.isfile(infilename):
            run(infilename, outfilename)
        else:
            print('File not found %s' % infilename)
    else:
        print(__doc__)