Changeset 742:464227881300 for src


Ignore:
Timestamp:
01/02/16 12:25:47 (9 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Extracted some common classes (re #283).

Location:
src/mlx
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r740 r742  
    22import const
    33import util
     4import rpccommon
    45
    56from common import MAVA_BASE_URL
     
    257258#------------------------------------------------------------------------------
    258259
    259 class Plane(object):
     260class Plane(rpccommon.Plane):
    260261    """Information about an airplane in the fleet."""
    261262    def __init__(self, s):
     
    265266        The first field is the tail number, the second field is the gate
    266267        number, the third field is the plane's status as a character."""
     268        super(Plane, self).__init__()
     269
    267270        try:
    268271            words = s.split(" ")
     
    271274
    272275            status = words[2] if len(words)>2 else None
    273             self.status = const.PLANE_HOME if status=="H" else \
    274                           const.PLANE_AWAY if status=="A" else \
    275                           const.PLANE_PARKING if status=="P" else \
    276                           const.PLANE_UNKNOWN
     276            self._setStatus(status)
    277277
    278278            gateNumber = words[1] if len(words)>1 else ""
     
    283283            self.tailNumber = None
    284284
    285     def __repr__(self):
    286         """Get the representation of the plane object."""
    287         s = "<Plane: %s %s" % (self.tailNumber,
    288                                "home" if self.status==const.PLANE_HOME else \
    289                                "away" if self.status==const.PLANE_AWAY else \
    290                                "parking" if self.status==const.PLANE_PARKING \
    291                                else "unknown")
    292         if self.gateNumber is not None:
    293             s += " (gate " + self.gateNumber + ")"
    294         s += ">"
    295         return s
    296 
    297 
    298 #------------------------------------------------------------------------------
    299 
    300 class Fleet(object):
     285#------------------------------------------------------------------------------
     286
     287class Fleet(rpccommon.Fleet):
    301288    """Information about the whole fleet."""
    302289    def __init__(self, f):
    303290        """Construct the fleet information by reading the given file object."""
    304         self._planes = {}
     291        super(Fleet, self).__init__()
     292
    305293        while True:
    306294            line = readline(f)
     
    308296
    309297            plane = Plane(line)
    310             if plane.tailNumber is not None:
    311                 self._planes[plane.tailNumber] = plane
    312 
    313     def isGateConflicting(self, plane):
    314         """Check if the gate of the given plane conflicts with another plane's
    315         position."""
    316         for p in self._planes.itervalues():
    317             if p.tailNumber!=plane.tailNumber and \
    318                p.status==const.PLANE_HOME and \
    319                p.gateNumber==plane.gateNumber:
    320                 return True
    321 
    322         return False
    323 
    324     def getOccupiedGateNumbers(self):
    325         """Get a set containing the numbers of the gates occupied by planes."""
    326         gateNumbers = set()
    327         for p in self._planes.itervalues():
    328             if p.status==const.PLANE_HOME and p.gateNumber:
    329                 gateNumbers.add(p.gateNumber)
    330         return gateNumbers
    331 
    332     def updatePlane(self, tailNumber, status, gateNumber = None):
    333         """Update the status of the given plane."""
    334         if tailNumber in self._planes:
    335             plane = self._planes[tailNumber]
    336             plane.status = status
    337             plane.gateNumber = gateNumber
    338 
    339     def __iter__(self):
    340         """Get an iterator over the planes."""
    341         for plane in self._planes.itervalues():
    342             yield plane
    343 
    344     def __getitem__(self, tailNumber):
    345         """Get the plane with the given tail number.
    346 
    347         If the plane is not in the fleet, None is returned."""
    348         return self._planes[tailNumber] if tailNumber in self._planes else None
    349 
    350     def __repr__(self):
    351         """Get the representation of the fleet object."""
    352         return self._planes.__repr__()
     298            self._addPlane(plane)
    353299
    354300#------------------------------------------------------------------------------
     
    771717        url = MAVA_BASE_URL + "/onlinegates_set.php"
    772718
    773         status = "H" if self._status==const.PLANE_HOME else \
    774                  "A" if self._status==const.PLANE_AWAY else \
    775                  "P" if self._status==const.PLANE_PARKING else ""
     719        status = Plane.status2str(self._status)
    776720
    777721        gateNumber = self._gateNumber if self._gateNumber else ""
Note: See TracChangeset for help on using the changeset viewer.