Changeset 63:11d3a9b5f97b for src/mlx
- Timestamp:
- 04/07/12 12:29:16 (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/web.py
r51 r63 12 12 import datetime 13 13 import codecs 14 import xml.sax 14 15 15 16 #--------------------------------------------------------------------------------------- … … 188 189 #------------------------------------------------------------------------------ 189 190 191 class NOTAM(object): 192 """A NOTAM for an airport.""" 193 def __init__(self, begin, notice, end = None, permanent = False, 194 repeatCycle = None): 195 """Construct the NOTAM.""" 196 self.begin = begin 197 self.notice = notice 198 self.end = end 199 self.permanent = permanent 200 self.repeatCycle = None 201 202 def __repr__(self): 203 """Get the representation of the NOTAM.""" 204 s = "<NOTAM " + str(self.begin) 205 if self.end: 206 s += " - " + str(self.end) 207 elif self.permanent: 208 s += " - PERMANENT" 209 if self.repeatCycle: 210 s += " (" + self.repeatCycle + ")" 211 s += ": " + self.notice 212 s += ">" 213 return s 214 215 #------------------------------------------------------------------------------ 216 217 class NOTAMHandler(xml.sax.handler.ContentHandler): 218 """A handler for the NOTAM database.""" 219 def __init__(self, airportICAOs): 220 """Construct the handler for the airports with the given ICAO code.""" 221 self._notams = {} 222 for icao in airportICAOs: 223 self._notams[icao] = [] 224 225 def startElement(self, name, attrs): 226 """Start an element.""" 227 if name!="notam" or \ 228 "A" not in attrs or not attrs["A"] or \ 229 "B" not in attrs or not attrs["B"] or \ 230 "E" not in attrs or not attrs["E"]: 231 return 232 233 icao = attrs["A"] 234 if icao not in self._notams: 235 return 236 237 begin = datetime.datetime.strptime(attrs["B"], "%Y-%m-%d %H:%M:%S") 238 239 c = attrs["C"] if "C" in attrs else None 240 end = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S") if c else None 241 242 permanent = attrs["C_flag"]=="PERM" if "C_flag" in attrs else False 243 244 repeatCycle = attrs["D"] if "D" in attrs else None 245 246 self._notams[icao].append(NOTAM(begin, attrs["E"], end = end, 247 permanent = permanent, 248 repeatCycle = repeatCycle)) 249 250 def get(self, icao): 251 """Get the NOTAMs for the given ICAO code.""" 252 return self._notams[icao] if icao in self._notams else [] 253 254 #------------------------------------------------------------------------------ 255 190 256 class Result(object): 191 257 """A result object. … … 350 416 #------------------------------------------------------------------------------ 351 417 418 class GetNOTAMs(Request): 419 """Get the NOTAMs from EURoutePro and select the ones we are interested 420 in.""" 421 def __init__(self, callback, departureICAO, arrivalICAO): 422 """Construct the request for the given airports.""" 423 super(GetNOTAMs, self).__init__(callback) 424 self._departureICAO = departureICAO 425 self._arrivalICAO = arrivalICAO 426 427 def run(self): 428 """Perform the plane update.""" 429 xmlParser = xml.sax.make_parser() 430 notamHandler = NOTAMHandler([self._departureICAO, self._arrivalICAO]) 431 xmlParser.setContentHandler(notamHandler) 432 433 url = "http://notams.euroutepro.com/notams.xml" 434 435 f = urllib2.urlopen(url) 436 try: 437 xmlParser.parse(f) 438 finally: 439 f.close() 440 441 result = Result() 442 result.departureNOTAMs = notamHandler.get(self._departureICAO) 443 result.arrivalNOTAMs = notamHandler.get(self._arrivalICAO) 444 445 return result 446 447 #------------------------------------------------------------------------------ 448 352 449 class Handler(threading.Thread): 353 450 """The handler for the web services. … … 375 472 """Update the status of the given plane.""" 376 473 self._addRequest(UpdatePlane(callback, tailNumber, status, gateNumber)) 474 475 def getNOTAMs(self, callback, departureICAO, arrivalICAO): 476 """Get the NOTAMs for the given two airports.""" 477 self._addRequest(GetNOTAMs(callback, departureICAO, arrivalICAO)) 377 478 378 479 def run(self): … … 407 508 #handler.getFleet(callback) 408 509 # Plane: HA-LEG home (gate 67) 409 handler.updatePlane(callback, "HA-LQC", const.PLANE_AWAY, "72") 410 time.sleep(3) 411 handler.getFleet(callback) 412 time.sleep(3) 413 414 #------------------------------------------------------------------------------ 510 #handler.updatePlane(callback, "HA-LQC", const.PLANE_AWAY, "72") 511 #time.sleep(3) 512 #handler.getFleet(callback) 513 #time.sleep(3) 514 515 handler.getNOTAMs(callback, "LHBP", "EPWA") 516 time.sleep(3) 517 518 519 #------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.