from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import cm
import operator
import os
import ConfigParser
import string

config = ConfigParser.ConfigParser()
config.read(os.environ["HOME"] + "/.abook/addressbook")
config.remove_section('format')

PAGE_HEIGHT=defaultPageSize[1]; PAGE_WIDTH=defaultPageSize[0]
styles = getSampleStyleSheet()

buchstabe = "A"


Title = "Hello world"
pageinfo = "platypus example"

def Pages(canvas, doc):
    canvas.saveState()
    canvas.restoreState()

def go(buchstabe):
    doc = SimpleDocTemplate("phello.pdf")
    Story = []
    style = styles["Normal"]
    addresses=[]
    for s in config.sections():
        nb=""
        ub=""
        mb=""
        if config.has_option(s,'name'):
            nb = "<b>" + config.get(s,'name') + "</b><br/>"    
            worte=config.get(s,'name').split()
            print len(worte)
            if len(worte)<2:
                nachname=worte[0]
            else:
                nachname=worte[1]
            anfangsbuchstabe=nachname[0:1]
            if anfangsbuchstabe!=buchstabe:
                buchstabe=anfangsbuchstabe
                print buchstabe
                p = Table(addresses)
                p.setStyle(TableStyle([('VALIGN',(0,0),(-1,-1),"TOP"),
                                        ('ALIGN',(0,-1),(0,-1),'RIGHT')]))

                Story.append(p)
                Story.append(PageBreak())
                addresses=[]

        if config.has_option(s,'address'):
            
    
        if config.has_option(s,'zip'):
            nb = nb + config.get(s,'zip') + " "    
        if config.has_option(s,'city'):
            nb = nb + config.get(s,'city') + "<br/>"    
        if config.has_option(s,'state'):
            nb = nb + config.get(s,'state') + " - "    
        if config.has_option(s,'country'):
            nb = nb + config.get(s,'country') + "<br/>"    
        nb = nb +"<br/>"

        if config.has_option(s,'phone'):
            ub= "Fon: " + config.get(s,'phone') + "<br/>"    
        if config.has_option(s,'mobile'):
            ub= ub + "Mobi: " + config.get(s,'mobile') + "<br/>"    
        if config.has_option(s,'email'):
            ub= ub + config.get(s,'email').replace(',','<br/>') + "<br/>"    
        ub=ub+"<br/>"

        if config.has_option(s,'custom3'):
            mb= config.get(s,'custom3') + "<br/>"    
        mb=mb+"<br/>"

        nameblock = Paragraph(nb,style)
        numberblock = Paragraph(ub,style)
        middleblock = Paragraph(mb,style)
        
        addresses.append([nameblock,middleblock,numberblock])

    p = Table(addresses)
    p.setStyle(TableStyle([('VALIGN',(0,0),(-1,-1),"TOP"),
        ('ALIGN',(0,-1),(0,-1),'RIGHT')]))

    Story.append(p)
    doc.build(Story, onFirstPage=Pages, onLaterPages=Pages)

go(buchstabe)

nb = nb + config.get(s,'address') + "<br/>"