demos/odyssey/fodyssey.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3721
diff changeset
     3
__version__='3.3.0'
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     4
__doc__=''
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     5
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     6
#REPORTLAB_TEST_SCRIPT
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
     7
import sys, copy, os
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     8
from reportlab.platypus import *
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     9
from reportlab.lib.units import inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    10
from reportlab.lib.styles import getSampleStyleSheet
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    11
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    13
import reportlab.rl_config
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    14
reportlab.rl_config.invariant = 1
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    15
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    16
styles = getSampleStyleSheet()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
Title = "The Odyssey"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
Author = "Homer"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    20
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    21
def myFirstPage(canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    22
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    23
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    24
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
def myLaterPages(canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    26
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
    canvas.setFont('Times-Roman',9)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    28
    canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
def go():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    32
    doc = SimpleDocTemplate('fodyssey.pdf',showBoundary='showboundary' in sys.argv)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
    doc.allowSplitting = not 'nosplitting' in sys.argv
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
    doc.build(Elements,myFirstPage,myLaterPages)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    35
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
Elements = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
ChapterStyle = copy.copy(styles["Heading1"])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
ChapterStyle.alignment = TA_CENTER
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
ChapterStyle.fontsize = 16
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
InitialStyle = copy.deepcopy(ChapterStyle)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    42
InitialStyle.fontsize = 16
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
InitialStyle.leading = 20
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
PreStyle = styles["Code"]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
def newPage():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
    Elements.append(PageBreak())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    48
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
def chapter(txt, style=ChapterStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
    newPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
    Elements.append(Paragraph(txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
    Elements.append(Spacer(0.2*inch, 0.3*inch))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    54
def fTitle(txt,style=InitialStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
    Elements.append(Paragraph(txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
ParaStyle = copy.deepcopy(styles["Normal"])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
ParaStyle.spaceBefore = 0.1*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
if 'right' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
    ParaStyle.alignment = TA_RIGHT
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
elif 'left' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    62
    ParaStyle.alignment = TA_LEFT
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    63
elif 'justify' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    64
    ParaStyle.alignment = TA_JUSTIFY
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    65
elif 'center' in sys.argv or 'centre' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
    ParaStyle.alignment = TA_CENTER
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    67
else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
    ParaStyle.alignment = TA_JUSTIFY
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    70
def spacer(inches):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    71
    Elements.append(Spacer(0.1*inch, inches*inch))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    72
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    73
def p(txt, style=ParaStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    74
    Elements.append(Paragraph(txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    75
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
def pre(txt, style=PreStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    77
    spacer(0.1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
    p = Preformatted(txt, style)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    79
    Elements.append(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    81
def parseOdyssey(fn):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
    from time import time
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
    E = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
    t0=time()
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    85
    text = open(fn,'r').read()
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    86
    i0 = text.index('Book I')
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    87
    endMarker = 'covenant of peace between the two contending parties.'
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    88
    i1 = text.index(endMarker)+len(endMarker)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    89
    PREAMBLE=list(map(str.strip,text[0:i0].split('\n')))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    90
    L=list(map(str.strip,text[i0:i1].split('\n')))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    91
    POSTAMBLE=list(map(str.strip,text[i1:].split('\n')))
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    92
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    93
    def ambleText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    94
        while L and not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    95
        while L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    96
            T=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    97
            while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    98
                T.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    99
            yield T
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   100
            while L and not L[0]: L.pop(0)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   102
    def mainText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   103
        while L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   104
            B = L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   105
            while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   106
            T=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   107
            while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   108
                T.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   109
            while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   110
            P = []
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   111
            while L and not (L[0].startswith('Book ') and len(L[0].split())==2):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   112
                E=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   113
                while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   114
                    E.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   115
                P.append(E)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   116
                if L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   117
                    while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   118
            yield B,T,P
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   119
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   120
    t1 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   121
    print("open(%s,'r').read() took %.4f seconds" %(fn,t1-t0))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   122
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   123
    E.append([spacer,2])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   124
    E.append([fTitle,'<font color=red>%s</font>' % Title, InitialStyle])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   125
    E.append([fTitle,'<font size=-4>by</font> <font color=green>%s</font>' % Author, InitialStyle])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   126
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   127
    for T in ambleText(PREAMBLE):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   128
        E.append([p,'\n'.join(T)])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   129
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   130
    for (B,T,P) in mainText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   131
        E.append([chapter,B])
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   132
        E.append([p,'<font size="+1" color="Blue"><b>%s</b></font>' % '\n'.join(T),ParaStyle])
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   133
        for x in P:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   134
            E.append([p,' '.join(x)])
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   135
    firstPre = 1
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   136
    for T in ambleText(POSTAMBLE):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   137
        E.append([p,'\n'.join(T)])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   138
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   139
    t3 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   140
    print("Parsing into memory took %.4f seconds" %(t3-t1))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   141
    del L
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   142
    t4 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   143
    print("Deleting list of lines took %.4f seconds" %(t4-t3))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   144
    for i in range(len(E)):
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 2969
diff changeset
   145
        E[i][0](*E[i][1:])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   146
    t5 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   147
    print("Moving into platypus took %.4f seconds" %(t5-t4))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   148
    del E
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   149
    t6 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   150
    print("Deleting list of actions took %.4f seconds" %(t6-t5))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   151
    go()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   152
    t7 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   153
    print("saving to PDF took %.4f seconds" %(t7-t6))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   154
    print("Total run took %.4f seconds"%(t7-t0))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   155
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   156
for fn in ('odyssey.full.txt','odyssey.txt'):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   157
    if os.path.isfile(fn):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   158
        break
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   159
if __name__=='__main__':
2967
ea62529bd1df reportlab-2.2: first stage changes in on the trunk
rgbecker
parents: 2963
diff changeset
   160
    parseOdyssey(fn)