source: src/mlx/pirep.py@ 298:24c67ec5cdca

Last change on this file since 298:24c67ec5cdca was 298:24c67ec5cdca, checked in by István Váradi <ivaradi@…>, 12 years ago

Documented the non-GUI modules

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