import const import rpccommon from common import MAVA_BASE_URL import jsonrpclib import hashlib import datetime import calendar import sys #--------------------------------------------------------------------------------------- class RPCObject(object): """Base class for objects read from RPC calls. It is possible to construct it from a dictionary.""" def __init__(self, value, instructions = {}): """Construct the object. value is the dictionary returned by the call. info is a mapping from names to 'instructions' on what to do with the corresponding values. If the instruction is None, it will be ignored. If the instruction is a function, the value will be passed to it and the return value will be stored in the object. For all other names, the value will be stored as the same-named attribute.""" for (key, value) in value.iteritems(): if key in instructions: instruction = instructions[key] if instruction is None: continue try: value = instruction(value) except: print >> sys.stderr, "Failed to convert value '%s' of attribute '%s':" % \ (value, key) import traceback traceback.print_exc() setattr(self, key, value) #--------------------------------------------------------------------------------------- class Reply(RPCObject): """The generic reply structure.""" #--------------------------------------------------------------------------------------- class ScheduledFlight(RPCObject): """A scheduled flight in the time table.""" # The instructions for the construction # Type: normal flight TYPE_NORMAL = 0 # Type: VIP flight TYPE_VIP = 1 _instructions = { "id" : int, "pairID": int, "typeCode": lambda value: BookedFlight._decodeAircraftType(value), "departureTime": lambda value: ScheduledFlight._decodeTime(value), "arrivalTime": lambda value: ScheduledFlight._decodeTime(value), "duration": lambda value: ScheduledFlight._decodeDuration(value), "type": int, "spec": int } @staticmethod def _decodeTime(value): """Decode the given value as a time value.""" return datetime.datetime.strptime(value, "%H:%M:%S").time() @staticmethod def _decodeDuration(value): """Decode the given value as a duration. A number of seconds will be returned.""" t = datetime.datetime.strptime(value, "%H:%M:%S") return (t.hour*60 + t.minute) * 60 + t.second def __init__(self, value): """Construct the scheduled flight object from the given JSON value.""" super(ScheduledFlight, self).__init__(value, ScheduledFlight._instructions) self.aircraftType = self.typeCode del self.typeCode def __repr__(self): return "ScheduledFlight<%d, %d, %s, %s (%s) - %s (%s) -> %d, %d>" % \ (self.id, self.pairID, BookedFlight.TYPE2TYPECODE[self.aircraftType], self.departureICAO, str(self.departureTime), self.arrivalICAO, str(self.arrivalTime), self.duration, self.spec) #--------------------------------------------------------------------------------------- class ScheduledFlightPair(object): """A pair of scheduled flights. Occasionally, one of the flights may be missing.""" @staticmethod def scheduledFlights2Pairs(scheduledFlights): """Convert the given list of scheduled flights into a list of flight pairs.""" flights = {} for flight in scheduledFlights: flights[flight.id] = flight flightPairs = [] while flights: (id, flight) = flights.popitem() pairID = flight.pairID if pairID in flights: pairFlight = flights[pairID] if flight.departureICAO=="LHBP" or \ (pairFlight.departureICAO!="LHBP" and id