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

Source Code for Module sabx10.pdf_gen.instructions

  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  Create the table data and orchestrate turning it into PDF files. 
 23  """ 
 24  from sabx10.oxm import process_rides, parse_top_level, Turn, Stop, Poi 
 25   
 26  from instruction_pdf import InstructionPdfGenerator 
 27   
28 -def _convert_turn(turn, segs):
29 """ 30 Format a turn as a table item. 31 32 Landmark is either "Parking" or "T - #", with # being the turn index. 33 34 Distance is how far into the ride the turn occurs. 35 36 Description is the turn cue. 37 38 Length is the length of the segment turning on to, saying how long you stay 39 on the road you're turning on to. 40 41 @param turn: L{Turn} to process 42 @type turn: L{Turn} object 43 @param segs: segment list, to get Length from 44 @type segs: C{list} of L{Segment} 45 46 @return: tuple of (landmark, distance, description, length) 47 @rtype: (C{string},C{string},C{string},C{string}) 48 """ 49 if turn.index == 0: 50 landmark = "Parking" 51 else: 52 landmark = "T - %s" % turn.index 53 distance = "%.2f" % turn.distance 54 description = " ".join(turn.cue.split()) 55 length = "%.2f" % segs[turn.index].length 56 57 return (landmark, distance, description, length)
58
59 -def _convert_stop(stop):
60 """ 61 Format a stop as a table item. 62 63 Landmark is "S - #", with # being the stop index. 64 65 Distance is how far into the ride the stop occurs. 66 67 Description is the stop description. 68 69 Length is filled with the stop type. 70 71 @param stop: L{Stop} to process 72 @type stop: L{Stop} object 73 74 @return: tuple of (landmark, distance, description, length) 75 @rtype: (C{string},C{string},C{string},C{string}) 76 """ 77 landmark = "S - %s" % stop.index 78 if stop.off_route >= 0.1: 79 distance = "%.1f + %.1f" % (stop.distance, stop.off_route) 80 else: 81 distance = "%.1f" % stop.distance 82 if stop.off_route >= 0.1: 83 description = "%s (%.1f miles off route)" % (stop.description, 84 stop.off_route) 85 description = " ".join(description.split()) 86 else: 87 description = " ".join(stop.description.split()) 88 length = stop.type 89 90 return (landmark, distance, description, length)
91
92 -def _convert_poi(poi):
93 """ 94 Format a poi as a table item. 95 96 Landmark is "P - #", with # being the poi index. 97 98 Distance is how far into the ride the poi occurs. 99 100 Description is the poi description. 101 102 Length is filled with the string "POI". 103 104 @param poi: L{Poi} to process 105 @type poi: L{Poi} object 106 107 @return: tuple of (landmark, distance, description, length) 108 @rtype: (C{string},C{string},C{string},C{string}) 109 """ 110 landmark = "P - %s" % poi.index 111 if poi.off_route >= 0.1: 112 distance = "%.1f + %.1f" % (poi.distance, poi.off_route) 113 else: 114 distance = "%.1f" % poi.distance 115 if poi.off_route >= 0.1: 116 description = "%s (%.1f miles off route)" % (poi.description, 117 poi.off_route) 118 description = " ".join(description.split()) 119 else: 120 description = " ".join(poi.description.split()) 121 length = "POI" 122 123 return (landmark, distance, description, length)
124
125 -def _create_table_data(ride):
126 """ 127 Create a list of data lines to generate the PDF table from. The list is 128 filled with the turns, stops, and pois for the given ride. 129 130 @param ride: L{Ride} to process 131 @type ride: L{Ride} object 132 133 @return: C{list} of (landmark, distance, description, length) 134 @rtype: C{list} of (C{string},C{string},C{string},C{string}) 135 """ 136 processed = [] 137 for item in ride.combined: 138 if isinstance(item, Turn): 139 processed.append(_convert_turn(item, ride.segs)) 140 elif isinstance(item, Stop): 141 processed.append(_convert_stop(item)) 142 elif isinstance(item, Poi): 143 processed.append(_convert_poi(item)) 144 145 processed.append( ( 146 "Finish", 147 "%.2f" % ride.distance, 148 "Arrive at finish!", 149 "Finish" 150 )) 151 152 return processed
153
154 -def pdf_all_rides(xml_tree, out_dir, out_base):
155 """ 156 Generate instruction PDF files for each ride in a rideset. 157 158 @param xml_tree: C{ElementTree} representation of a rideset 159 @type xml_tree: C{ElementTree} stuff 160 @param out_dir: directory to write the PDF files to 161 @type out_dir: C{string} 162 @param out_base: file name base for PDF files 163 @type out_base: C{string} 164 """ 165 sabx = parse_top_level(xml_tree) 166 ride_list, bounds = process_rides(xml_tree) 167 pdf_gen = InstructionPdfGenerator(sabx['title'], out_dir, out_base) 168 for ride in ride_list: 169 table_data = _create_table_data(ride) 170 pdf_gen.process_ride(ride.index, ride.distance, table_data)
171