source: src/mlx/pirep.py@ 420:fc063b7b571e

Last change on this file since 420:fc063b7b571e was 401:15ad3c16ee11, checked in by István Váradi <ivaradi@…>, 12 years ago

The exception strings are converted from UTF-8 to unicode for proper logging (re #170)

File size: 5.8 KB
Line 
1
2from util import utf2unicode
3
4import const
5import cPickle as pickle
6
7#------------------------------------------------------------------------------
8
9## @package mlx.pirep
10#
11# The PIREP module.
12#
13# This module defines only one class, \ref PIREP. It is used to extract and
14# store the information needed for a PIREP. The saved PIREPs are pickled
15# instances of this class.
16
17#------------------------------------------------------------------------------
18
19class PIREP(object):
20 """A pilot's report of a flight."""
21 delayCodeNames = { const.DELAYCODE_LOADING : "Loading Problems",
22 const.DELAYCODE_NETWORK : "Net Problems",
23 const.DELAYCODE_SYSTEM : "System Crash/Freezing",
24 const.DELAYCODE_TRAFFIC : "Traffic Problems",
25 const.DELAYCODE_WEATHER : "Weather Problems",
26 const.DELAYCODE_VATSIM : "VATSIM Problem",
27 const.DELAYCODE_CONTROLLER : "Controller's Fault",
28 const.DELAYCODE_NAVIGATION : "Navigation Problem",
29 const.DELAYCODE_APRON : "Apron Navigation Problems",
30 const.DELAYCODE_PERSONAL : "Personal Reasons" }
31
32 @staticmethod
33 def _formatLine(timeStr, line):
34 """Format the given time string and line as needed for the ACARS and
35 some other things."""
36 return "[" + timeStr + "]-[" + line + "]"
37
38 @staticmethod
39 def load(path):
40 """Load a PIREP from the given path.
41
42 Returns the PIREP object, or None on error."""
43 try:
44 with open(path, "rb") as f:
45 pirep = pickle.load(f)
46 if "numCrew" not in dir(pirep):
47 pirep.numCrew = pirep.bookedFlight.numCrew
48 if "numPassengers" not in dir(pirep):
49 pirep.numPassengers = pirep.bookedFlight.numPassengers
50 if "bagWeight" not in dir(pirep):
51 pirep.bagWeight = pirep.bookedFlight.bagWeight
52 if "mailWeight" not in dir(pirep):
53 pirep.mailWeight = pirep.bookedFlight.mailWeight
54 return pirep
55 except Exception, e:
56 print "Failed loading PIREP from %s: %s" % (path,
57 utf2unicode(str(e)))
58 return None
59
60 def __init__(self, flight):
61 """Initialize the PIREP from the given flight."""
62 self.bookedFlight = flight.bookedFlight
63
64 self.numCrew = flight.numCrew
65 self.numPassengers = flight.numPassengers
66 self.bagWeight = flight.bagWeight
67 self.cargoWeight = flight.cargoWeight
68 self.mailWeight = flight.mailWeight
69
70 self.filedCruiseAltitude = flight.filedCruiseAltitude
71 self.cruiseAltitude = flight.cruiseAltitude
72 self.route = flight.route
73
74 self.departureMETAR = flight.departureMETAR.upper()
75 self.arrivalMETAR = flight.arrivalMETAR.upper()
76
77 self.departureRunway = flight.departureRunway.upper()
78 self.sid = flight.sid.upper()
79
80 self.star = flight.star
81 self.transition = flight.transition
82 self.approachType = flight.approachType.upper()
83 self.arrivalRunway = flight.arrivalRunway.upper()
84
85 self.flightType = flight.flightType
86 self.online = flight.online
87
88 self.comments = flight.comments
89 self.flightDefects = flight.flightDefects
90 self.delayCodes = flight.delayCodes
91
92 self.blockTimeStart = flight.blockTimeStart
93 self.flightTimeStart = flight.flightTimeStart
94 self.flightTimeEnd = flight.flightTimeEnd
95 self.blockTimeEnd = flight.blockTimeEnd
96 self.flownDistance = flight.flownDistance
97 self.fuelUsed = flight.startFuel - flight.endFuel
98
99 logger = flight.logger
100 self.rating = logger.getRating()
101 self.logLines = logger.lines
102 self.faultLineIndexes = logger.faultLineIndexes
103
104 def getACARSText(self):
105 """Get the ACARS text.
106
107 This is a specially formatted version of the log without the faults."""
108 text = "[MAVA LOGGER X LOG]-[%s]" % (const.VERSION,)
109 for index in range(0, len(self.logLines)):
110 if index not in self.faultLineIndexes:
111 (timeStr, line) = self.logLines[index]
112 if timeStr is not None:
113 text += PIREP._formatLine(timeStr, line)
114 return text
115
116 def getRatingText(self):
117 """Get the rating text.
118
119 This is a specially formatted version of the lines containing the
120 faults."""
121 text = ""
122 for index in self.faultLineIndexes:
123 (timeStr, line) = self.logLines[index]
124 if timeStr is not None:
125 text += PIREP._formatLine(timeStr, line)
126 text += "\n"
127
128 text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
129
130 return text
131
132 def getTimeComment(self):
133 """Get the time comment.
134
135 This is basically a collection of the delay codes, if any."""
136 if not self.delayCodes:
137 return "UTC"
138 else:
139 s = ""
140 for code in self.delayCodes:
141 if s: s += ", "
142 s += PIREP.delayCodeNames[code]
143 return s
144
145 def getSTAR(self):
146 """Get the STAR and/or the transition."""
147 star = self.star if self.star is not None else ""
148 if self.transition is not None:
149 if star: star += ", "
150 star += self.transition
151 return star.upper()
152
153 def save(self, path):
154 """Save the PIREP to the given file.
155
156 Returns whether the saving has succeeded."""
157 try:
158 with open(path, "wb") as f:
159 pickle.dump(self, f)
160 return None
161 except Exception, e:
162 error = utf2unicode(str(e))
163 print u"Failed saving PIREP to %s: %s" % (path, error)
164 return error
Note: See TracBrowser for help on using the repository browser.