Package sabx10 :: Package pdf_gen :: Module instruction_pdf
[hide private]
[frames] | no frames]

Source Code for Module sabx10.pdf_gen.instruction_pdf

  1  ############################################################################### 
  2  # 
  3  # sabx10 - an SABX file manipulation library 
  4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  5  # 
  6  # This file is part of sabx10. 
  7  # 
  8  # sabx10 is free software: you can redistribute it and/or modify it under the 
  9  # terms of the GNU General Public License as published by the Free Software 
 10  # Foundation, either version 3 of the License, or (at your option) any later 
 11  # version. 
 12  # 
 13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along with 
 18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
 19  # 
 20  ############################################################################### 
 21  """ 
 22  Take table data and turn it into PDF files. Reportlab is used to generate the 
 23  PDF files. 
 24  """ 
 25  import os.path 
 26   
 27  from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph 
 28  from reportlab.lib.pagesizes import letter 
 29  from reportlab.lib import colors 
 30  from reportlab.lib.units import inch 
 31  from reportlab.lib.styles import getSampleStyleSheet 
 32   
 33  from sabx10.map import BORDER 
 34   
 35  from consts import COL_WIDTH_1, COL_WIDTH_2, COL_WIDTH_4, COL_WIDTH_ALL 
 36   
37 -class InstructionPdfGenerator(object):
38 - def __init__(self, title, out_dir, out_base):
39 """ 40 Save the title, out_dir, and out_base, then setup the style sheet and 41 column widths. 42 43 @param title: base title for the PDF file 44 @type title: C{string} 45 @param out_dir: directory to write the PDF files to 46 @type out_dir: C{string} 47 @param out_base: file name base for PDF files 48 @type out_base: C{string} 49 """ 50 self.title = title 51 self.out_dir = out_dir 52 self.out_base = out_base 53 54 stylesheet = getSampleStyleSheet() 55 self.normalStyle = stylesheet['Normal'] 56 self.headerStyle = stylesheet['Heading3'] 57 58 page_width, page_height = letter 59 self.colWidths = [COL_WIDTH_1 * inch, 60 COL_WIDTH_2 * inch, 61 page_width - \ 62 (BORDER * 2.0 * inch) - (COL_WIDTH_ALL * inch), 63 COL_WIDTH_4 * inch]
64
65 - def _create_titles(self):
66 """ 67 Create the column headers for the output table. 68 69 @return: tuple of C{Paragraph}s containing ("Landmark", "Distance", 70 "Description", "Length") 71 @rtype: (C{Paragraph},C{Paragraph},C{Paragraph},C{Paragraph}) 72 """ 73 return ( 74 Paragraph("Landmark", self.headerStyle), 75 Paragraph("Distance", self.headerStyle), 76 Paragraph("Description", self.headerStyle), 77 Paragraph("Length", self.headerStyle) 78 )
79
80 - def _massage_table_data(self, table_data):
81 """ 82 Turn the titles and generic table data into objects recognized and 83 properly formatted by Reportlab. Basically, turn the description field 84 from a regular C{string} into a C{Paragraph} so it'll be wrapped 85 properly. 86 87 @param table_data: data to format 88 @type table_data: C{list} of (C{string},C{string},C{string},C{string}) 89 90 @return: new C{list} with processed data 91 @rtype: C{list} of (C{string},C{string},C{Paragraph},C{string}) 92 """ 93 processed = [self._create_titles()] 94 for line in table_data: 95 processed.append( (line[0], line[1], 96 Paragraph(line[2], self.normalStyle), line[3]) ) 97 return processed
98
99 - def process_ride(self, index, distance, table_data):
100 """ 101 Take table data for a ride and turn it into a PDF file containing the 102 table. 103 104 @param index: index of ride 105 @type index: C{int} 106 @param distance: length of ride 107 @type distance: C{float} 108 @param table_data: C{list} of (landmark, distance, description, length) 109 @type table_data: C{list} of (C{string},C{string},C{string},C{string}) 110 """ 111 table_data = self._massage_table_data(table_data) 112 113 tbl = Table(table_data, colWidths=self.colWidths, repeatRows=1) 114 tbl.setStyle(TableStyle([('ALIGN', (0,0), (-1,-1), 'LEFT'), 115 ('VALIGN', (0,0), (-1,-1), 'TOP'), 116 ('GRID', (0,0), (-1,-1), 1, colors.black)])) 117 doc_name = "%s_%s.pdf" % (self.out_base, index) 118 doc_name = os.path.join(self.out_dir, doc_name) 119 doc = SimpleDocTemplate(doc_name, 120 title="%s - %.1f Miles" % (self.title, 121 distance), 122 author="SABikeRides.com", 123 pagesize=letter, 124 leftMargin = BORDER * inch, 125 rightMargin = BORDER * inch, 126 topMargin = BORDER * inch, 127 bottomMargin = BORDER * inch) 128 doc.build([tbl])
129