source: src/mlx/pirep.py@ 187:5995d9ded144

Last change on this file since 187:5995d9ded144 was 151:a2584357ff6c, checked in by István Váradi <ivaradi@…>, 13 years ago

Added support for saving and loading PIREPs

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