demos/odyssey/dodyssey.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: 3951
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
_NEW_PARA=os.environ.get('NEW_PARA','0')[0] in ('y','Y','1')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    10
_REDCAP=int(os.environ.get('REDCAP','0'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    11
_CALLBACK=os.environ.get('CALLBACK','0')[0] in ('y','Y','1')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
if _NEW_PARA:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
    def Paragraph(s,style):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    14
        from rlextra.radxml.para import Paragraph as PPPP
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    15
        return PPPP(s,style)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    16
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
from reportlab.lib.units import inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
from reportlab.lib.styles import getSampleStyleSheet
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
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
    20
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    21
import reportlab.rl_config
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    22
reportlab.rl_config.invariant = 1
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    23
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    24
styles = getSampleStyleSheet()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    26
Title = "The Odyssey"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
Author = "Homer"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    28
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
def myTitlePage(canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    32
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
def myLaterPages(canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    35
    canvas.setFont('Times-Roman',9)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
    canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
def go():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
    def myCanvasMaker(fn,**kw):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
        from reportlab.pdfgen.canvas import Canvas
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 2969
diff changeset
    42
        canv = Canvas(fn,**kw)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
        # attach our callback to the canvas
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
        canv.myOnDrawCB = myOnDrawCB
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
        return canv
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
    doc = BaseDocTemplate('dodyssey.pdf',showBoundary=0)
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
    #normal frame as for SimpleFlowDocument
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
    frameT = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
    #Two Columns
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
    frame1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='col1')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    54
    frame2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
                        doc.height, id='col2')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
    doc.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=myTitlePage),
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
                        PageTemplate(id='OneCol',frames=frameT, onPage=myLaterPages),
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
                        PageTemplate(id='TwoCol',frames=[frame1,frame2], onPage=myLaterPages),
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
                        ])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
    doc.build(Elements,canvasmaker=myCanvasMaker)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    62
Elements = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    63
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    64
ChapterStyle = copy.deepcopy(styles["Heading1"])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    65
ChapterStyle.alignment = TA_CENTER
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
ChapterStyle.fontsize = 14
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    67
InitialStyle = copy.deepcopy(ChapterStyle)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
InitialStyle.fontsize = 16
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
InitialStyle.leading = 20
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    70
PreStyle = styles["Code"]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    71
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    72
def newPage():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    73
    Elements.append(PageBreak())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    74
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    75
chNum = 0
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
def myOnDrawCB(canv,kind,label):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    77
    print('myOnDrawCB(%s)'%kind, 'Page number=', canv.getPageNumber(), 'label value=', label)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    79
def chapter(txt, style=ChapterStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
    global chNum
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    81
    Elements.append(NextPageTemplate('OneCol'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
    newPage()
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    83
    chNum += 1
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
    if _NEW_PARA or not _CALLBACK:
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
    85
        Elements.append(Paragraph(txt, style))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    86
    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    87
        Elements.append(Paragraph(('foo<onDraw name="myOnDrawCB" label="chap %d"/> '%chNum)+txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
    Elements.append(Spacer(0.2*inch, 0.3*inch))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
    if useTwoCol:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
        Elements.append(NextPageTemplate('TwoCol'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    91
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
def fTitle(txt,style=InitialStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    93
    Elements.append(Paragraph(txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
ParaStyle = copy.deepcopy(styles["Normal"])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
ParaStyle.spaceBefore = 0.1*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    97
if 'right' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
    ParaStyle.alignment = TA_RIGHT
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
elif 'left' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
    ParaStyle.alignment = TA_LEFT
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
elif 'justify' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   102
    ParaStyle.alignment = TA_JUSTIFY
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
elif 'center' in sys.argv or 'centre' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   104
    ParaStyle.alignment = TA_CENTER
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   105
else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
    ParaStyle.alignment = TA_JUSTIFY
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   107
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   108
useTwoCol = 'notwocol' not in sys.argv
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   109
def spacer(inches):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   110
    Elements.append(Spacer(0.1*inch, inches*inch))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   111
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   112
def p(txt, style=ParaStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   113
    if _REDCAP:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   114
        fs, fe = '<font color="red" size="+2">', '</font>'
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   115
        n = len(txt)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   116
        for i in range(n):
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   117
            if 'a'<=txt[i]<='z' or 'A'<=txt[i]<='Z':
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   118
                txt = (txt[:i]+(fs+txt[i]+fe))+txt[i+1:]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   119
                break
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   120
        if _REDCAP>=2 and n>20:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   121
            j = i+len(fs)+len(fe)+1+int((n-1)/2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   122
            while not ('a'<=txt[j]<='z' or 'A'<=txt[j]<='Z'): j += 1
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   123
            txt = (txt[:j]+('<b><i><font size="+2" color="blue">'+txt[j]+'</font></i></b>'))+txt[j+1:]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   124
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   125
        if _REDCAP==3 and n>20:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   126
            n = len(txt)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   127
            fs = '<font color="green" size="+1">'
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   128
            for i in range(n-1,-1,-1):
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   129
                if 'a'<=txt[i]<='z' or 'A'<=txt[i]<='Z':
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   130
                    txt = txt[:i]+((fs+txt[i]+fe)+txt[i+1:])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   131
                    break
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   132
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   133
    Elements.append(Paragraph(txt, style))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   134
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   135
firstPre = 1
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   136
def pre(txt, style=PreStyle):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   137
    global firstPre
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   138
    if firstPre:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   139
        Elements.append(NextPageTemplate('OneCol'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   140
        newPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   141
        firstPre = 0
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   142
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   143
    spacer(0.1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   144
    p = Preformatted(txt, style)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   145
    Elements.append(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   146
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   147
def parseOdyssey(fn):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   148
    from time import time
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   149
    E = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   150
    t0=time()
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   151
    text = open(fn,'r').read()
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   152
    i0 = text.index('Book I')
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   153
    endMarker = 'covenant of peace between the two contending parties.'
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   154
    i1 = text.index(endMarker)+len(endMarker)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   155
    PREAMBLE=list(map(str.strip,text[0:i0].split('\n')))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   156
    L=list(map(str.strip,text[i0:i1].split('\n')))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   157
    POSTAMBLE=list(map(str.strip,text[i1:].split('\n')))
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   158
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   159
    def ambleText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   160
        while L and not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   161
        while L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   162
            T=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   163
            while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   164
                T.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   165
            yield T
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   166
            while L and not L[0]: L.pop(0)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   167
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   168
    def mainText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   169
        while L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   170
            B = L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   171
            while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   172
            T=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   173
            while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   174
                T.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   175
            while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   176
            P = []
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   177
            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
   178
                E=[]
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   179
                while L and L[0]:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   180
                    E.append(L.pop(0))
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   181
                P.append(E)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   182
                if L:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   183
                    while not L[0]: L.pop(0)
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   184
            yield B,T,P
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   185
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   186
    t1 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   187
    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
   188
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   189
    E.append([spacer,2])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   190
    E.append([fTitle,'<font color="red">%s</font>' % Title, InitialStyle])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   191
    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
   192
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   193
    for T in ambleText(PREAMBLE):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   194
        E.append([p,'\n'.join(T)])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   195
2969
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   196
    for (B,T,P) in mainText(L):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   197
        E.append([chapter,B])
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   198
        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
   199
        for x in P:
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   200
            E.append([p,' '.join(x)])
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   201
    firstPre = 1
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   202
    for T in ambleText(POSTAMBLE):
75353f5fafd0 reportlab-2.2: new odyssey in place
rgbecker
parents: 2967
diff changeset
   203
        E.append([p,'\n'.join(T)])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   204
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   205
    t3 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   206
    print("Parsing into memory took %.4f seconds" %(t3-t1))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   207
    del L
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   208
    t4 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   209
    print("Deleting list of lines took %.4f seconds" %(t4-t3))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   210
    for i in range(len(E)):
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 2969
diff changeset
   211
        E[i][0](*E[i][1:])
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   212
    t5 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   213
    print("Moving into platypus took %.4f seconds" %(t5-t4))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   214
    del E
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   215
    t6 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   216
    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
   217
    go()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   218
    t7 = time()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   219
    print("saving to PDF took %.4f seconds" %(t7-t6))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   220
    print("Total run took %.4f seconds"%(t7-t0))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   221
3951
cc24d5a46691 replaced md5 with hashlib
Andy Robinson <andy@reportlab.com>
parents: 3721
diff changeset
   222
    import hashlib
cc24d5a46691 replaced md5 with hashlib
Andy Robinson <andy@reportlab.com>
parents: 3721
diff changeset
   223
    print('file digest: %s' % hashlib.md5(open('dodyssey.pdf','rb').read()).hexdigest())
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   224
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   225
def run():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   226
    for fn in ('odyssey.full.txt','odyssey.txt'):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   227
        if os.path.isfile(fn):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   228
            parseOdyssey(fn)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   229
            break
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   230
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   231
def doProf(profname,func,*args,**kwd):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   232
        import hotshot, hotshot.stats
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   233
        prof = hotshot.Profile(profname)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   234
        prof.runcall(func)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   235
        prof.close()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   236
        stats = hotshot.stats.load(profname)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   237
        stats.strip_dirs()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   238
        stats.sort_stats('time', 'calls')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   239
        stats.print_stats(20)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   240
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   241
if __name__=='__main__':
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   242
    if '--prof' in sys.argv:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   243
        doProf('dodyssey.prof',run)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   244
    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   245
        run()