import const import rpccommon from common import MAVA_BASE_URL import jsonrpclib import hashlib import datetime #--------------------------------------------------------------------------------------- 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 value = instruction(value) setattr(self, key, value) #--------------------------------------------------------------------------------------- class Reply(RPCObject): """The generic reply structure.""" #--------------------------------------------------------------------------------------- class BookedFlight(RPCObject): """A booked flight.""" # FIXME: copied from web.BookedFlight TYPECODE2TYPE = { "736" : const.AIRCRAFT_B736, "73G" : const.AIRCRAFT_B737, "738" : const.AIRCRAFT_B738, "73H" : const.AIRCRAFT_B738C, "733" : const.AIRCRAFT_B733, "734" : const.AIRCRAFT_B734, "735" : const.AIRCRAFT_B735, "DH4" : const.AIRCRAFT_DH8D, "762" : const.AIRCRAFT_B762, "763" : const.AIRCRAFT_B763, "CR2" : const.AIRCRAFT_CRJ2, "F70" : const.AIRCRAFT_F70, "LI2" : const.AIRCRAFT_DC3, "TU3" : const.AIRCRAFT_T134, "TU5" : const.AIRCRAFT_T154, "YK4" : const.AIRCRAFT_YK40, "146" : const.AIRCRAFT_B462 } # FIXME: copied from web.BookedFlight @staticmethod def _decodeAircraftType(typeCode): """Decode the aircraft type from the given typeCode.""" if typeCode in BookedFlight.TYPECODE2TYPE: return BookedFlight.TYPECODE2TYPE[typeCode] else: raise Exception("Invalid aircraft type code: '" + typeCode + "'") # FIXME: copied from web.BookedFlight @staticmethod def getDateTime(date, time): """Get a datetime object from the given textual date and time.""" return datetime.datetime.strptime(date + " " + time, "%Y-%m-%d %H:%M:%S") # The instructions for the construction _instructions = { "numPassengers" : int, "numCrew" : int, "bagWeight" : int, "cargoWeight" : int, "mailWeight" : int, "aircraftType" : lambda value: BookedFlight._decodeAircraftType(value) } def __init__(self, value): """Construct the booked flight object from the given RPC result value.""" super(BookedFlight, self).__init__(value, BookedFlight._instructions) self.departureTime = \ BookedFlight.getDateTime(self.date, self.departureTime) self.arrivalTime = \ BookedFlight.getDateTime(self.date, self.arrivalTime) if self.arrivalTime