Changeset 859:1e789c934953 for src/mlx/rpc.py
- Timestamp:
- 06/18/17 11:17:51 (7 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/rpc.py
r858 r859 66 66 "arrivalTime": lambda value: ScheduledFlight._decodeTime(value), 67 67 "duration": lambda value: ScheduledFlight._decodeDuration(value), 68 "type": int, 69 "spec": int 68 "spec": int, 69 "validFrom": lambda value: ScheduledFlight._decodeDate(value), 70 "validTo": lambda value: ScheduledFlight._decodeDate(value), 71 "date": lambda value: ScheduledFlight._decodeDate(value) 70 72 } 71 73 … … 74 76 """Decode the given value as a time value.""" 75 77 return datetime.datetime.strptime(value, "%H:%M:%S").time() 78 79 @staticmethod 80 def _decodeDate(value): 81 """Decode the given value as a date value.""" 82 if not value or value=="0000-00-00": 83 return const.defaultDate 84 else: 85 return datetime.datetime.strptime(value, "%Y-%m-%d").date() 76 86 77 87 @staticmethod … … 90 100 del self.typeCode 91 101 102 self.type = ScheduledFlight.TYPE_VIP if self.spec==1 \ 103 else ScheduledFlight.TYPE_NORMAL 104 del self.spec 105 106 def compareBy(self, other, name): 107 """Compare this flight with the other one according to the given 108 attribute name.""" 109 if name=="callsign": 110 try: 111 cs1 = int(self.callsign[2:]) 112 cs2 = int(other.callsign[2:]) 113 return cmp(cs1, cs2) 114 except: 115 return cmp(self.callsign, other.callsign) 116 else: 117 return cmp(getattr(self, name), getattr(other, name)) 118 92 119 def __repr__(self): 93 120 return "ScheduledFlight<%d, %d, %s, %s (%s) - %s (%s) -> %d, %d>" % \ … … 95 122 self.departureICAO, str(self.departureTime), 96 123 self.arrivalICAO, str(self.arrivalTime), 97 self.duration, self. spec)124 self.duration, self.type) 98 125 99 126 #--------------------------------------------------------------------------------------- … … 104 131 Occasionally, one of the flights may be missing.""" 105 132 @staticmethod 106 def scheduledFlights2Pairs(scheduledFlights ):133 def scheduledFlights2Pairs(scheduledFlights, date): 107 134 """Convert the given list of scheduled flights into a list of flight 108 135 pairs.""" 136 weekday = str(date.weekday()+1) 137 109 138 flights = {} 139 weekdayFlights = {} 110 140 for flight in scheduledFlights: 111 141 flights[flight.id] = flight 142 if (flight.type==ScheduledFlight.TYPE_NORMAL and 143 flight.arrivalICAO!="LHBP" and weekday in flight.days and 144 flight.validFrom<=date and flight.validTo>=date) or \ 145 flight.type==ScheduledFlight.TYPE_VIP: 146 weekdayFlights[flight.id] = flight 112 147 113 148 flightPairs = [] 114 149 115 while flights: 116 (id, flight) = flights.popitem() 117 pairID = flight.pairID 118 if pairID in flights: 119 pairFlight = flights[pairID] 120 if flight.departureICAO=="LHBP" or \ 121 (pairFlight.departureICAO!="LHBP" and id<pairID): 122 flightPairs.append(ScheduledFlightPair(flight, pairFlight)) 123 else: 124 flightPairs.append(ScheduledFlightPair(pairFlight, flight)) 125 del flights[pairID] 126 else: 150 while weekdayFlights: 151 (id, flight) = weekdayFlights.popitem() 152 if flight.type==ScheduledFlight.TYPE_NORMAL: 153 pairID = flight.pairID 154 if pairID in flights: 155 pairFlight = flights[pairID] 156 if flight.departureICAO=="LHBP" or \ 157 (pairFlight.departureICAO!="LHBP" and 158 flight.callsign<pairFlight.callsign): 159 flightPairs.append(ScheduledFlightPair(flight, pairFlight)) 160 else: 161 flightPairs.append(ScheduledFlightPair(pairFlight, flight)) 162 del flights[pairID] 163 if pairID in weekdayFlights: 164 del weekdayFlights[pairID] 165 elif flight.type==ScheduledFlight.TYPE_VIP: 127 166 flightPairs.append(ScheduledFlightPair(flight)) 167 168 flightPairs.sort(cmp = lambda pair1, pair2: 169 cmp(pair1.flight0.date, pair2.flight0.date)) 128 170 129 171 return flightPairs … … 133 175 self.flight0 = flight0 134 176 self.flight1 = flight1 177 178 def compareBy(self, other, name): 179 """Compare this flight pair with the other one according to the given 180 attribute name, considering the first flights.""" 181 return self.flight0.compareBy(other.flight0, name) 182 183 def __repr__(self): 184 return "ScheduledFlightPair<%s, %s, %s>" % \ 185 (self.flight0.callsign, self.flight0.departureICAO, 186 self.flight0.arrivalICAO) 135 187 136 188 #--------------------------------------------------------------------------------------- … … 280 332 _instructions = { 281 333 "status" : lambda value: rpccommon.Plane.str2status(value), 282 "gateNumber" : lambda value: value if value else None 334 "gateNumber" : lambda value: value if value else None, 335 "typeCode": lambda value: BookedFlight._decodeAircraftType(value) 283 336 } 284 337 … … 286 339 """Construct the plane.""" 287 340 RPCObject.__init__(self, value, instructions = Plane._instructions) 341 self.aircraftType = self.typeCode 342 del self.typeCode 288 343 289 344 #--------------------------------------------------------------------------------------- … … 524 579 self._server.getTimetable(sessionID, 525 580 date.strftime("%Y-%m-%d"), 526 date.weekday() +1,581 date.weekday()+1, 527 582 typeCodes)) 528 583 return ScheduledFlightPair.scheduledFlights2Pairs([ScheduledFlight(value) 529 for value in values]) 584 for value in values], 585 date) 586 587 def bookFlights(self, flightIDs, date, tailNumber): 588 """Book the flights with the given IDs on the given date to be flown 589 with the plane of the given tail number.""" 590 values = self._performCall(lambda sessionID: 591 self._server.bookFlights(sessionID, 592 flightIDs, 593 date.strftime("%Y-%m-%d"), 594 tailNumber)) 595 return [BookedFlight(value) for value in values] 530 596 531 597 def _performCall(self, callFn, acceptResults = []):
Note:
See TracChangeset
for help on using the changeset viewer.